Unity移动跨平台开发-与Native项目合并

在Unity中完成对项目的开发之后,导出各个平台的工程项目

  注意配置好Unity与各个平台的交互接口。

  Unity调用各个平台的接口:详见 ExitScript.cs

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class ExitScript : MonoBehaviour {
    // This tells unity to look up the function FooPluginFunction
    // inside the static binary
    [DllImport ("__Internal")]
    private static extern float doExitSelector ();

    void doExit () {
#if UNITY_ANDROID
        using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using( AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                jo.Call("doExitSelector", "");
            }
        }
#endif
        
#if UNITY_IPHONE
        doExitSelector();
#endif
    }
}                

 

  Unity接收各个平台的消息调用:详见 SocketListener.cs

using UnityEngine;
using System.Collections;

public class SocketListener : MonoBehaviour {

    void SocketMessage (string message) {
        gameObject.GetComponent<TextMesh>().text = message;
    }
}

// Android调用:UnityPlayer.UnitySendMessage("SocketListener", "SocketMessage", message);
// iOS调用:UnitySendMessage("SocketListener", [@"SocketMessage" UTF8String], [message UTF8String]);

 

与Android平台项目结合

  将Unity项目,或原Android项目作为lib引入。

  更改AndroidManifest.xml,将lib配置加入,修改入口Activity

  其中,在lib中启动主项目中的Activity需要使用隐式Intent

    隐式Intent配置:

<activity android:name="com.shawn.zp.UnityTestProxyActivity" ....>
  <intent-filter>
    <action android:name="unity.app.main" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

    启动代码:

Intent intent = new Intent();
intent.setAction("unity.app.main"); 
startActivity(intent);

与iOS平台项目结合

  详见:http://www.cnblogs.com/shawn-zp/p/3225477.html

posted on 2013-08-01 17:27  shawn.zp  阅读(4650)  评论(4编辑  收藏  举报