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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM