我有一个在Unity PC上的Unity中创建的游戏,它运行正常。但是,当我试图创建一个显示在unity应用程序顶部的自定义UIView时,它会在iOS上运行。
为此,我在Assert-> Plugin-> iOS中创建了一个单音类,如下所示
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyNativeClass : NSObject{
NSDate *creationDate;
}
@end
@implementation MyNativeClass
static MyNativeClass * _sharedInstence;
+(MyNativeClass *)sharedInstence{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Instance Class created");
_sharedInstence =[[MyNativeClass alloc]init];
});
return _sharedInstence;
}
-(id)init{
self = [super init];
if(self){
[self initHelper];
}
return self;
}
-(void)initHelper{
NSLog(@"Init helper called");
creationDate = [NSDate date];
}
-(double)getElapsedTime{
NSLog(@"getElapsedTime");
return [[NSDate date]timeIntervalSinceDate:creationDate];
}
// condensed version
- (UIWindow*) getTopApplicationWindow
{
// grabs the top most window
NSArray* windows = [[UIApplication sharedApplication] windows];
return ([windows count] > 0) ? windows[0] : nil;
}
- (UIViewController*) getTopViewController
{
// get the top most window
UIWindow *window = [self getTopApplicationWindow];
// get the root view controller for the top most window
UIViewController *vc = window.rootViewController;
while (vc.presentedViewController != nil)
{
vc = vc.presentedViewController;
}
return vc;
}
-(UIView*)createView{
CGRect rect = UIScreen.mainScreen.bounds;
float xAxis = (rect.size.width-100)/2;
float yAxis = (rect.size.height-100)/2;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xAxis, yAxis, 100, 100)];
view.backgroundColor = [UIColor redColor];
return view;
}
-(void)addViewToCurrentController{
NSLog(@"addViewToWindow caled");
UIView *view = [self createView];
UIViewController *vc = [self getTopViewController];
NSLog(@"addViewToWindow caled %@",view);
[vc.view addSubview:view];
view = nil;
}
@end
extern "C"{
double IOSgetElapsedTime(){
NSLog(@"IOSgetElapsedTime");
return [[MyNativeClass sharedInstence] getElapsedTime];
}
void addViewToController()
{
NSLog(@"getaddedViewToWindow caled");
// calls the method to display our view controller over the unity controller
[[MyNativeClass sharedInstence] addViewToCurrentController];
}
} // end of extern C block
此后,我在FrameworkBridge中注册了如下相同的方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using AOT;
public class FrameworkBridge : MonoBehaviour
{
#if UNITY_IOS
[DllImport ("__Internal")]
private static extern void switchUserRole (string message);
[DllImport ("__Internal")]
private static extern double IOSgetElapsedTime();
[DllImport ("__Internal")]
private static extern void addViewToController();
#endif
void Start()
{
Debug.Log ("Elapsed Time: " + getElapsedTime());
// Debug.Log ("View Created: " + ShowMyCustomView());
//addViewToWindow();
Debug.Log ("ShowMyCustomView");
addViewToCurrentController();
Debug.Log ("ShowMyCustomView");
}
int frameCounter = 0;
// Update is called once per frame
void Update()
{
frameCounter ++;
if(frameCounter>=5){
Debug.Log("Tick: " + getElapsedTime());
frameCounter = 0;
}
}
public static void message(string message) {
#if UNITY_IOS
if (Application.platform == RuntimePlatform.IPhonePlayer) {
switchUserRole(message);
}
#endif
}
double getElapsedTime(){
if (Application.platform == RuntimePlatform.IPhonePlayer)
return IOSgetElapsedTime();
Debug.LogWarning("Wrong platform!");
return 0;
}
// Now make c# methods that can provide the iOS functionality
void addViewToCurrentController()
{
#if UNITY_IOS
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
Debug.Log("Tick View Created Call");
// this calls our native method defined above
addViewToCurrentController();
}
#endif
}
}
When I have build and run the in iPhone device I got this error
Undefined symbols for architecture arm64:
"_addViewToWindow", referenced from:
_FrameworkBridge_addViewToWindow_m0B84013F8B64453FEB515019B6E3C8E24F54C8A5 in Assembly-CSharp.o
(maybe you meant: _FrameworkBridge_addViewToWindow_m0B84013F8B64453FEB515019B6E3C8E24F54C8A5)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have restart Xcode many time, Clean driveData many time. But no luck
或者,如果有人可以帮助我如何在运行时创建自定义UIView。我的意思是,当我的应用构建并运行时,我可以创建一个UIView并通过统一帮助程序类更新该视图颜色。