Unity-IOS互動整理

來源:互聯網
上載者:User

標籤:sources   螢幕   sse   ase   接受   ant   plugins   des   end   

最近年前談工作,順便把IOS互動的內容整理一下隨筆。

我這裡平時用的跟IOS互動的方式,主要分為三部分

1、Asset/Plugins/IOS下面有一個繼承自UnityAppController的AppController的類,檔案名稱是AppController.mm

2、在Xcode裡面跟Unity互動的類UnityViewController.h和UnityViewController.m

3、在Unity裡面跟IOS互動的類

需要注意的是:

Unity調用IOS的方法,是不能直接調用objective-c的方法的,而是調用C語言的方法。

代碼如下:

1、

AppController.mm

這個檔案丟到Unity的Asset/Plugins/IOS下

#import "UnityAppController.h" //匯入Unity的UnityAppController.h#import "UnityViewController.h"//匯入我們自己寫介面的類的.h檔案@interface AppController : UnityAppController@property(nonatomic,strong) UINavigationController *naVC;-(void) createUI;@end@implementation AppController-(void) createUI{    _rootController = [[UIViewController alloc]init];    _rootView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];    _rootController.view = _rootView;        UnityViewController *vc = [[UnityViewController alloc]init];    self.naVC =[[UINavigationController alloc]initWithRootViewController:vc];        [_rootView addSubview:self.naVC.view];        _window.rootViewController = _rootController;    [_window bringSubviewToFront:_rootView];    [_window makeKeyAndVisible];}@endIMPL_APP_CONTROLLER_SUBCLASS(AppController); //這是一個固定寫法,因為這個代碼,所以啟動介面是從這裡啟動

 

2、

UnityViewController.h

這個檔案是放在Xcode工程目錄下的

#import "UnityAppController.h"@interface UnityViewController : UIViewController@property (nonatomic,strong) NSString *str;@end

 

UnityViewController.m 

這個檔案是放在Xcode工程目錄下的

#import "UnityViewController.h"#import "UnityAppController+ViewHandling.h"#import <UI/UnityView.h>@interface UnityViewController ()@end@implementation UnityViewControllerstatic id object;- (void)viewDidLoad {        [super viewDidLoad];    // self.view.backgroundColor = [UIColor blueColor];    object = self.navigationController;    self.str = @"我是威少,我成功調用了ios的介面";    [self.view addSubview:GetAppController().unityView];    GetAppController().unityView.frame = self.view.frame;        // Do any additional setup after loading the view.}//視圖已經完全過渡到螢幕上時調用的方法-(void)viewDidAppear:(BOOL)animated{        UnitySendMessage("ReceiveiOSMessage", "ReceiveiOSInputMessage", [self.str UTF8String]);    //1.參數1接受訊息所掛的指令碼的物體名稱    //2.參數2接受訊息的方法名    //3.參數3發送的字串    }//返回字串類型,注意字串類型在返回的時候一定要加上strdup,
//Unity可以調用這個方法const char * _BackStringParm(const char *parm){ const char *parm1 = strcat(strdup(parm),"dddd"); return strdup(parm1);}//C語言返回int類型,
//Unity可以調用這個方法int _BackIntParm(int parm){ return parm;}//c語言返回void,
//Unity可以調用這個方法void _BackBtnPress(){ UnitySendMessage("ReceiveiOSMessage", "ReceiveiOSInputMessage", "點擊我返回"); //[object popViewControllerAnimated:YES];}-(void)viewWillAppear:(BOOL)animated{ self.navigationController.navigationBar.hidden = YES;}-(void)viewDidDisappear:(BOOL)animated{ self.navigationController.navigationBar.hidden = NO;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */@end

 

3、

unityscript.cs

[DllImport ("__Internal")]

public static extern void _BackBtnPress();

[DllImport ("__Internal")]

public static extern int _BackIntParm(int x);

[DllImport ("__Internal")]

public static extern string _BackStringParm(string str);

以上三個方法就是從ios那邊映射調用,有沒有發現這個和C#程式調用C語言的dll庫的方式類似?

噢不,不是類似,就是一樣。因為我們從C#這邊調用的就是C語言寫的方法,而不是OC寫的方法。

 

public void ReceiveiOSInputMessage(string str)

{

  Debug.Log(str);

}

以上的這個方法,是IOS那邊使用UnitySendMessage方法調用的,

例如 UnitySendMessage("ReceiveiOSMessage", "ReceiveiOSInputMessage", "點擊我返回");

參數1是GameObject的名字,參數2是GameObject下的指令碼裡的方法名,參數3是返回的字串。

 

最後,有沒有發現這一套下來其實和Unity-Android其實相似?

Unity-IOS:Unity使用的是UnityAppController,實質就是UIViewController之間的互動。

Unity-Android:Unity使用的是UnityPlayerActivity,實質就是Activity之間的互動。

 

Unity-IOS互動整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.