Search Unity

Best way to do In-Editor processing scripts with Unity ECS/Burst/Job system ?

Discussion in 'C# Job System' started by Mr-Mechanical, Feb 28, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Hello, I am interested in performant editor scripts. What is the recommended/most elegant way to update systems for in-Editor scripts?
    Thanks.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Just add a [AlwaysUpdate System] tag and the system will be loaded in the editor (assuming you use default world initialization otherwise you'll have to create and set it up yourself.)

    By default unity hybrid and rendering systems run in editor.

    You can check out the initialization in

    Unity.Entities.Hybrid\Injection\AutomaticWorldBootstrap.cs
    Unity.Entities.Hybrid\Injection\DefaultWorldInitialization.cs

    -edit-

    the actual editor world setup is created from game object entity

    Code (CSharp):
    1.         void Initialize()
    2.         {
    3.             DefaultWorldInitialization.DefaultLazyEditModeInitialize();
    4.             // ...
    5.         }
    So if you want to setup the systems to run in the editor without having a GameObjectEntity in your scene just call DefaultWorldInitialization.DefaultLazyEditModeInitialize() yourself.
     
    Last edited: Aug 11, 2019
    Razmot and Mr-Mechanical like this.
  3. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    Here is my working solution
    Code (CSharp):
    1.  [ExecuteAlways]
    2.     public class NoiseEditor : MonoBehaviour
    3.     {
    4.         NoiseSystem _noiseSystem;
    5.  
    6.         public void OnEnable()
    7.         {
    8.             Debug.Log("---------------------------------------------------");//helps see wich logs are from the last compile
    9.             if (EditorApplication.isPlaying || _noiseSystem != null) return;
    10.  
    11.             DefaultWorldInitialization.DefaultLazyEditModeInitialize();//boots the ECS EditorWorld
    12.  
    13.             _noiseSystem = World.Active.GetOrCreateSystem<NoiseSystem>();
    14.  
    15.             EditorApplication.update += () => _noiseSystem.Update();//makes the system tick properly, not every 2 seconds !
    16.         }
    17.  
    18.         public void OnDestroy()
    19.         {
    20.             //extra safety against post-compilation problems (typeLoadException) and spamming the console with failed updates
    21.             if (!EditorApplication.isPlaying)
    22.                 EditorApplication.update -= () => _noiseSystem.Update();
    23.         }