关于Unity使用Delegate与Java(Android平台)的回调。

161 阅读2分钟

最近正在做Unity微信的账号关联这个功能,碰巧微信没有提供官方的Unity包,所以必须自己给Unity做微信跟安卓的接口。插了各种资料文档所以想做一下总结,避免其他人也走弯路。

虽然最近也做了Line,谷歌,苹果的账号关联,这里不写账号关联这块,先写写如何跟原生代码回调。

先写一下安卓,Unity给安卓平台提供了一个类AndroidJavaProxy。

先说C#方面的

先写用于回调的类,需要注意一下几点

1.继承于AndroidJavaProxy类。这个类专门用回调的。

官方文档 docs.unity3d.com/ScriptRefer…

2.构造函数的base里面写入对应的Java回调用的Interface的名字,需要是包名+接口名。

3.添加回调的方法。这里Callback是回调的方法。对应上上面Interface里面的方法跟参数就行。

class NativeCallback : AndroidJavaProxy
    {
        public NativeCallback() : base("com.unity.intergration.IntergrationInterface") { }

        public void Callback(int result)
        {
            Debug.Log(result);
        }
    }

然后写调用的方法

“com.unity.intergration.UnityIntergration”对应Java里面的类。
PluginMethod对应上面类里面的方法。

  public void CallNative()
        {
            AndroidJavaObject pluginClass = new AndroidJavaObject("com.unity.intergration.UnityIntergration");
            var callback = new NativeCallback();
            callback.onCallback = onComplete;
            pluginClass.Call("PluginMethod", callback);
        }

然后上Java代码

这是在AndroidStudio里面写好,然后拷贝的Unity项目的Plugins/Android下面。Java代码可以直接放在Unity的Plugins/Android/下面直接调用的。

回调用的Interface,对应上面的NativeCallback类

package com.unity.intergration;
public interface IntergrationInterface {    void Callback(int result);}

前面调用的Java类

package com.unity.intergration;

public class UnityIntergration {
    public void PluginMethod(IntergrationInterface intergrationInterface){
        intergrationInterface.Callback(123);
    }
}

调用完成!!!

===========================分割线============================

既然C#跟Java都支持方法重载,试试重载呢,于是

C# 方面

回调Class

class NativeCallback : AndroidJavaProxy
    {
        public NativeCallback() : base("com.unity.intergration.IntergrationInterface") { }

        public void Callback(int result)
        {
            Debug.Log(result);
        }

        public void Callback(string result)
        {
            Debug.Log(result);
        }
    }

调用Method

        public void CallNative1()
        {
            AndroidJavaObject pluginClass = new AndroidJavaObject("com.unity.intergration.UnityIntergration");
            var callback = new NativeCallback();
            callback.onCallback = onComplete;
            pluginClass.Call("PluginMethod1", callback);
        }

Java 

回调类

package com.unity.intergration;

public interface IntergrationInterface {
    void Callback(int result);
    void Callback(String result);
}

被调用Method

public void PluginMethod1(IntergrationInterface intergrationInterface){
    intergrationInterface.Callback("abcdefg");
}

调用完成!!!

============================分割线==============================

再试试直接返回Java的类吧

C#

class NativeCallback : AndroidJavaProxy
    {

        public NativeCallback() : base("com.unity.intergration.IntergrationInterface") { }

        public Action<string> onCallback;

        public void Callback(string result)
        {
            Debug.Log(result);
        }

        public void Callback(int result)
        {
            Debug.Log(result);
        }

        public void Callback(AndroidJavaObject result)
        {
            var str = result.Call<string>("GetStringData");
            Debug.Log(str);
        }
    }




 public void CallNative2()
        {
            AndroidJavaObject pluginClass = new AndroidJavaObject("com.unity.intergration.UnityIntergration");
            var callback = new NativeCallback();
            callback.onCallback = onComplete;
            pluginClass.Call("PluginMethod2", callback);

        }

Java

package com.unity.intergration;

public class NativeParameter {
    public int a;
    public String b;
    public int GetA(){
        return a;
    }
    public String GetB(){
        return b;
    }
    public String GetStringData(){
        return "a:" + a + ";b:" + b;
    }
}

package com.unity.intergration;

public interface IntergrationInterface {
    void Callback(int result);
    void Callback(String result);
    void Callback(NativeParameter nativeParameter);
}


package com.unity.intergration;

public class UnityIntergration {
    public void PluginMethod(IntergrationInterface intergrationInterface){
        intergrationInterface.Callback(123);
    }

    public void PluginMethod1(IntergrationInterface intergrationInterface){
        intergrationInterface.Callback("abcdefg");
    }

    public void PluginMethod2(IntergrationInterface intergrationInterface){
        NativeParameter nativeParameter = new NativeParameter();
        nativeParameter.a = 123;
        nativeParameter.b = "abcdefg";
        intergrationInterface.Callback(nativeParameter);
    }
}

完成!!!

GitHub的代码:

github.com/yingyugang/…

辛辛苦苦码了这多,走过路过不要错过,各位大神给个赞吧 :)。。。。