首页 > 解决方案 > UNITY ECS 不在运行时注册新实体

问题描述

我是新来的,希望你能解决我的小问题。

我决定在 Unity 中测试新的 ECS 系统并遇到这样的问题:

我有两个实体和一个控制它们的系统。当我开始播放模式时 - 我的系统找到了两个实体,但是当我在运行时生成第三个实体时 - 系统没有更新它。

我的系统:

public class MovementSystem : ComponentSystem
{
    public struct ComponentFilter
    {
        public Transform m_Transform;
        public MoveComponent m_MoveComponent;
    }

    protected override void OnUpdate()
    {
        var entities = GetEntities<ComponentFilter>();
        foreach (var item in entities)
        {
            Debug.Log(item.m_MoveComponent);
        }
    }
}

我的组件:

[RequireComponent(typeof(GameObjectEntity))
public class MoveComponent : MonoBehaviour 
{
    //do nothing. may be added to another gameObject by AddComponent 
    //if object doesn't have GameObjectEntity - creates it
    //and then system will not update it.
}

如何在我的系统中注册这个新对象?谢谢你的回答!

标签: c#unity3d

解决方案


如果我正确理解了这个问题,那么您还没有实例化新实体。您可以在您的 MovementSystem 中使用.EntityManager.Instantiate(Entity srcEntity)或在另一个地方使用World.Active.GetOrCreateManager<EntityManager>().Instantiate(Entity srcEntity).


推荐阅读