最近年前談工作,順便把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]; } @end IMPL_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 UnityViewController static 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之間的交互。