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

转载于:https://www.cnblogs.com/shawn-zp/p/3230536.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值