unity调用安卓各种方法集合

简便调用安卓端的方法

public class SDKInterfaceAndroid
{

    private AndroidJavaObject jo;
    private static ASCSDKInterface _instance;
    public static ASCSDKInterface Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new SDKInterfaceDefault();
            }
            return _instance;
        }
    }
    public SDKInterfaceAndroid()
    {
        using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        }
    }

    public T SDKCall<T>(string method, params object[] param)
    {
        try
        {
            return jo.Call<T>(method, param);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
        return default(T);
    }

    public void SDKCall(string method, params object[] param)
    {
        try
        {
            jo.Call(method, param);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }
}

直接使用上面的代码就能方便的调用有返回值或者无返回值的安卓方法
例如:

//调用无返回值方法
SDKInterfaceAndroid.Instance.SDKCall("你的安卓方法名");
//调用返回值为bool类型
bool isOn=SDKInterfaceAndroid.Instance.SDKCall<bool>("getbool");
//调用返回值为int类型
int Num=SDKInterfaceAndroid.Instance.SDKCall<int>("getInt");
//调用方法并传入参数
SDKInterfaceAndroid.Instance.SDKCall("安卓方法名","要传入的字符类型参数");

猜你喜欢

转载自blog.csdn.net/qq_18293987/article/details/80215729