Unity calls jar package or aar package

I have been doing VR cloud conferences recently, and I have used the service of Huawei cloud conference. Huawei cloud conference service does not provide the SDK of the Unity version, but the native sdk of the Android version, so I can only encapsulate a layer in Unity to call Android. interface. In the process, some problems were encountered, so let's make a summary.

1. Get the current Application or the current Activity

            using AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            using AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            using AndroidJavaObject app = currentActivity.Call<AndroidJavaObject>("getApplication");

2. The function of the Android library returns ArrayList, how to convert it in c#? Generally, first return AndroidJavaObject, then convert ArrayList into AndroidJavaObject[], and finally traverse the loop in C# to convert each unit in the array into your own structure.

        public static AttendeeInfo ToAttendeeInfo(AndroidJavaObject attendeeObject)
        {
            AttendeeInfo attendeeInfo = new AttendeeInfo();

            attendeeInfo.setName(attendeeObject.Call<string>("getName"));
        }

        public List<AttendeeInfo> GetVideoAttendeeList()
        {
            using AndroidJavaObject attendeeListObj = IConfState.Call<AndroidJavaObject>("getVideoAttendeeList");
            if (attendeeListObj == null)
                return null;


            AndroidJavaObject[] objArray = attendeeListObj.Call<AndroidJavaObject[]>("toArray");

            List<AttendeeInfo> attendeeInfos = new List<AttendeeInfo>();
            foreach (var obj in objArray)
            {
                attendeeInfos.Add(AttendeeInfo.ToAttendeeInfo(obj));
            }

            return attendeeInfos;
        }

3. In the Android interface, there are ArrayList parameters in the parameters. How to do it?

First save the AndroidJavaObject of the ArrayList, then convert each item of C# into each item of the Android interface array, and call the add interface to add it one by one.

        public SDKERR GeneralWatch(List<GeneralWatchItemParamEx> watchs)
        {
            using AndroidJavaObject watchList = new AndroidJavaObject("java.util.ArrayList");
            foreach (var watch in watchs)
            {
                watchList.Call<bool>("add", watch.ToXRAndroidJavaObject());
            }

            return SDKERR.ToSDKERR(IConfCtrl.Call<AndroidJavaObject>("generalWatch", watchList));
        }

4. There is a callback function in the parameters. How to pass parameters in C#?

There is a class in C# that specifies the inheritance of AndroidJavaProxy. It should be noted that the callback interface can only be the interface Interface in Android, and then javaInterface is the path of android. Also every function in the callback interface must be implemented. The basic type can be directly matched. If it is other structures, AndroidJavaObject must be used.

    public class SdkCallback: AndroidJavaProxy
    {
        public readonly static string DEFALUT_JAVAINTERFACE = "com.huawei.hwmsdk.common.SdkCallback";
        public event Action<AndroidJavaObject> OnSuccess;
        public event Action OnFailed;

        public SdkCallback(string javaInterface) : base(javaInterface)
        {
        }

        public SdkCallback() : base(DEFALUT_JAVAINTERFACE)
        {
        }

        public void onFailed(AndroidJavaObject sdkerr)
        {
            SDKERR sDKERR = SDKERR.ToSDKERR(sdkerr);
            Debug.LogError("SdkCallback onFailed: " + sDKERR.ToString());

            OnFailed?.Invoke();
        }

        public void onSuccess(AndroidJavaObject var1)
        {
            OnSuccess?.Invoke(var1);
        }
    }

5. How to call the enum type in the Android interface?

AndroidJavaClass statuCodeClass = new AndroidJavaClass("com.huawei.huaweicloud.xrmeeting.rpc.core.RpcStatusCode");
AndroidJavaObject statusCode = statuCodeClass.GetStatic<AndroidJavaObject>("OK");

Guess you like

Origin blog.csdn.net/grace_yi/article/details/122668978