iOS - U3D和iOS交互

一、applicationDidBecomeActive方法替換,更改啟動(dòng)方法和啟動(dòng)頁面

****第一種:直接替換啟動(dòng)頁面(次方案較少用)****

    - (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;
    }

****第二種:進(jìn)入U(xiǎn)3D頁面后,點(diǎn)擊自定義控件跳轉(zhuǎn)()****

        1)替換根控制器,進(jìn)入U(xiǎn)nityAppController 找到- (void)startUnity:(UIApplication*)application 方法,進(jìn)入到 showGameUI 里面,更改代碼

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

        2)U3D做判斷點(diǎn)擊屏幕具體區(qū)域,傳個(gè)C函數(shù)給iOS端進(jìn)行 控制器跳轉(zhuǎn),例如:
            注:char 字符串 和 NSString 字符串 相互轉(zhuǎn)換 通過NSUTF8String相關(guān)

            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中調(diào)用U3D方法

    U3D端:
圖片.png
    iOS端:

        extern "C" void _RemoveNativeScene(){

        }
        
        extern "C" void _AddNativeScene(){
            
        }

三、iOS端像U3D發(fā)送信息

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

四.U3D中使用 ShareSDK流程相關(guān)(由于U3D不知道引用具體第三方SDK和依賴庫,所以根據(jù)具體情況自己增刪等)

1.報(bào)錯(cuò) ShareSDK 頭文件缺失
圖片.png
    1)刪除引用SDK文件夾(Remove References) ,刪除相關(guān)依賴庫(SDK內(nèi)涵)
圖片.png
    2)點(diǎn)擊工程名字,TARGETS - General - Linked Frameworks and Libraries添加紅色三個(gè),并在左側(cè)刪除報(bào)紅的三個(gè)依賴庫
圖片.png
    3)實(shí)現(xiàn)U3D那邊寫的C函數(shù),并在XCODE內(nèi)自己實(shí)現(xiàn)
圖片.png

四.U3D和iOS橫豎屏相關(guān)

    1.U3D那邊設(shè)置成豎屏模式,iOS這邊設(shè)置成 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);
        }

五.內(nèi)存優(yōu)化相關(guān)

   1. 集成項(xiàng)目模式有兩種方式,一種是把iOS集成到U3D工程中,另一種是把U3D集成到iOS中!
   2. U3D給iOS這邊的工程,項(xiàng)目緩存有時(shí)候能達(dá)到400M , 優(yōu)化步驟有以下幾種:
      1).U3D端對圖集進(jìn)行拼湊和去重!
                圖集拼湊的意思就是-------U3D懂!
                圖集去重的意思就是-------去重!

      2).U3D端創(chuàng)建組件(比如iOS中的tabbar)的時(shí)候不需要?jiǎng)?chuàng)建移除--創(chuàng)建移除,而是采用創(chuàng)建隱藏方式!

      3).iOS端控制內(nèi)存管理,這里就不復(fù)述了!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,829評論 25 708
  • 無意中點(diǎn)開韓國電影《芭比》,看了簡介,并沒有抱多大希望。只是對該影片的題材比較感興趣,紀(jì)實(shí)性,具有諷刺意味。 影片...
    小魚叔閱讀 1,965評論 2 4
  • 家的味道,新疆的味道……非常想念你們,我愛你們我的家人^_^^_^^_^
    想念也是幸福閱讀 296評論 0 0
  • 明天肯定又是一個(gè)大晴天。 今天實(shí)驗(yàn)還算順利。而且還遇到一位文化部的退休干部,給我介紹了一名老年音樂家!哈哈哈哈哈哈...
    傅五歲閱讀 250評論 0 0