Created
November 5, 2017 03:34
-
-
Save NullEntity/8599a15580b07ebc1a60f6cf01cc1bb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void RemoveMissingComponentsInCurrentScene(ILevelData levelData) | |
{ | |
var badComponents = SceneManager | |
.GetActiveScene() | |
.GetRootGameObjects() | |
.Flatten() | |
.Select(gameObject => new | |
{ | |
GameObject = gameObject, | |
Components = gameObject.GetComponents<Component>() | |
.Select((c, i) => new {Component = c, Index = i}) | |
.Where(componentIndex => componentIndex.Component == null) | |
.ToList() | |
}) | |
.Where(gameObjectComponents => gameObjectComponents.Components.Any()) | |
.ToList(); | |
foreach (var gameObjectComponents in badComponents) | |
{ | |
var serializedObject = new SerializedObject(gameObjectComponents.GameObject); | |
var prop = serializedObject.FindProperty("m_Component"); | |
gameObjectComponents.Components | |
.Select(_ => _.Index) | |
.OrderByDescending(x => x) | |
.ForEach(i => prop.DeleteArrayElementAtIndex(i)); | |
serializedObject.ApplyModifiedProperties(); | |
EditorUtility.SetDirty(gameObjectComponents.GameObject); | |
EditorSceneManager.MarkSceneDirty(gameObjectComponents.GameObject.scene); | |
} | |
if (badComponents.Any()) | |
{ | |
Debug.Log("Updating scene " + levelData.Scene); | |
EditorSceneManager.SaveOpenScenes(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment