Skip to content

Instantly share code, notes, and snippets.

@wappenull
Created June 6, 2021 04:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wappenull/acfd388185c543bfe8fa98f6ab07f317 to your computer and use it in GitHub Desktop.
Save wappenull/acfd388185c543bfe8fa98f6ab07f317 to your computer and use it in GitHub Desktop.
Help track unknown Unity scene dirty which bloat the version control.
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
// See https://forum.unity.com/threads/how-to-know-what-makes-scene-dirty.694390
namespace Wappen.Editor
{
/// <summary>
/// Editor tool to track live scene change.
/// </summary>
public static class SceneChangeTracker
{
public static bool TrackSceneChanges = false;
[MenuItem("Tools/Wappen/Toggle SceneChangeTracker")]
static void Toggle( )
{
if( TrackSceneChanges )
DisableSceneDirtyChecker( );
else
EnableSceneDirtyChecker( );
EditorUtility.DisplayDialog( nameof(SceneChangeTracker), "SceneChangeTracker is now " + (TrackSceneChanges ? "ON" : "OFF"), "YEE" );
}
public static void EnableSceneDirtyChecker( )
{
if( !TrackSceneChanges )
{
Undo.postprocessModifications += OnPostProcessModifications;
EditorSceneManager.sceneDirtied += SceneDirtied;
TrackSceneChanges = true;
}
}
public static void DisableSceneDirtyChecker( )
{
if( TrackSceneChanges )
{
Undo.postprocessModifications -= OnPostProcessModifications;
EditorSceneManager.sceneDirtied -= SceneDirtied;
TrackSceneChanges = false;
}
}
private static void SceneDirtied( Scene scene )
{
Debug.Log( $"sceneDirtied on {scene.name}. (See full stacktrace)" );
}
public static UndoPropertyModification[] OnPostProcessModifications( UndoPropertyModification[] propertyModifications )
{
foreach( UndoPropertyModification mod in propertyModifications )
{
Debug.Log( $"Dirty {mod.currentValue.target.name}: {mod.currentValue.propertyPath} to {mod.currentValue.value} from {mod.previousValue.value}", mod.currentValue.target );
}
return propertyModifications;
}
}
}
@TheXRMonk
Copy link

Godlike!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment