Disable undo on all relevant Unity function invocations

This commit is contained in:
Haï~
2023-10-02 04:53:27 +02:00
parent a1e5a43491
commit 0e15f255b3
5 changed files with 81 additions and 37 deletions
+41
View File
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
@@ -74,5 +75,45 @@ namespace AnimatorAsCode.V1
{
return new EditorCurveBinding {path = binding.path, type = binding.type, propertyName = binding.propertyName + "." + suffix};
}
internal static void NoUndo<T>(T obj, Action action)
{
try
{
UndoDisable(obj);
action.Invoke();
}
finally
{
UndoEnable(obj);
}
}
internal static TResult NoUndo<T, TResult>(T obj, Func<TResult> action)
{
try
{
UndoDisable(obj);
return action.Invoke();
}
finally
{
UndoEnable(obj);
}
}
private static void UndoDisable<T>(T state)
{
typeof(T)
.GetProperty("pushUndo", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(state, false);
}
private static void UndoEnable<T>(T state)
{
typeof(T)
.GetProperty("pushUndo", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(state, true);
}
}
}