示例#1
0
        public static void Initialize()
        {
            if (s_Types != null)
            {
                return;
            }

            ObjectOffset     = UnsafeUtility.SizeOf <ObjectOffsetType>();
            s_CreateTypeLock = new SpinLock();
            s_Types          = new ComponentType[MaximumTypesCount];
            s_Count          = 0;

            s_Types[s_Count++] = new ComponentType(null, 0, TypeCategory.ComponentData, null, null, 0);
            // This must always be first so that Entity is always index 0 in the archetype
            s_Types[s_Count++] = new ComponentType(typeof(Entity), sizeof(Entity), TypeCategory.EntityData,
                                                   FastEquality.CreateLayout(typeof(Entity)), EntityRemapUtility.CalculateEntityOffsets(typeof(Entity)), 0);
        }
示例#2
0
        private static ComponentType BuildComponentType(Type type)
        {
            var          componentSize = 0;
            TypeCategory category;

            FastEquality.Layout[] fastEqualityLayout = null;
            EntityOffsetInfo[]    entityOffsets      = null;
            var memoryOrdering = CalculateMemoryOrdering(type);

            if (typeof(IComponentData).IsAssignableFrom(type))
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (type.IsClass)
                {
                    throw new ArgumentException($"{type} is an IComponentData, and thus must be a struct.");
                }
                if (!UnsafeUtility.IsBlittable(type))
                {
                    throw new ArgumentException(
                              $"{type} is an IComponentData, and thus must be blittable (No managed object is allowed on the struct).");
                }
#endif

                category           = TypeCategory.ComponentData;
                componentSize      = UnsafeUtility.SizeOf(type);
                fastEqualityLayout = FastEquality.CreateLayout(type);
                entityOffsets      = EntityRemapUtility.CalculateEntityOffsets(type);
            }
            else if (typeof(ISharedComponentData).IsAssignableFrom(type))
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (type.IsClass)
                {
                    throw new ArgumentException($"{type} is an ISharedComponentData, and thus must be a struct.");
                }
#endif

                category           = TypeCategory.ISharedComponentData;
                fastEqualityLayout = FastEquality.CreateLayout(type);
            }
            else if (type.IsValueType)
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (!UnsafeUtility.IsBlittable(type))
                {
                    throw new ArgumentException($"{type} is used for FixedArrays, and thus must be blittable.");
                }
#endif
                category      = TypeCategory.OtherValueType;
                componentSize = UnsafeUtility.SizeOf(type);
            }
            else if (type.IsClass)
            {
                category = TypeCategory.Class;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (type.FullName == "Unity.Entities.GameObjectEntity")
                {
                    throw new ArgumentException(
                              "GameObjectEntity can not be used from EntityManager. The component is ignored when creating entities for a GameObject.");
                }
#endif
            }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            else
            {
                throw new ArgumentException($"'{type}' is not a valid component");
            }