Ejemplo n.º 1
0
        protected override void OnUpdate()
        {
            var groupLength = m_Groups.Count;

            m_CachedDictionaryEventKeepIds.Clear();

            // Remove automatically groups with 0 entities in it
            // If none are found, we remove the group from the list.
            // If an entity is attached to the group with the same id, we use another version of this id (a là Entity class)
            for (int i = 0; i != m_EntityGroup.Length; i++)
            {
                var group = m_EntityGroup.EntityGroups[i];
                m_CachedDictionaryEventKeepIds[group.ReferenceId] = true;
            }
            for (int i = 0; i != m_MaxId; i++)
            {
                if (m_Groups.FastTryGet(i, out var _) &&
                    !m_CachedDictionaryEventKeepIds.FastTryGet(i, out var __))
                {
                    var group = m_Groups[i];

                    Debug.Log($"Group({i}, {group.Version}) was unused, so it was removed.");

                    group.IsCreated = false;
                    group.Version++;

                    m_PooledGroups.Enqueue(group);
                    m_Groups.Remove(i);
                }
            }
        }
Ejemplo n.º 2
0
        internal EntityGroup AttachBehaviour(EntityGroupBehaviour behaviour)
        {
            if (behaviour.SceneGroupId < 0)
            {
                return(CreateGroup());
            }

            if (!m_ApplyGroupIdFromSceneGroupIds.FastTryGet(behaviour.SceneGroupId, out var groupId))
            {
                var data = CreateGroup();

                m_ApplyGroupIdFromSceneGroupIds[behaviour.SceneGroupId] = data.ReferenceId;

                return(data);
            }

            return(m_Groups[groupId]);
        }
Ejemplo n.º 3
0
        public void Update(ref EntityArray array)
        {
            m_Entities.Clear();
            EntitySpawnCount   = 0;
            EntityReplaceCount = 0;
            EntityRemovalCount = 0;

            var defaultEntity = new Entity();
            var length        = array.Length;

            if (NeedEntitiesList)
            {
                UnityEngine.Profiling.Profiler.BeginSample("ForLoop #1 - With entities");
                for (int i = 0; i != length; i++)
                {
                    Entity entity;
                    UnityEngine.Profiling.Profiler.BeginSample("Get entities and init var");
                    var checkedEntity = array[i];
                    UnityEngine.Profiling.Profiler.EndSample();
                    UnityEngine.Profiling.Profiler.BeginSample("TryGet()");
                    if (!m_CurrentEntities.FastTryGet(checkedEntity.Index, out entity))
                    {
                        if (NeedEntitiesList)
                        {
                            m_Entities.Add(new EntityChangeItem()
                            {
                                entity     = checkedEntity,
                                wasCreated = true
                            });
                        }
                        m_CurrentEntities[checkedEntity.Index] = checkedEntity;
                        EntitySpawnCount++;
                    }
                    else if (NeedToCheckReplacement)
                    {
                        UnityEngine.Profiling.Profiler.BeginSample("Check Version");
                        // The entity was replaced
                        if (entity.Version != checkedEntity.Version)
                        {
                            EntityReplaceCount++;
                            m_CurrentEntities[checkedEntity.Index] = checkedEntity;
                        }
                        UnityEngine.Profiling.Profiler.EndSample();
                    }
                    UnityEngine.Profiling.Profiler.EndSample();
                }
                UnityEngine.Profiling.Profiler.EndSample();
            }
            else
            {
                UnityEngine.Profiling.Profiler.BeginSample("ForLoop #1 - Without entities");
                EntitySpawnCount = length - m_LastLength;
                m_LastLength     = length;
                UnityEngine.Profiling.Profiler.EndSample();
            }

            UnityEngine.Profiling.Profiler.BeginSample("Check for removal");
            if (NeedToCheckRemoval && array.Length != m_CurrentEntities.Count)
            {
                var toRemove = new NativeList <int>(Allocator.Temp);
                foreach (var entity in m_CurrentEntities.Values)
                {
                    var exist = false;
                    for (int i = 0; i != array.Length; i++)
                    {
                        if (array[i].Index == entity.Index)
                        {
                            exist = true;
                            break;
                        }
                    }
                    if (exist)
                    {
                        continue;
                    }
                    EntityRemovalCount++;
                    m_Entities.Add(new EntityChangeItem()
                    {
                        entity     = entity,
                        wasCreated = false
                    });

                    toRemove.Add(entity.Index);
                }
                for (int i = 0; i != toRemove.Length; i++)
                {
                    m_CurrentEntities.Remove(toRemove[i]);
                }

                toRemove.Dispose();
            }
            UnityEngine.Profiling.Profiler.EndSample();
        }