我有一个“实体”类,它有一个“组件”对象列表。现在,“组件”只是一堆不同子类的根类,每个子类每个实体只能表示一次。现在,我想检索某种类型的“组件”,我该怎么做?
这有点难以解释,但这是我想要的代码示例:
class Component {...} // The root class
class CompA : Component {...} // A type of Component
class Entity { List<Component> components; } // Entity with a list of component
...
Entity entity = new Entity(); // Create a new entity
entity.components.Add(new CompA()); // Add a component of type 'CompA'
CompA c = entity.GetComponent<CompA>(); // This is what I'd like to do :)