標籤:
之前完全沒接觸過unity,因為現在公司需要在unity的代碼上做二次開發,所以我要能夠隨意調用unity的方法,在網上搜了很多種方法都沒什麼用,全是幾年前的版本,我只是在別人原來的方法上更新下,寫一個最新版本的互動例子。 我用的是unity5.3和Xcode7.3
建立一個按鈕直接跳轉到unity項目的方法網上可以搜到,我就不做描述了
這裡只說怎樣隨意調用unity裡單個的方法
建立unity情境這個可以通過
雨松momo大神的方法來弄 http://www.xuanyusong.com/archives/517
我的方法就是在他的基礎上更新了一下,主要是他那個版本是很老的,不適用現在的版本
匯出成iOS後我建立了一個類叫MyView
MyView.h檔案
#import <UIKit/UIKit.h>@interface MyView : NSObject+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller;+(void)CreateTitle:(UIViewController *)controller;+(void)LeftButtonPressed;+(void)RightButtonPressed;+(void)UpButtonPressed;+(void)DownButtonPressed;+(void)Init:(UIViewController *)controller;@end
MyView.mm檔案
#import "MyView.h"@interface MyView ()@end@implementation MyView//建立按鈕+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller{ UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //設定按鈕範圍 btn.frame = rect; //設定按鈕顯示內容 [btn setTitle:title forState:UIControlStateNormal]; //設定按鈕改變後綁定的回應程式法 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; //加入View中 [controller.view addSubview:btn];}//建立標籤+(void)CreateTitle:(UIViewController *)controller{ //建立label視圖 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; //設定顯示內容 label.text = @"旋轉控制"; //設定背景顏色 label.backgroundColor = [UIColor blueColor]; //設定文字顏色 label.textColor = [UIColor whiteColor]; //設定顯示位置置中 label.textAlignment = NSTextAlignmentCenter; //設定字型大小 label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20]; [controller.view addSubview:label];}//向左按鈕+(void)LeftButtonPressed{ UnitySendMessage("Cube", "MoveLeft","");}//向右按鈕+(void)RightButtonPressed{ UnitySendMessage("Cube", "MoveRight","");}//向上按鈕+(void)UpButtonPressed{ UnitySendMessage("Cube", "MoveUp","");}//向下按鈕+(void)DownButtonPressed{ UnitySendMessage("Cube", "MoveDown","");}+(void)Init:(UIViewController *)controller{ [self CreateTitle:controller]; [self CreateButton:@"左旋轉" rect:CGRectMake(0, 50, 100, 40) action:@selector(LeftButtonPressed) controller:controller]; [self CreateButton:@"右旋轉" rect:CGRectMake(0, 100, 100, 40) action:@selector(RightButtonPressed) controller:controller]; [self CreateButton:@"上旋轉" rect:CGRectMake(0, 150, 100, 40) action:@selector(UpButtonPressed) controller:controller]; [self CreateButton:@"下旋轉" rect:CGRectMake(0, 200, 100, 40) action:@selector(DownButtonPressed) controller:controller];}@end
然後進入UnityAppController.mm裡
在UnityAppController.mm裡面匯入標頭檔
找到這一行
在這個方法裡的return YES 之前加上這段代碼
然後運行就出現了這個介面
點擊按鈕後這個模型會旋轉
好了,就是這些
iOS和unity的互動問題