juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.13k stars 518 forks source link

Resolve mUnityPlayer reference for native plugins #857

Closed t5ujiri closed 1 year ago

t5ujiri commented 1 year ago

Is your feature request related to a problem? Please describe.

This is about android builds.

Some Native libraries assumes the MainActivity inherits UnityActivity. Those libraries do native function calls in this manner.

            AndroidHelper.Instance.DispatchUiThread(unityActivity =>
            {
                // Create the plugin
                using (AndroidJavaClass cloudServices = new AndroidJavaClass("com.microsoft.CloudServices"))
                {
                    // Initialize the plugin
                    cloudServices.CallStatic("initialize", unityActivity);

                    // Update static variable to say that the plugin has been initialized
                    javaInitialized = true;

                    // Set the task completion source so the CreateSession method can
                    // continue back on the Unity thread.
                    pluginInit.SetResult(true);
                }
            });

Appearently com.microsoft.CloudServices need Unity Player instance. This doesn't work, as in UaaL the MainActivity class originated from flutter and doesn't inherits UnityActivity.

AndroidJavaException: java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class "Ljp/co/gatari/aurisclassiceditormobile/MainActivity;" or its superclasses
E/Unity   (17038): java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class "Ljp/co/gatari/aurisclassiceditormobile/MainActivity;" or its superclasses
E/Unity   (17038):  at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
E/Unity   (17038):  at com.unity3d.player.UnityPlayer.access$500(Unknown Source:0)
E/Unity   (17038):  at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:126)
E/Unity   (17038):  at android.os.Handler.dispatchMessage(Handler.java:102)
E/Unity   (17038):  at android.os.Looper.loopOnce(Looper.java:201)
E/Unity   (17038):  at android.os.Looper.loop(Looper.java:288)
E/Unity   (17038):  at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
E/Unity   (17038):   at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0 
E/Unity   (17038):   at UnityEngine.AndroidJNISafe.GetFieldID (System.IntPtr clazz, System.String name, System.String sig) [0x00000] in <00000000000000000000000000000000>:0 
E/Unity   (17038):   at UnityEngine._Andr

Describe the solution you'd like Please let me know if there is a way to make those native libraries work. Can I set the CustomUnityPlayer (which inherits UnityActivity) reference to Flutter's MainActivity?

timbotimbo commented 1 year ago

This is caused by a 'bugfix' in the newer unity versions.

This was discovered and discussed here: https://github.com/juicycleff/flutter-unity-view-widget/issues/836

That issue also includes a possible fix.