2.選擇要匯出的平臺,這裡選擇iOS,然後點選右下角的"Switch Plateform"按鈕,然後點選上面的「Add Open Scenes」新增場景
3.點選「Build」,匯出iOS專案
__attribute__ ((visibility("default"))) @interface FrameworkLibAPI : NSObject // call it any time after UnityFrameworkLoad to set object implementing NativeCallsProtocol methods +(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi; @end
@implementation FrameworkLibAPI id<NativeCallsProtocol> api = NULL; +(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi { api = aApi; } @end
extern "C" { void showHostMainWindow(const char* color) { return [api showHostMainWindow:[NSString stringWithUTF8String:color]]; } }
- (void)initUnity { [self setUfw: UnityFrameworkLoad()]; // Set UnityFramework target for Unity-iPhone/Data folder to make Data part of a UnityFramework.framework and uncomment call to setDataBundleId // ODR is not supported in this case, ( if you need embedded and ODR you need to copy data ) [[self ufw] setDataBundleId: "com.unity3d.framework"]; [[self ufw] registerFrameworkListener: self]; [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self]; }
#if UNITY_IOS || UNITY_TVOS public class NativeAPI { [DllImport("__Internal")] public static extern void showHostMainWindow(string lastStringColor); } #endif
void OnGUI() { GUIStyle style = new GUIStyle("button"); style.fontSize = 45; if (GUI.Button(new Rect(10, 300, 600, 100), "Show Main With Color", style)) showHostMainWindow(); } void showHostMainWindow() { #if UNITY_ANDROID try { AndroidJavaClass jc = new AndroidJavaClass("com.unity.mynativeapp.SharedClass"); jc.CallStatic("showMainActivity", lastStringColor); } catch(Exception e) { appendToText("Exception during showHostMainWindow"); appendToText(e.Message); } #elif UNITY_IOS || UNITY_TVOS NativeAPI.showHostMainWindow(lastStringColor); #endif }
self.btnSendMsg = [UIButton buttonWithType: UIButtonTypeSystem]; [self.btnSendMsg setTitle: @"Send Msg" forState: UIControlStateNormal]; [self.btnSendMsg addTarget: self action: @selector(sendMsgToUnity) forControlEvents: UIControlEventPrimaryActionTriggered]; - (void)sendMsgToUnity { [[self ufw] sendMessageToGOWithName: "Cube" functionName: "ChangeColor" message: "yellow"]; } /* goName: 場景中的遊戲物體GameObject name: 這個遊戲物體掛載的指令碼中的一個方法 msg: 引數 */ - (void)sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg { UnitySendMessage(goName, name, msg); } void UnitySendMessage(const char* obj, const char* method, const char* msg);
string lastStringColor = ""; void ChangeColor(string newColor) { appendToText( "Changing Color to " + newColor ); lastStringColor = newColor; if (newColor == "red") GetComponent<Renderer>().material.color = Color.red; else if (newColor == "blue") GetComponent<Renderer>().material.color = Color.blue; else if (newColor == "yellow") GetComponent<Renderer>().material.color = Color.yellow; else GetComponent<Renderer>().material.color = Color.black; }