Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Generic JobComponentSystem

Discussion in 'Data Oriented Technology Stack' started by Radu392, Oct 11, 2019 at 3:12 AM.

  1. Radu392


    Jan 6, 2016
    Hi, I'm trying to create a generic JobComponentSystem, but I'm not sure how to get it to run non stop like a normal system.

    Code (CSharp):
    1.     public class RenderSystem<T> : JobComponentSystem where T : struct, IComponentData {
    3.         protected override void OnCreate() {
    4.             Debug.Log("created system");
    5.             base.OnCreate();
    6.         }
    7.         protected override JobHandle OnUpdate(JobHandle inputDeps) {
    8.             Debug.Log("updating system");
    9.             return inputDeps;
    10.         }
    11.     }
    I add it to the world in the Start() function of a MonoBehavior:

    Code (CSharp):
    1.             RenderSystem<FastRenderer> renderSystem = World.Active.GetOrCreateSystem<RenderSystem<FastRenderer>>();
    2.             renderSystem.Enabled = true;
    OnCreate is called, but OnUpdate is not.

    I tried renderSystem.Update(); right after I created the system and it did call OnUpdate, but only once since it was done in the Start() method. Isn't there a way to make a generic system run automatically just like a normal system? If not, do I have to call the renderSystem.Update() somewhere myself every frame? If so, where? In the Update() of a Monobehavior or a system? An example would be greatly appreciated since I couldn't find any info on this. I've heard some vague words about System Groups but that sounds intimidating since I never actually saw an example on how it's done.

    I tried doing renderSystem.Update() inside a Monobehavior Update and it seemed to work, but I'm just not sure if I'm getting the best performance that way. Is there a better way or is that it?
    Last edited: Oct 11, 2019 at 3:21 AM
  2. ilih


    Aug 6, 2013
    Probably because it does nothing, as far I know if JobComponentSystem.OnUpdate called only if it has entities to work with and this check is done with manually created EntityQuery in OnCreate or automatically created EntityQuery based on scheduled job.
    Try to create EntityQuery in OnCreate method or schedule the actual job in OnUpdate.
  3. eizenhorn


    Oct 17, 2016
    It's not entirely true. If you haven't any EQ it will runs like with [AlwaysUpdateSystem] attribute - always.

    @Radu392 your problem is your system not in player loop (cause it's generic), after creation you should put it in to some of root component groups (Initialization, Simulation, Presentation).

    Code (CSharp):
    1. using Unity.Jobs;
    2. using Unity.Entities;
    3. using UnityEngine;
    5. public class TestJob : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         var simulationGroup = World.Active.GetOrCreateSystem<SimulationSystemGroup>();
    10.         var s1 = World.Active.GetOrCreateSystem<GenericSystem<Data1>>();
    11.         var s2 = World.Active.GetOrCreateSystem<GenericSystem<Data2>>();
    12.         var s3 = World.Active.GetOrCreateSystem<GenericSystem<Data3>>();
    13.         simulationGroup.AddSystemToUpdateList(s1);
    14.         simulationGroup.AddSystemToUpdateList(s2);
    15.         simulationGroup.AddSystemToUpdateList(s3);
    16.         simulationGroup.SortSystemUpdateList();
    17.     }
    18. }
    20. public struct Data1: IComponentData{}
    21. public struct Data2: IComponentData{}
    22. public struct Data3: IComponentData{}
    24. public class GenericSystem<T> : JobComponentSystem where T : struct, IComponentData
    25. {
    26.     protected override void OnCreate() {
    27.         Debug.Log("Created: " + typeof(T).Name);
    28.     }
    29.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    30.     {
    31.         Debug.Log("Updated: " + typeof(T).Name);
    32.         return inputDeps;
    33.     }
    34. }
    Radu392 and ilih like this.
  4. Radu392


    Jan 6, 2016
    Yep that works, thanks!