Skip to content

Instantly share code, notes, and snippets.

@NullEntity
Created November 5, 2017 03:34
Show Gist options
  • Save NullEntity/8599a15580b07ebc1a60f6cf01cc1bb7 to your computer and use it in GitHub Desktop.
Save NullEntity/8599a15580b07ebc1a60f6cf01cc1bb7 to your computer and use it in GitHub Desktop.
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