Skip to content

Commit 9ab7736

Browse files
committed
feat: Add undo util
1 parent 7d16d8c commit 9ab7736

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#if UNITY_EDITOR
2+
/**
3+
* $File: JCS_Undo.cs $
4+
* $Date: $
5+
* $Revision: $
6+
* $Creator: Jen-Chieh Shen $
7+
* $Notice: See LICENSE.txt for modification and distribution information
8+
* Copyright (c) 2026 by Shen, Jen-Chieh $
9+
*/
10+
using System;
11+
using UnityEditor;
12+
13+
namespace JCSUnity
14+
{
15+
/// <summary>
16+
/// Undo and redo utilities.
17+
/// </summary>
18+
public static class JCS_Undo
19+
{
20+
/* Variables */
21+
22+
/* Setter & Getter */
23+
24+
/* Functions */
25+
26+
/// <summary>
27+
/// Like `Undo.RegisterCompleteObjectUndo` but a wrapper.
28+
/// </summary>
29+
public static void RegisterComplete(
30+
UnityEngine.Object objectToUndo, string name, Action action)
31+
{
32+
Undo.RegisterCompleteObjectUndo(objectToUndo, name);
33+
34+
action?.Invoke();
35+
36+
EditorUtility.SetDirty(objectToUndo);
37+
}
38+
}
39+
}
40+
#endif

Assets/JCSUnity/Scripts/Util/JCS_Undo.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ public static int Delta(this int data, int val)
5252
return data + val;
5353
}
5454

55+
public static long Delta(this long data, long val, long max)
56+
{
57+
return data.Delta(val, 0, max);
58+
}
59+
public static long Delta(this long data, long val, long min, long max)
60+
{
61+
return System.Math.Clamp(data.Delta(val), min, max);
62+
}
63+
public static long Delta(this long data, long val)
64+
{
65+
return data + val;
66+
}
67+
5568
public static float Delta(this float data, float val, float max)
5669
{
5770
return data.Delta(val, 0.0f, max);
@@ -65,6 +78,19 @@ public static float Delta(this float data, float val)
6578
return data + val;
6679
}
6780

81+
public static double Delta(this double data, double val, double max)
82+
{
83+
return data.Delta(val, 0.0f, max);
84+
}
85+
public static double Delta(this double data, double val, double min, double max)
86+
{
87+
return System.Math.Clamp(data.Delta(val), min, max);
88+
}
89+
public static double Delta(this double data, double val)
90+
{
91+
return data + val;
92+
}
93+
6894
/// <summary>
6995
/// Delta the `num` with `val` by percentage and clamp the
7096
/// result with `min` and `max`.
@@ -84,6 +110,21 @@ public static int DeltaP(this int data, int p)
84110
return data + val;
85111
}
86112

113+
public static long DeltaP(this long data, long p, long max)
114+
{
115+
return data.DeltaP(p, 0, max);
116+
}
117+
public static long DeltaP(this long data, long p, long min, long max)
118+
{
119+
return System.Math.Clamp(data.DeltaP(p), min, max);
120+
}
121+
public static long DeltaP(this long data, long p)
122+
{
123+
int val = (int)(data * p / 100.0);
124+
125+
return data + val;
126+
}
127+
87128
public static float DeltaP(this float data, float p, float max)
88129
{
89130
return data.DeltaP(p, 0.0f, max);
@@ -99,6 +140,21 @@ public static float DeltaP(this float data, float p)
99140
return data + val;
100141
}
101142

143+
public static double DeltaP(this double data, double p, double max)
144+
{
145+
return data.DeltaP(p, 0.0, max);
146+
}
147+
public static double DeltaP(this double data, double p, double min, double max)
148+
{
149+
return System.Math.Clamp(data.DeltaP(p), min, max);
150+
}
151+
public static double DeltaP(this double data, double p)
152+
{
153+
double val = (data * p / 100.0);
154+
155+
return data + val;
156+
}
157+
102158
#endregion
103159

104160
#region String
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# JCS_Undo
2+
3+
Undo and redo utilities.
4+
5+
## Functions
6+
7+
| Name | Description |
8+
|:-----------------|:------------------------------------------------------|
9+
| RegisterComplete | Like `Undo.RegisterCompleteObjectUndo` but a wrapper. |

0 commit comments

Comments
 (0)