Search Unity

Is multi-Threading in unity editor script possible?

Discussion in 'Scripting' started by zhutianlun810, Jul 3, 2020.

  1. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    172
    Hello,
    I am implementing a custom explorer window for my art team. If I select a gameObj in the window, one feature is to show a button, and when you click it, it will print all scenes containing this gameObj. Therefore, this is a very time-consuming job. My art team is complaining that it is very slow and they can't do anything rather than watching the progress bar after clicking the button.

    My goal is to make it faster and async. Now I check whether a specific prefab containing the gameObj by using AssetDataBase.Load(prefabPath) and prefab.transform.Find. (Yes, we have a fixed structure for very scene so it works. we have a prefab's name same as the scene containing it for every scene). Is there any way to speed up this?

    And, my second question is how to make it async. I have googled it for 2 days and most article/forum said I can‘t use Unity API in other threads. Is it true?I don't want to use Coroutine because it can increase the whole searching time.
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    Yes, all Unity APIs need to be called from the main thread, unless specifically noted in the documentation.

    Instead of doing a full search each time, you could build an index, which would make searches much faster. You can use AssetPostprocessor.OnPostprocessAllAssets to know when assets were changed and update the index. There's also Unity's Quick Search package, which already builds an index and which you could possibly query for what you need.

    Another approach, which would allow to do everything on another thread, is to read the assets directly. If you've enabled text serialization, then all the prefabs are just YAML files that you can directly parse and search without blocking the main thread.