Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

RGTechPro
Copy link

@RGTechPro RGTechPro commented Feb 13, 2025

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)

  • 🛠️ 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

Sorry, something went wrong.

@RGTechPro RGTechPro changed the title Anr fix poker Anr fix "com.unity3d.player.UnityPlayer.nativeRender" Feb 13, 2025
@timbotimbo
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants