首页 > 移动平台 > 详细

Unity调用原生(iOS, Android)方法

时间:2015-09-09 22:50:30      阅读:411      评论:0      收藏:0      [点我收藏+]

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]);
}

 

Unity调用原生(iOS, Android)方法

原文:http://www.cnblogs.com/shawn-zp/p/4796132.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!