docs.unity3d.com
    Show / Hide Table of Contents

    Class SystemBase

    Implement SystemBase to create a systems in ECS.

    Inheritance
    Object
    ComponentSystemBase
    SystemBase
    EntitiesForEachSystemInNamespaceEndingInSystem
    IJobEntitySystemInNamespaceEndingInSystem
    JobWithCodeSystemInNamespaceEndingInSystem
    CompanionGameObjectUpdateTransformSystem
    SceneSectionStreamingSystem
    WeakAssetReferenceLoadingSystem
    CopyInitialTransformFromGameObjectSystem
    CopyTransformFromGameObjectSystem
    CopyTransformToGameObjectSystem
    Inherited Members
    ComponentSystemBase.Enabled
    ComponentSystemBase.EntityQueries
    ComponentSystemBase.GlobalSystemVersion
    ComponentSystemBase.LastSystemVersion
    ComponentSystemBase.EntityManager
    ComponentSystemBase.World
    ComponentSystemBase.SystemHandleUntyped
    ComponentSystemBase.Time
    ComponentSystemBase.OnCreateForCompiler()
    ComponentSystemBase.OnCreate()
    ComponentSystemBase.OnStartRunning()
    ComponentSystemBase.OnStopRunning()
    ComponentSystemBase.OnDestroy()
    ComponentSystemBase.ShouldRunSystem()
    ComponentSystemBase.GetComponentTypeHandle<T>(Boolean)
    ComponentSystemBase.GetDynamicComponentTypeHandle(ComponentType)
    ComponentSystemBase.GetBufferTypeHandle<T>(Boolean)
    ComponentSystemBase.GetSharedComponentTypeHandle<T>()
    ComponentSystemBase.GetDynamicSharedComponentTypeHandle(ComponentType)
    ComponentSystemBase.GetEntityTypeHandle()
    ComponentSystemBase.RequireForUpdate(EntityQuery)
    ComponentSystemBase.RequireSingletonForUpdate<T>()
    ComponentSystemBase.HasSingleton<T>()
    ComponentSystemBase.GetSingleton<T>()
    ComponentSystemBase.TryGetSingleton<T>(T)
    ComponentSystemBase.SetSingleton<T>(T)
    ComponentSystemBase.GetSingletonEntity<T>()
    ComponentSystemBase.TryGetSingletonEntity<T>(Entity)
    ComponentSystemBase.GetEntityQuery(ComponentType[])
    ComponentSystemBase.GetEntityQuery(NativeArray<ComponentType>)
    ComponentSystemBase.GetEntityQuery(EntityQueryDesc[])
    Namespace: Unity.Entities
    Syntax
    [BurstCompile]
    public abstract class SystemBase : ComponentSystemBase
    Remarks

    Systems in ECS

    A typical system operates on a set of entities that have specific components. The system identifies the components of interest, reading and writing data, and performing other entity operations as appropriate.

    The following example shows a basic system that iterates over entities using a Entities.ForEach construction. In this example, the system iterates over all entities with both a Displacement and a Velocity component and updates the Displacement based on the delta time elapsed since the last frame.

    public struct Position : IComponentData
    {
       public float3 Value;
    }
    
    public struct Velocity : IComponentData
    {
       public float3 Value;
    }
    
    public partial class ECSSystem : SystemBase
    {
       protected override void OnUpdate()
       {
           // Local variable captured in ForEach
           float dT = Time.DeltaTime;
    
           Entities
               .WithName("Update_Displacement")
               .ForEach(
                   (ref Position position, in Velocity velocity) =>
                   {
                       position = new Position()
                       {
                           Value = position.Value + velocity.Value * dT
                       };
                   }
               )
               .ScheduleParallel();
       }
    }

    System lifecycle callbacks

    You can define a set of system lifecycle event functions when you implement a system. The runtime invokes these functions in the following order:

    • OnCreate() -- called when the system is created.
    • OnStartRunning() -- before the first OnUpdate and whenever the system resumes running.
    • OnUpdate() -- every frame as long as the system has work to do (see ShouldRunSystem()) and the system is Enabled.
    • OnStopRunning() -- whenever the system stops updating because it finds no entities matching its queries. Also called before OnDestroy.
    • OnDestroy() -- when the system is destroyed.

    All of these functions are executed on the main thread. To perform work on background threads, you can schedule jobs from the OnUpdate() function.

    System update order

    The runtime executes systems in the order determined by their ComponentSystemGroup. Place a system in a group using UpdateInGroupAttribute. Use UpdateBeforeAttribute and UpdateAfterAttribute to specify the execution order within a group.

    If you do not explicitly place a system in a specific group, the runtime places it in the default World SimulationSystemGroup. By default, all systems are discovered, instantiated, and added to the default World. You can use the DisableAutoCreationAttribute to prevent a system from being created automatically.

    Entity queries

    A system caches all queries created through an Entities.ForEach construction, through ComponentSystemBase.GetEntityQuery, or through ComponentSystemBase.RequireForUpdate. By default, the runtime only calls a system's OnUpdate() function when one of these cached queries finds entities. You can use the AlwaysUpdateSystemAttribute to have the system always update. Note that a system with no queries is also updated every frame.

    Entities.ForEach and Job.WithCode constructions

    The Entities property provides a convenient mechanism for iterating over entity data. Using an Entities.ForEach construction, you can define your entity query, specify a lambda function to run for each entity, and either schedule the work to be done on a background thread or execute the work immediately on the main thread.

    The Entities.ForEach construction uses a C# compiler extension to take a data query syntax that describes your intent and translate it into efficient (optionally) job-based code.

    The Job property provides a similar mechanism for defining a C# Job. You can only use Schedule()to run a Job.WithCode construction, which executes the lambda function as a single job.

    System attributes

    You can use a number of attributes on your SystemBase implementation to control when it updates:

    • UpdateInGroupAttribute -- place the system in a ComponentSystemGroup.
    • UpdateBeforeAttribute -- always update the system before another system in the same group.
    • UpdateAfterAttribute -- always update the system after another system in the same group.
    • AlwaysUpdateSystemAttribute -- invoke OnUpdate every frame.
    • DisableAutoCreationAttribute -- do not create the system automatically.
    • AlwaysSynchronizeSystemAttribute -- force a sync point before invoking OnUpdate.

    Properties

    Name Description
    Dependency

    The ECS-related data dependencies of the system.

    Entities

    Provides a mechanism for defining an entity query and invoking a lambda expression on each entity selected by that query.

    Job

    Provides a mechanism for defining and executing an [IJob].

    Methods

    Name Description
    CompleteDependency()
    GetBuffer<T>(Entity, Boolean)

    Gets the dynamic buffer of an entity.

    GetBufferFromEntity<T>(Boolean)

    Gets a BufferFromEntity<T> object that can access a DynamicBuffer<T>.

    GetComponent<T>(Entity)

    Look up the value of a component for an entity.

    GetComponentDataFromEntity<T>(Boolean)

    Gets an dictionary-like container containing all components of type T, keyed by Entity.

    GetStorageInfoFromEntity()

    Gets a StorageInfoFromEntity object that can access a EntityStorageInfo.

    HasComponent<T>(Entity)

    Checks whether an entity has a specific type of component.

    OnUpdate()

    Implement OnUpdate() to perform the major work of this system.

    SetComponent<T>(Entity, T)

    Sets the value of a component of an entity.

    Update()

    Update the system manually.

    Extension Methods

    ComponentSystemBaseManagedComponentExtensions.GetSingleton<T>(ComponentSystemBase)
    ComponentSystemBaseManagedComponentExtensions.SetSingleton<T>(ComponentSystemBase, T)
    In This Article
    • Systems in ECS
      • System lifecycle callbacks
      • System update order
      • Entity queries
      • Entities.ForEach and Job.WithCode constructions
      • System attributes
    • Properties
    • Methods
    • Extension Methods
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie 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.
    Generated by DocFX on 18 October 2023