flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

Anr fix "com.unity3d.player.UnityPlayer.nativeRender"

Open RGTechPro opened this issue 10 months ago โ€ข 1 comments

Description

This PR introduces a fix for the ANR (Application Not Responding) issue: com.unity3d.player.UnityPlayer.nativeRender in the flutter_unity_widget package. The ANR issue was caused by attempting to send Unity messages when the Unity player was not properly loaded or initialized.

Changes Made:

  • Added a safety check before calling UnityPlayer.UnitySendMessage() to ensure that unityPlayer is not null and unityLoaded is true.

Modified Function:

fun postMessage(gameObject: String, methodName: String, message: String) {
    if (unityPlayer == null || !unityLoaded) {
        return
    }
    UnityPlayer.UnitySendMessage(gameObject, methodName, message)
}

Type of Change

Motivation and Context

The ANR issue was reported with the following trace:

com.unity3d.player.UnityPlayer.nativeRender (UnityPlayer.java) com.unity3d.player.UnityPlayer$C$a.handleMessage (UnityPlayer.java:123) android.os.Handler.dispatchMessage (Handler.java:102) android.os.Looper.loopOnce (Looper.java:226) android.os.Looper.loop (Looper.java:313) com.unity3d.player.UnityPlayer$C.run (UnityPlayer.java:25)

By adding a check for unityPlayer and unityLoaded before sending messages, we prevent calls to Unity when it is not ready, thus mitigating the ANR.

How Has This Been Tested?

  • Manual Testing: Tested by launching the app multiple times and interacting with Unity features. No ANRs were observed.

Additional Notes

  • This fix addresses the ANR without introducing any breaking changes.

  • Verified compatibility with Unity versions 2021 and above.

  • [ ] โœจ New feature (non-breaking change which adds functionality)

  • [X] ๐Ÿ› ๏ธ Bug fix (non-breaking change which fixes an issue)

  • [ ] โŒ Breaking change (fix or feature that would cause existing functionality to change)

  • [ ] ๐Ÿงน Code refactor

  • [ ] โœ… Build configuration change

  • [ ] ๐Ÿ“ Documentation

  • [ ] ๐Ÿ—‘๏ธ Chore

RGTechPro avatar Feb 13 '25 06:02 RGTechPro

Do you have an example or reproduction steps to trigger the error when calling this function?

For example; do you call it before unity is created, immediately in the created callback, or just after a pop or dispose.

timbotimbo avatar Feb 13 '25 10:02 timbotimbo