Unity 3D 添加ios视图
Untiy导出的ios都是单视图的程序,如果要在这个视图上添加其他视图的方法:
void) applicationDidFinishLaunching:(UIApplication*)application
{
printf_console("-> applicationDidFinishLaunching()\n");
if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[self startUnity:application];
//添加自己的视图,记得在release掉myView
myView=[[myViewController alloc]init];
[application.keyWindow addSubview:myView.view];
}
- ({
printf_console("-> applicationDidFinishLaunching()\n");
if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[self startUnity:application];
//添加自己的视图,记得在release掉myView
myView=[[myViewController alloc]init];
[application.keyWindow addSubview:myView.view];
}
如果要在myView中执行其他操作,就要使用通知方法,因为你没有办法把myView设置为代理类。
void)viewDidLoad {
//添加接收通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addbtns:) name:@"showbtns" object:nil];
[self.view setFrame:CGRectMake(0, 0, 0, 0)];
[super viewDidLoad];
}
-(void)addbtns:(NSNotification *)notification
{
play=YES;
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
[btn setTitle:@"滚动" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor brownColor]];
[btn addTarget:self action:@selector(StartTimer) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[self.view setFrame:CGRectMake(0, 0, 300, 50)];
}
- (//添加接收通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addbtns:) name:@"showbtns" object:nil];
[self.view setFrame:CGRectMake(0, 0, 0, 0)];
[super viewDidLoad];
}
-(void)addbtns:(NSNotification *)notification
{
play=YES;
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
[btn setTitle:@"滚动" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor brownColor]];
[btn addTarget:self action:@selector(StartTimer) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[self.view setFrame:CGRectMake(0, 0, 300, 50)];
}
然后在程序其他地方发出通知,一般是untiy接口函数发出通知,因为untiy导出的是单视图,一开始根本没有其他视图或程序发出通知
extern "C" {
void IOSExcuteFucntion(const char* functionName,const char* arg)
{
if (delegateObject == nil)
delegateObject = [[UnityExcuteDelegate alloc] init];
NSString *funnameString=[CreateNSString(functionName) stringByAppendingFormat:@":"];
NSString *funartString=CreateNSString(arg);
[delegateObject performSelector:NSSelectorFromString(funnameString) withObject:funartString];
//[delegateObject performSelector:(SEL)(method)];
//objc_msgSend(delegateObject, (SEL)method);
}
}
@implementation UnityExcuteDelegate
- (id)init
{
self = [super init];
return self;
}
-(void)dealloc
{
[super dealloc];
}
-(void)test_function:(NSString *)str
{
//发出通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"showbtns" object:self userInfo:nil];
}
@end
void IOSExcuteFucntion(const char* functionName,const char* arg)
{
if (delegateObject == nil)
delegateObject = [[UnityExcuteDelegate alloc] init];
NSString *funnameString=[CreateNSString(functionName) stringByAppendingFormat:@":"];
NSString *funartString=CreateNSString(arg);
[delegateObject performSelector:NSSelectorFromString(funnameString) withObject:funartString];
//[delegateObject performSelector:(SEL)(method)];
//objc_msgSend(delegateObject, (SEL)method);
}
}
@implementation UnityExcuteDelegate
- (id)init
{
self = [super init];
return self;
}
-(void)dealloc
{
[super dealloc];
}
-(void)test_function:(NSString *)str
{
//发出通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"showbtns" object:self userInfo:nil];
}
@end
分类:
ios
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架