unity 与oc、java交互
1.unity cs 书写基类PlatformFactory.cs
using System.Collections;using System.Collections.Generic;using UnityEngine;using System;public class PlatformFactory { protected static PlatformFactory _Instance = null; public static PlatformFactory Instance { get { if (null == _Instance) { _Instance = CreatePlatformInterface(); } return _Instance; } } static PlatformFactory CreatePlatformInterface() {#if UNITY_IOS && !UNITY_EDITOR _Instance = new IOSPlatform();#elif UNITY_ANDROID && !UNITY_EDITOR _Instance = new AndroidPlatform();#else _Instance = new EditorPlatform();#endif return _Instance; } public virtual void initSDK() { } public virtual void loadInterAd() { } public virtual void showInterAd() { } public virtual void loadRewardedVideo() { } public virtual void showRewardedVideo(string tag,Action<string> succeed = null,Action<string> fail = null) { } /// <summary> /// Tea平台打点 /// </summary> /// <param name="custom"></param> /// <param name="dic"></param> public virtual void TeaAnalystic(string custom, Dictionary<string, string> dic) { } public virtual void GameQuit() { }}
Android AndroidPlatform.cs
#if UNITY_ANDROIDusing System.Collections;using System.Collections.Generic;using System.Runtime.InteropServices;using UnityEngine;using System;public class AndroidPlatform : PlatformFactory{ public override void initSDK() { AndroidPlatformWrapper.Instance.initSDK(); } public override void showRewardedVideo(string tag, Action<string> succeed = null, Action<string> fail = null) { Debug.Log("AndroidPlatform showRewardedVideo"); AndroidPlatformWrapper.Instance.showRewardedVideo(tag); } public override void showInterAd() { Debug.Log("AndroidPlatform showInterAd"); AndroidPlatformWrapper.Instance.showInterAd(); } public override void TeaAnalystic(string custom, Dictionary<string, string> dic) { AndroidPlatformWrapper.Instance.TeaAnalystic(custom,dic); } public override void GameQuit() { AndroidPlatformWrapper.Instance.GameQuit(); }}#endif
AndroidPlatformWrapper.cs
#if UNITY_ANDROIDusing System.Collections;using System.Collections.Generic;using UnityEngine;using AOT;using System;public class AndroidPlatformWrapper : MonoBehaviour { static AndroidPlatformWrapper _instance; AndroidJavaObject jo; public delegate void CallbackDelegate(string str); private Action<string> succeed; private Action<string> fail; public static AndroidPlatformWrapper Instance { get { if (_instance == null) { GameObject go = new GameObject("AndroidPlatformWrapper"); go.AddComponent<AndroidPlatformWrapper>(); _instance = go.GetComponent<AndroidPlatformWrapper>(); DontDestroyOnLoad(go); } return _instance; } } public void initSDK() { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); //jo = new AndroidJavaObject("com.hw.GamePlayer"); jo = new AndroidJavaObject("com.hellowd.HwGamePlayer"); jo.Call("initHwSDK", currentActivity, Config_Android.SERVER_URL); } /// <summary> /// 播放视频 /// </summary> public void showRewardedVideo(string tag, Action<string> succeed = null, Action<string> fail = null) { this.succeed = succeed; Debug.Log("AndroidPlatformWrapper showRewardedVideo"); object[] paramArray = new object; paramArray = tag; paramArray = "PlatformCallback_FinishRewardAd"; paramArray = "PlatformCallback_FailedRewardAd"; if(jo != null) jo.Call("showHwRewardAd", paramArray); } /// <summary> /// 视频播放成功 /// </summary> /// <param name="jsonStr"></param> void PlatformCallback_FinishRewardAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FinishRewardAd:"+jsonStr); IAP_Ad.ADPlaySuccess(); } /// <summary> /// 视频播放失败 /// </summary> /// <param name="jsonStr"></param> void PlatformCallback_FailedRewardAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FailedRewardAd:"+jsonStr); IAP_Ad.ADPlayFailed(); } public void showInterAd() { Debug.Log("AndroidPlatformWrapper showInterAd"); object[] paramArray = new object; paramArray = "showInterAd"; paramArray = "PlatformCallback_FinishInterAd"; paramArray = "PlatformCallback_FailedInterAd"; if (jo != null) jo.Call("showHwInterAd", paramArray); } void PlatformCallback_FinishInterAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FinishInterAd:"+jsonStr); } void PlatformCallback_FailedInterAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FailedInterAd:" + jsonStr); } public void TeaAnalystic(string custom, Dictionary<string, string> dic) { Debug.Log("AndroidPlatformWrapper TeaAnalystic:" + custom); string jsonStr = FullSerializerAPI.Serialize(typeof(Dictionary<string, string>), dic); object[] paramArray = new object; paramArray = custom; paramArray = jsonStr; AndroidJavaObject hwjo = new AndroidJavaObject("com.hw.GamePlayer"); hwjo.Call("TeaAnalystic", paramArray); } /// <summary> /// 退出游戏 /// </summary> public void GameQuit() { if (jo != null) jo.Call("GameQuit"); }}#endif
ios IOSPlatform.cs
#if UNITY_IOS && !UNITY_EDITORusing System.Collections;using System.Collections.Generic;using UnityEngine;public class IOSPlatform : PlatformFactory{ public override void initSDK() { IOSPlatformWrapper.initSDK(); } public override void showRewardedVideo(string tag, Action<string> succeed = null, Action<string> fail = null) { Debug.Log("AndroidPlatform showRewardedVideo"); } public override void showInterAd() { Debug.Log("AndroidPlatform showInterAd"); } public override void TeaAnalystic(string custom, Dictionary<string, string> dic) { } public override void GameQuit() { }}#endif
IOSPlatformWrapper.cs
using AOT;using System.Collections;#if UNITY_IOS && !UNITY_EDITORusing System.Collections.Generic;using System.Runtime.InteropServices;using UnityEngine;public class IOSPlatformWrapper{ public delegate void CallbackDelegate(string str); private static extern void initHwAds( string jsonStr, CallbackDelegate callBack = null); public static void initSDK() { Debug.Log("initSDK"); initHwAds("121212", initSDKCallback); } public static void initSDKCallback(string str) { Debug.Log("initSDKCallback"); } public static void showRewardedVideo(string tag, Action<string> succeed = null, Action<string> fail = null) { } /// <summary> /// 视频播放成功 /// </summary> /// <param name="jsonStr"></param> static void PlatformCallback_FinishRewardAd(string jsonStr) { } /// <summary> /// 视频播放失败 /// </summary> /// <param name="jsonStr"></param> static void PlatformCallback_FailedRewardAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FailedRewardAd:"+jsonStr); } public static void showInterAd() { Debug.Log("AndroidPlatformWrapper showInterAd"); } static void PlatformCallback_FinishInterAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FinishInterAd:"+jsonStr); } static void PlatformCallback_FailedInterAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FailedInterAd:" + jsonStr); } public static void TeaAnalystic(string custom, Dictionary<string, string> dic) { Debug.Log("AndroidPlatformWrapper TeaAnalystic:" + custom); } /// <summary> /// 退出游戏 /// </summary> public static void GameQuit() { if (jo != null) jo.Call("GameQuit"); }}#endifFrom U3D to JAVA
object[] paramArray = new object; paramArray = "showInterAd"; paramArray = "PlatformCallback_FinishInterAd"; paramArray = "PlatformCallback_FailedInterAd"; if (jo != null) jo.Call("showHwInterAd", paramArray);
image.png
From JAVA to U3D
UnityPlayer.UnitySendMessage("AndroidPlatformWrapper",arg2,arg1);
4.From OC to U3D
1)分为两种办法:一种是通过UnitySendMessage方法来调用Unity所定义的方法。另一种方法则是通过入口参数,传入一个U3D的非托管方法,然后调用该方法即可。两种方式的对比如下:
UnitySendMessage方式非托管方法方式接口声明固定,只能是void method(string message)。接口灵活,可以为任意接口。不能带有返回值可以带返回值必须要挂载到对象后才能调用。可以不用挂载对象,但需要通过接口传入该调用方法
2)UnitySendMessage
oc书写如下:
UnitySendMessage("Main Camera", "callback", resultStr.UTF8String);
3)非托管方式 cs代码如下
private static extern void initHwAds( string jsonStr, CallbackDelegate callBack = null); public static void initSDK() { Debug.Log("initSDK"); initHwAds("121212", initSDKCallback); }
oc代码如下
typedef void (*CallbackDelegate)(const char *object); CallbackDelegate call; + (void)ToUnity:(char *) str{ call(str); } void initHwAds( char *str ,CallbackDelegate callback){ NSLog(@"initHwAds complete111111 %s",str); //callback(str); call = callback; ; }
5.DllImport 介绍
Unity本身对第三方C/C++/Objective-C编写的类库具有广泛的支持。不过不能使用Using的方式来引用,需要使用DllImport的方式来引用,一下是官方文档内容
Unity has extensive support for native plug-ins, which are libraries of native code written in C, C++, Objective-C, etc. Plug-ins allow your game code (written in Javascript or C#) to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ game code.(
Unity对本地插件有广泛的支持,这些插件是用C、c++、Objective-C等语言编写的本地代码库。插件允许游戏代码(用Javascript或c#编写)从这些库调用函数。这个特性允许Unity集成中间件库或现有的C/ c++游戏代码。)
6.MonoPInvokeCallback标签声明
声明一个静态方法,并使用MonoPInvokeCallback特性来标记为回调方法,目的是让iOS中调用该方法时可以转换为对应的托管方法。如:
static void PlatformCallback_FinishInterAd(string jsonStr) { Debug.Log("AndroidPlatformWrapper PlatformCallback_FinishInterAd:"+jsonStr); }
注意:MonoPInvokeCallback特性参数是上一步中定义的非托管delegate。方法的声明一定要与delegate定义一致,并且必须为static进行修饰(iOS不支持非静态方法回调),否则会导致异常。
7.类型传递
对于基础类型数据(如:int、double、string等)是可以直接从U3D中传递给iOS的。具体对应关系如下表所示
image.png
页:
[1]