Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Unity: Android "main" (UI) thread

Discussion in 'Android' started by cowlinator, Mar 18, 2013.

  1. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    All Android apps have a "Main" thread that handles all the UI. This is how the Android OS tests if an app is responsive or not. (see more here: http://android-developers.blogspot.com/2009/05/painless-threading.html )

    I'm sometimes getting "Application Not Responding" on my app, which appears to be originating in java plugins, so I need to know a few things about Unity.

    So here's what I would like to know:

    Which android thread does the Unity engine run on?

    I know that all unity script code runs on a single thread; is this the same thread as the engine?

    When calls are made to native java android plugins, does it run the java code in a different thread? (It seems to be asynchronous as near as I can tell, but then it doesn't make any sense to me how the java plugin could be on the main thread, as is shown in the ANR report I am getting)
     
  2. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    1. Unity is using it's own threads to do the processing required by the engine.
    2. It's not entirely true that unity scripting is bound to be single threaded - but yes, if you call into java from a unity script it will be the same thread executing that java function as were executing the script.

    If you are seing ANR's it's most likely due to a java plugin doing stuff on the UI thread, or in some other way blocking the input events. This can happen outside of unity control. In fact, my guess is that most java plugins are hooking up either to the UI thread to render views on top of unity, or do async processing on their own thread since stalling script execution is rarely a good idea.
     
    Pyr3z likes this.