Exemple #1
0
        public void UpdateSceneEntities()
        {
            CollectSceneEntities();

            bool didChange = false;

            for (int i = 0; i < sceneEntities.Count; i++)
            {
                int storedId;

                Entity           sceneEntity = sceneEntities[i];
                EntityDefinition definition;

                Chassis entityChassis = sceneEntity.GetComponent <Chassis>();
                if (entityChassis != null)
                {
                    Debug.Log($"Removed Chassis from {sceneEntity.name} because you cannot have both an entity and a Chassis component");
                    Object.DestroyImmediate(entityChassis);
                }

                int currentId = sceneEntity.id;

                if (sceneEntityToSceneIdMap.TryGetValue(sceneEntity, out storedId))
                {
                    Debug.Assert(currentId == storedId, $"Should never hit this. Expected {currentId} to be {storedId}");
                    definition = GetEntityDefinitionForSceneEntity(sceneEntity);
                }
                else
                {
                    Debug.Log(sceneEntity.name);
                    didChange      = true;
                    sceneEntity.id = GetCurrentMission().AllocateEntityId();
                    EditorUtility.SetDirty(sceneEntity); // todo -- dev only
                    if (currentId != 0)
                    {
                        Debug.Log("Duplicated");
                        EntityDefinition toClone = GetEntityDefinitionForSceneEntity(currentId);
                        definition = currentMission.CloneEntityDefinition(toClone);
                    }
                    else
                    {
                        Debug.Log("Created");
                        definition = currentMission.CreateEntityDefinition();
                    }
                }

                // if a new one is created, add a definition and link
                // if a one is duplicated, clone definition and link
                // we *might* have unlinked scene entities which is ok

                sceneEntityToSceneIdMap.Set(sceneEntity, sceneEntity.id);
                sceneEntityToDefinitionMap.Set(sceneEntity, definition);

                SetSceneEntityName(sceneEntity, definition);
            }

            GetCurrentMission().Change();

            if (didChange)
            {
                EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
            }
        }