iOS - U3D和iOS交互

一、applicationDidBecomeActive方法替换,更改启动方法和启动页面

****第一种:直接替换启动页面(次方案较少用)****

    - (void)applicationDidBecomeActive:(UIApplication*)application
    {
       ::printf("-> applicationDidBecomeActive()\n");
    
      // if(_snapshotView)
      // {
      ////  [_snapshotView removeFromSuperview];
      ////  _snapshotView = nil;
      // }
    
    
        //自插入代码
        static dispatch_once_t disOnce;
        dispatch_once(&disOnce,^{
            [self performSelector:@selector(startMSUView:) withObject:application afterDelay:0];
        });
        
        if(_unityAppReady)
        {
           if(UnityIsPaused() && _wasPausedExternal == false)
          {
             UnityPause(0);
             UnityWillResume();
          }
          UnitySetPlayerFocus(1);
        }
        else if(!_startUnityScheduled)
        {
       //  _startUnityScheduled = true;
       //  [self performSelector:@selector(startUnity:) withObject:application afterDelay:0];
        }
    
       _didResignActive = false;
    }
    
    //自插入代码
    - (void)startMSUView:(UIApplication *)application{
        MSUController *msu = [[MSUController alloc] init];
        _window.rootViewController = msu;
    }

****第二种:进入U3D页面后,点击自定义控件跳转()****

        1)替换根控制器,进入UnityAppController 找到- (void)startUnity:(UIApplication*)application 方法,进入到 showGameUI 里面,更改代码

          UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:_rootController];
          _rootController.navigationController.navigationBarHidden = YES;
          _window.rootViewController = nav;

        2)U3D做判断点击屏幕具体区域,传个C函数给iOS端进行 控制器跳转,例如:
            注:char 字符串 和 NSString 字符串 相互转换 通过NSUTF8String相关

            extern "C" void SendRoomInfo(const char *a) {
                  NSString *dataString = [NSString stringWithUTF8String:a];
                  NSData *data =[dataString dataUsingEncoding:NSUTF8StringEncoding];
                  NSDictionary *dataDict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  
                  if ((dataString.length==0) {
                      NSLog(@"离开");
                      if (mainVC != nil) {
                         [mainVC.navigationController popViewControllerAnimated:YES];
                         [mainVC.view removeFromSuperview];
                         mainVC =nil;
                      }  else {
                        NSLog(@"开始");
                        if (!mainVC) {
                            mainVC =[[MSUMainController alloc]init];
                            UINavigationController *nav =(UINavigationController*)[[UIApplication sharedApplication]keyWindow].rootViewController;
                            mainVC.gameNumber =tempModel.players.count;
                            [nav pushViewController:mainVC animated:YES];
                          }
                      }
                  }
              }

二,iOS中调用U3D方法

    U3D端:
图片.png
    iOS端:

        extern "C" void _RemoveNativeScene(){

        }
        
        extern "C" void _AddNativeScene(){
            
        }

三、iOS端像U3D发送信息

    主要方法在于:    UnitySendMessage("NativeConnector", "OnServerRequest", [[dict mj_JSONString] UTF8String]);
7F04879E-5ABD-4C3B-9635-9DD19156BB97.png

四.U3D中使用 ShareSDK流程相关(由于U3D不知道引用具体第三方SDK和依赖库,所以根据具体情况自己增删等)

1.报错 ShareSDK 头文件缺失
图片.png
    1)删除引用SDK文件夹(Remove References) ,删除相关依赖库(SDK内涵)
图片.png
    2)点击工程名字,TARGETS - General - Linked Frameworks and Libraries添加红色三个,并在左侧删除报红的三个依赖库
图片.png
    3)实现U3D那边写的C函数,并在XCODE内自己实现
图片.png

四.U3D和iOS横竖屏相关

    1.U3D那边设置成竖屏模式,iOS这边设置成 protrait模式 

    2.在UnityAppController.m 中修改方法 supportedInterfaceOrientationsForWindow , 

        - (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:    (UIWindow*)window
        {
            // UIInterfaceOrientationMaskAll
            // it is the safest way of doing it:
            // - GameCenter and some other services might have portrait-only variant
            //     and will throw exception if portrait is not supported here
            // - When you change allowed orientations if you end up forbidding current one
            //     exception will be thrown
            // Anyway this is intersected with values provided from UIViewController, so we are good
            // 只支持竖屏
            return UIInterfaceOrientationMaskPortrait;
        //    return (1 << UIInterfaceOrientationPortrait) | (1 << UIInterfaceOrientationPortraitUpsideDown)
        //        | (1 << UIInterfaceOrientationLandscapeRight) | (1 << UIInterfaceOrientationLandscapeLeft);
        }

五.内存优化相关

   1. 集成项目模式有两种方式,一种是把iOS集成到U3D工程中,另一种是把U3D集成到iOS中!
   2. U3D给iOS这边的工程,项目缓存有时候能达到400M , 优化步骤有以下几种:
      1).U3D端对图集进行拼凑和去重!
                图集拼凑的意思就是-------U3D懂!
                图集去重的意思就是-------去重!

      2).U3D端创建组件(比如iOS中的tabbar)的时候不需要创建移除--创建移除,而是采用创建隐藏方式!

      3).iOS端控制内存管理,这里就不复述了!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,929评论 25 709
  • 无意中点开韩国电影《芭比》,看了简介,并没有抱多大希望。只是对该影片的题材比较感兴趣,纪实性,具有讽刺意味。 影片...
    小鱼叔阅读 6,017评论 2 4
  • 家的味道,新疆的味道……非常想念你们,我爱你们我的家人^_^^_^^_^
    想念也是幸福阅读 2,047评论 0 0
  • 明天肯定又是一个大晴天。 今天实验还算顺利。而且还遇到一位文化部的退休干部,给我介绍了一名老年音乐家!哈哈哈哈哈哈...
    傅五岁阅读 1,720评论 0 0

友情链接更多精彩内容