Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by andrew-fray · Jan 18, 2017 at 02:50 PM · threadingmultithreading

How to check if code is running on the unity thread?

Unity isn't multithreaded. If you start a separate thread and call (eg) Application.isEditor, you get an exception at runtime:

get_isEditor can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

Let's assume I have some common code that could run on either the main unity thread or in a separate thread. How do I check if it's safe for me to call the Unity API?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by ericbegue · Jan 18, 2017 at 03:12 PM

You could store the main thread into a variable then check the current thread against it to figure out whether it's the same or not:

 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 
 using UnityEngine;
 
 public class MainThreadTest : MonoBehaviour
 {
     static Thread mainThread = Thread.CurrentThread;
 
     // Use this for initialization
     void Start ()
     {
         var thread = new Thread( SomeWork );
         thread.Start();
     }
     
     void SomeWork()
     {
         if(Thread.CurrentThread != mainThread )
         {
             Debug.LogWarning("This is not the main thread.");
         }
     }
 }


AI Scripting - Panda BT - Behaviour Tree scripting for Unity.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 18, 2017 at 04:12 PM 4
Share

Do not initialize your static variable like that:

 static Thread mainThread = Thread.CurrentThread;

Unity loades your scene in a background thread. So you can't ensure that the thread which accesses your class the first time is the main thread. You should gather the value in Awake or any other callback that is ensured to actually run on the main thread.

avatar image ericbegue Bunny83 · Jan 18, 2017 at 04:54 PM 0
Share

I was not aware about that. So Unity is multi-threaded internally?

avatar image SoylentGraham ericbegue · Mar 29, 2018 at 05:08 PM 0
Share

Yeah, unity IS multithreaded, but *the unityengine api is NOT threadsafe. And some things occur on different threads (scene loading, rendering etc), and this varies per platform.

avatar image roointan · Nov 27, 2018 at 10:57 PM 1
Share

Why not edit the answer as suggested by @Bunny83 ?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MonoBehaviour.Invoke and Threading 2 Answers

Multithreading freezes editor 1 Answer

Getting current time from the worker thread 1 Answer

How to use MultiThreading to remove momentary unresponsiveness 0 Answers

Threaded procedural animation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges