EntityManager | Entities | 0.3.0-preview.4
docs.unity3d.com
    Show / Hide Table of Contents

    EntityManager

    The EntityManager owns EntityData, EntityArchetypes, SharedComponentData and EntityQuery.

    EntityManager is where you find APIs to create entities, check if an entity is still alive, instantiate entities and add or remove components.

    // Create an entity with no components on it
    var entity = EntityManager.CreateEntity();
    
    // Adding a component at runtime
    EntityManager.AddComponent(entity, new MyComponentData());
    
    // Get the ComponentData
    MyComponentData myData = EntityManager.GetComponentData<MyComponentData>(entity);
    
    // Set the ComponentData
    EntityManager.SetComponentData(entity, myData);
    
    // Removing a component at runtime
    EntityManager.RemoveComponent<MyComponentData>(entity);
    
    // Does the entity exist and does it have the component?
    bool has = EntityManager.HasComponent<MyComponentData>(entity);
    
    // Is the entity still alive?
    bool has = EntityManager.Exists(entity);
    
    // Instantiate the entity
    var instance = EntityManager.Instantiate(entity);
    
    // Destroy the created instance
    EntityManager.DestroyEntity(instance);
    
    // EntityManager also provides batch APIs
    // to create and destroy many entities in one call. 
    // They are significantly faster 
    // and should be used where ever possible
    // for performance reasons.
    
    // Instantiate 500 entities and write the resulting entity IDs to the instances array
    var instances = new NativeArray<Entity>(500, Allocator.Temp);
    EntityManager.Instantiate(entity, instances);
    
    // Destroy all 500 entities
    EntityManager.DestroyEntity(instances);
    
    Back to top
    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