Unity调用原生程序方法,定义接口(doTestSelector):
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class TestScript : MonoBehaviour { // This tells unity to look up the function FooPluginFunction // inside the static binary [DllImport ("__Internal")] private static extern float doTestSelector (string info); void doTest () { #if UNITY_ANDROID using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using( AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")) { jo.Call("doTestSelector", "my info"); } } #endif #if UNITY_IPHONE doTestSelector("my info"); #endif }
public void backToUnity () {
// Do somthing...
}
}
原生程序实现接口,例如iOS,打开原生ViewController:
因为需求限制,所以仍然以Objective-C开发
extern "C" float doTestSelector(const char* info) { YourViewController *vc = [[YourViewController alloc] initWithNibName:@"yourViewControllerName" bundle:nil]; [[UnityGetMainWindow() rootViewController] presentViewController:vc animated:YES completion:nil]; return 0.0f; }
关闭原生程序,回到Unity,同样以iOS为例:
- (IBAction)OnClickBack:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; UnitySendMessage("TestScriptGameObject", [@"backToUnity" UTF8String], [@"" UTF8String]); }
时间: 2024-10-10 15:01:27