Unity3D和Android:"UnityMain"之间的区别和“主要"线程? [英] Unity3D & Android: Difference between "UnityMain" and "main" threads?

查看:1708
本文介绍了Unity3D和Android:"UnityMain"之间的区别和“主要"线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR::我正在使用JNI从Unity C#调用自定义JAR.但是android库表示它正在"UnityMain"线程上运行,而该活动的实际ui线程称为"main".两者有什么区别?

TLDR: I'm using JNI to call into my custom JAR from Unity C#. But the android library says it's running on the "UnityMain" thread, while the actual ui thread for the activity is called "main". What is the difference between the two?

详细信息: 这对我来说是个问题,因为出现错误无法在尚未调用Looper.prepare()的线程内创建处理程序".这是从Java打印两个线程时得到的输出:

Details: This is a problem for me since I get the error "Can't create handler inside thread that has not called Looper.prepare()". Here's the output I get when printing the two threads from Java:

/// Java Output
Current Thread:    Thread[UnityMain,5,main]
MainLooper Thread: Thread[main,5,main]

要解决此问题,我正在使用Activity.runOnUiThread方法运行JNI调用:

To resolve this, I'm running the JNI calls using the Activity.runOnUiThread method:

/// Unity C# Code
activityObj.Call("runOnUiThread", new AndroidJavaRunnable(() => {
  // JNI calls and other stuff
}

现在从Java打印两个线程时,我得到以下输出:

Now I get the following output when printing the two threads from Java:

/// Java Output
Current Thread:    Thread[main,5,main]
MainLooper Thread: Thread[main,5,main]

现在唯一的问题是我无法从主"线程(即在"runOnUiThread"回调内部)进行Unity协程或Invoke调用.我收到以下Unity错误:

Only problem now is that I can't make Unity Coroutine or Invoke calls from the "main" thread (i.e. inside the "runOnUiThread" callback). I get the following Unity error:

/// Unity C# Output
E/Unity   (21048): Invoke can only be called from the main thread.
E/Unity   (21048): Constructors and field initializers will be executed from the loading thread when loading a scene.
E/Unity   (21048): Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

那么"UnityMain"和"main"线程之间有什么区别?为什么Java的主"线程与Unity的线程不同?

So what is the difference between the "UnityMain" and "main" threads? And why is the Java "main" thread different from that of Unity?

推荐答案

Unity运行其自己的线程来处理其处理.启动应用程序时,它不会篡改由Android OS创建的Android主线程.通常,当您编写传统的Android应用程序时,只有一个主线程,并且处理UI的所有内容都必须在主线程上运行.使用第二个主"线程是Unity做出的设计选择,可能因此它可以做任何想要的事情而不会弄乱Android主应用程序.如果要在Unity之外的Android UI中执行任何操作,则需要在主线程上运行代码.您可以使用以下代码段在任何地方执行此操作:

Unity runs its own thread to handle its processing. It doesn't usurp the Android main thread created by the Android OS when launching the app. Typically when you write a traditional Android app there is only one main thread, and everything that deals with the UI must run on the main thread. To use a second "main" thread, was a design choice made by Unity, probably so it can do whatever it wants without messing with the apps Android main thread. If you want to do anything in the Android UI outside of Unity you'll need to have your code run on the main thread. You can do this from anywhere using the below snippet:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        Log.d("MAIN", "Thread? " + Thread.currentThread());
    }
});

如果您要调用的Android/Java代码可以访问Application上下文或Activity上下文,那么您也可以使用runOnUiThread.

If the Android/Java code you are calling into has access to the Application context or an Activity context you can use runOnUiThread as well.

这篇关于Unity3D和Android:"UnityMain"之间的区别和“主要"线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆