Skip to content

Commit ecacd8b

Browse files
committed
feat: register complete shorthand
1 parent 1ad85d2 commit ecacd8b

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Undo.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Copyright (c) 2026 by Shen, Jen-Chieh $
99
*/
1010
using System;
11+
using System.Runtime.CompilerServices;
1112
using UnityEditor;
1213

1314
namespace JCSUnity
@@ -27,13 +28,44 @@ public static class JCS_Undo
2728
/// Like `Undo.RegisterCompleteObjectUndo` but a wrapper.
2829
/// </summary>
2930
public static void RegisterComplete(
30-
UnityEngine.Object objectToUndo, string name, Action action)
31+
UnityEngine.Object obj, string name, Action action)
3132
{
32-
Undo.RegisterCompleteObjectUndo(objectToUndo, name);
33+
var objs = new UnityEngine.Object[1] { obj };
34+
35+
RegisterComplete(objs, name, action);
36+
}
37+
public static void RegisterComplete(
38+
UnityEngine.Object[] objs, string name, Action action)
39+
{
40+
Undo.RegisterCompleteObjectUndo(objs, name);
3341

3442
action?.Invoke();
3543

36-
EditorUtility.SetDirty(objectToUndo);
44+
foreach (UnityEngine.Object obj in objs)
45+
EditorUtility.SetDirty(obj);
46+
}
47+
48+
/// <summary>
49+
/// Like `RegisterComplete` but automatically assign the
50+
/// undo operation name.
51+
/// </summary>
52+
public static void RegisterComplete(
53+
UnityEngine.Object obj,
54+
Action action,
55+
[CallerMemberName] string name = "")
56+
{
57+
var objs = new UnityEngine.Object[1] { obj };
58+
59+
RegisterComplete(objs, action, name);
60+
}
61+
public static void RegisterComplete(
62+
UnityEngine.Object[] objs,
63+
Action action,
64+
[CallerMemberName] string name = "")
65+
{
66+
string id = objs.GetType().Name;
67+
68+
RegisterComplete(objs, $"{id} ({name})", action);
3769
}
3870
}
3971
}

0 commit comments

Comments
 (0)