博客> iOS - U3D和iOS交互
iOS - U3D和iOS交互
2017-12-13 17:37 评论:0 阅读:261 OS_JJ
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端:

 2B4B8828048385CA8EEC5766C7A294DB.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 头文件缺失

 F25A227F-DA3F-48B4-8E5D-678113B6B3BA.png

    1)删除引用SDK文件夹(Remove References) ,删除相关依赖库(SDK内涵)

 D38BB719-0A9D-4902-AF89-36A0F57B0578.png

    2)点击工程名字,TARGETS - General - Linked Frameworks and Libraries添加红色三个,并在左侧删除报红的三个依赖库

 AD089978-8A80-4FF7-99B1-C1E8DDF08FFC.png

    3)实现U3D那边写的C函数,并在XCODE内自己实现

 C9CE0AB4-982B-431E-BB5B-B569A8D4A9EF.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 <&lt; UIInterfaceOrientationPortrait) | (1 <&lt; UIInterfaceOrientationPortraitUpsideDown)
        //        | (1 <&lt; UIInterfaceOrientationLandscapeRight) | (1 <&lt; UIInterfaceOrientationLandscapeLeft);
        }

五.内存优化相关

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

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

         3.iOS端控制内存管理,这里就不复述了!
收藏
0
sina weixin mail 回到顶部