Created
October 15, 2020 09:15
-
-
Save krisrok/eaa8b227b62e19555c5aefe86c5b6618 to your computer and use it in GitHub Desktop.
Unity3D Editor script for warning about objects auto-setting the scene dirty right after saving it (looking at you, ContentSizeFitter)
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
using System; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
[InitializeOnLoad] | |
public class SceneDirtyFlagChecker | |
{ | |
private static Dictionary<Scene, DateTime> _sceneSavedTimestampMap = new Dictionary<Scene, DateTime>(); | |
static SceneDirtyFlagChecker() | |
{ | |
Undo.postprocessModifications += OnPostProcessModifications; | |
EditorSceneManager.sceneSaved += EditorSceneManager_sceneSaved; | |
} | |
private static void EditorSceneManager_sceneSaved(Scene scene) | |
{ | |
_sceneSavedTimestampMap[scene] = DateTime.Now; | |
} | |
private static UndoPropertyModification[] OnPostProcessModifications(UndoPropertyModification[] propertyModifications) | |
{ | |
if (Application.isPlaying) | |
return propertyModifications; | |
for (int i = 0; i < propertyModifications.Length; i++) | |
{ | |
Scene? scene = null; | |
switch (propertyModifications[i].currentValue.target) | |
{ | |
case Component comp: | |
scene = comp.gameObject.scene; | |
break; | |
case GameObject go: | |
scene = go.scene; | |
break; | |
} | |
if (scene.HasValue && _sceneSavedTimestampMap.TryGetValue(scene.Value, out DateTime timestamp)) | |
{ | |
if ((DateTime.Now - timestamp).TotalSeconds < 1) | |
Debug.LogWarning($"Object set scene dirty right after save: {propertyModifications[i].currentValue.target})\nYou can ignore this if the change was done manually.", propertyModifications[i].currentValue.target); | |
} | |
} | |
return propertyModifications; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment