首页 > 解决方案 > 将组件存储在静态类中可行吗?

问题描述

我正在为 C# 中 ECS 的自定义实现而苦苦挣扎。

我将组件存储在静态类中,如下所示:

internal static class ComponentStorage<T> where T : struct, IComponent
{
    private static readonly T[] components = new T[capacity];
    public static ref T Get(int index)
    {
        return ref components[index];
    }
    public static void Add(T component, int index)
    {
        components[index] = component;
    }
}

我这样做有两个主要原因:

我用表格等管理管理器类中的组件。

到目前为止,我的实现运行良好,但基于这种方法我很紧张。

我找不到静态通用装箱问题的正确答案,而且我还没有看到任何 ecs 使用它。

标签: c#genericsstaticamazon-ecsentity-component-system

解决方案


推荐阅读