Unity与安卓的信息交互
这次分享的不同于传统的方式AndroidJavaClass("com.unity3d.player.UnityPlayer")
如果是新手的话,请看 交互新手教程
这里讲的是在Unity中调用java代码,或者在unity中传参到java中,在Java代码中运行。
以下是在Java中的代码
public class UnitySdk {
private final String TAG = "sdkcall";
private static final class MInstanceHolder{
static final UnitySdk mInstance = new UnitySdk();
}
public static UnitySdk getInstance() { return MInstanceHolder.mInstance; }
private UnitySdk() {
UnityPlayer mPlayer;
try {
ArrayList<Class<?>> clsList = new ArrayList<Class<?>>();
Class<?> cls = UnityPlayer.currentActivity.getClass();
clsList.add(cls);
cls = cls.getSuperclass();
while (cls != null) {
clsList.add(cls);
cls = cls.getSuperclass();
}
mPlayer = null;
for (Class<?> c : clsList) {
Field[] flds = c.getDeclaredFields();
for (Field fld : flds) {
if (UnityPlayer.class.equals(fld.getType())) {
fld.setAccessible(true);
mPlayer = (UnityPlayer) fld.get(UnityPlayer.currentActivity);
break;
}
}
}
OnInit();
} catch (Throwable t) {
Log.i(TAG, "mPlayer=" + t);
return;
}
Log.i(TAG, "add mPlayer=" + mPlayer);
}
public Activity activity(){return UnityPlayer.currentActivity;}
private void OnInit(){
Log.i(TAG, "OnInit: 初始化成功");
}
public int Getint()
{
return 520;
}
public String Getstring()
{
return "这个是个字符串信息";
}
public void LogIntPram(int _int)
{
Log.i(TAG, "LogIntPram: "+_int);
String str = Integer.toString(_int);
Toast.makeText(activity(),str,Toast.LENGTH_LONG).show();
}
public void LogStringPram(String _str)
{
Log.i(TAG, "LogStringPram: "+_str);
Toast.makeText(activity(),_str,Toast.LENGTH_LONG*2).show();
}
}
构建一个UnitySdk,方便在Unity中调用,而不是使用Unity自带的UnityPlayer类,在构造函数函数中,调用com.unity3d.player.UnityPlayer。将UnitySdk至为单例类。可在函数中对其他的一些方法,或自定义方法调用。
*
Unity中设置
将Android studio中打好的jar包,加载到Unity中,路径为:Plugins/Android/libs
在Unity中,将调用java的方法统一管理到Sdkmgr中。
public class Sdkmgr : MonoBehaviour
{
private static AndroidJavaObject mainObject;
private static Sdkmgr instance = null;
public static Sdkmgr Instance
{
get
{
if (instance == null)
{
instance = GameObject.Find("mgr").AddComponent<Sdkmgr>();
}
return instance;
}
}
private void Awake()
{
#if !UNITY_EDITOR && UNITY_ANDROID
Debug.Log("调用Awake");
var mainClass = new AndroidJavaClass("com.wzt.sdkcall.UnitySdk");
mainObject = mainClass.CallStatic<AndroidJavaObject>("getInstance");
#endif
}
public int GetInt()
{
#if UNITY_EDITOR
return 0;
#elif UNITY_ANDROID
return mainObject.Call<int>("Getint");
#endif
}
public string GetString()
{
#if UNITY_EDITOR
return "";
#elif UNITY_ANDROID
return mainObject.Call<string>("Getstring");
#endif
}
public void LogInt(int num)
{
if (mainObject != null)
{
Debug.Log(num);
mainObject.Call("LogIntPram", num);
}
}
public void LogString(string str)
{
if (mainObject != null)
{
Debug.Log(str);
mainObject.Call("LogStringPram", str);
}
}
}
在业务逻辑中调用代码:
void OnbtnIntClick()
{
txtContent.text = Sdkmgr.Instance.GetInt().ToString();
}
void OnbtnStrClick()
{
txtContent.text = Sdkmgr.Instance.GetString();
}
void OnbtnLogintClick()
{
Sdkmgr.Instance.LogInt(89);
}
void OnbtnLogstrClick()
{
Sdkmgr.Instance.LogString("这是一掉信息的!");
}
效果图
Demo演示