Skip to content

Commit d34d05c

Browse files
authored
Merge pull request #9 from kxn4t/add-undo-for-material-swap
add Undo support in Material Swap
2 parents 17138a4 + 40cba20 commit d34d05c

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

Editor/MaterialSwapHelper/MaterialSwapGenerator.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static GenerationResult CreateMaterialSwap(GameObject targetRoot, CopiedM
3939

4040
try
4141
{
42+
// Set up Undo group for this operation
43+
Undo.SetCurrentGroupName("Create Material Swap");
44+
int undoGroup = Undo.GetCurrentGroup();
45+
4246
var (colorMenu, isNewMenu) = EnsureColorMenu(targetRoot);
4347
var groups = MaterialSwapHelperSession.GetCopiedDataGroups();
4448

@@ -97,6 +101,9 @@ public static GenerationResult CreateMaterialSwap(GameObject targetRoot, CopiedM
97101

98102
EditorUtility.SetDirty(targetRoot);
99103

104+
// Collapse all Undo operations into a single operation
105+
Undo.CollapseUndoOperations(undoGroup);
106+
100107
string resultMessage = isNewMenu
101108
? $"Created Color Menu with {groups.Count} color variations (Color{startingColorNumber}-Color{startingColorNumber + groups.Count - 1})"
102109
: $"Added {groups.Count} color variations (Color{startingColorNumber}-Color{startingColorNumber + groups.Count - 1})";
@@ -128,6 +135,10 @@ public static GenerationResult CreateMaterialSwapPerObject(GameObject targetRoot
128135

129136
try
130137
{
138+
// Set up Undo group for this operation
139+
Undo.SetCurrentGroupName("Create Material Swap Per Object");
140+
int undoGroup = Undo.GetCurrentGroup();
141+
131142
var (colorMenu, isNewMenu) = EnsureColorMenu(targetRoot);
132143
var groups = MaterialSwapHelperSession.GetCopiedDataGroups();
133144

@@ -154,8 +165,11 @@ public static GenerationResult CreateMaterialSwapPerObject(GameObject targetRoot
154165
var colorVariation = new GameObject(colorName);
155166
colorVariation.transform.SetParent(colorMenu, false);
156167

168+
// Register the created GameObject with Undo system
169+
Undo.RegisterCreatedObjectUndo(colorVariation, "Create Color Variation");
170+
157171
#if MODULAR_AVATAR_INSTALLED
158-
var menuItem = colorVariation.AddComponent<ModularAvatarMenuItem>();
172+
var menuItem = Undo.AddComponent<ModularAvatarMenuItem>(colorVariation);
159173
ModularAvatarIntegration.ConfigureMenuItemAsToggle(menuItem, colorName, colorNumber, targetRoot.name);
160174
#endif
161175
createdVariations.Add(colorVariation);
@@ -192,6 +206,9 @@ public static GenerationResult CreateMaterialSwapPerObject(GameObject targetRoot
192206

193207
EditorUtility.SetDirty(targetRoot);
194208

209+
// Collapse all Undo operations into a single operation
210+
Undo.CollapseUndoOperations(undoGroup);
211+
195212
string resultMessage = isNewMenu
196213
? $"Created Color Menu with {groups.Count} color variations (per-object components)"
197214
: $"Added {groups.Count} color variations with per-object components";
@@ -310,8 +327,11 @@ private static (GameObject colorVariation, int nextColorNumber, string newColorN
310327
colorVariation = new GameObject(newColorName);
311328
colorVariation.transform.SetParent(colorMenu, false);
312329

330+
// Register the created GameObject with Undo system
331+
Undo.RegisterCreatedObjectUndo(colorVariation, "Create Color Variation");
332+
313333
#if MODULAR_AVATAR_INSTALLED
314-
var menuItem = colorVariation.AddComponent<ModularAvatarMenuItem>();
334+
var menuItem = Undo.AddComponent<ModularAvatarMenuItem>(colorVariation);
315335
ModularAvatarIntegration.ConfigureMenuItemAsToggle(menuItem, newColorName, nextColorNumber, gameObjectName);
316336
#endif
317337
}

Editor/MaterialSwapHelper/ModularAvatarIntegration.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using UnityEngine;
5+
using UnityEditor;
56

67
#if MODULAR_AVATAR_INSTALLED
78
using System.Reflection;
@@ -105,11 +106,14 @@ public static GameObject CreateColorMenu(GameObject parent, string menuName)
105106
var colorMenu = new GameObject(menuName);
106107
colorMenu.transform.SetParent(parent.transform, false);
107108

109+
// Register the created GameObject with Undo system
110+
Undo.RegisterCreatedObjectUndo(colorMenu, "Create Color Menu");
111+
108112
// Add Menu Installer
109-
var menuInstaller = colorMenu.AddComponent<ModularAvatarMenuInstaller>();
113+
var menuInstaller = Undo.AddComponent<ModularAvatarMenuInstaller>(colorMenu);
110114

111115
// Add Menu Item
112-
var menuItem = colorMenu.AddComponent<ModularAvatarMenuItem>();
116+
var menuItem = Undo.AddComponent<ModularAvatarMenuItem>(colorMenu);
113117
ConfigureMenuItemAsSubmenu(menuItem, menuName);
114118

115119
return colorMenu;
@@ -123,11 +127,14 @@ public static GameObject CreateColorVariation(Transform parent, string name, int
123127
var colorVariation = new GameObject(name);
124128
colorVariation.transform.SetParent(parent, false);
125129

130+
// Register the created GameObject with Undo system
131+
Undo.RegisterCreatedObjectUndo(colorVariation, "Create Color Variation");
132+
126133
// Add Material Swap component
127-
var materialSwap = colorVariation.AddComponent<ModularAvatarMaterialSwap>();
134+
var materialSwap = Undo.AddComponent<ModularAvatarMaterialSwap>(colorVariation);
128135

129136
// Add Menu Item component
130-
var menuItem = colorVariation.AddComponent<ModularAvatarMenuItem>();
137+
var menuItem = Undo.AddComponent<ModularAvatarMenuItem>(colorVariation);
131138
ConfigureMenuItemAsToggle(menuItem, name, colorNumber, gameObjectName);
132139

133140
return colorVariation;
@@ -141,6 +148,9 @@ public static void SetupMaterialSwap(GameObject swapObject, GameObject rootObjec
141148
var materialSwap = swapObject.GetComponent<ModularAvatarMaterialSwap>();
142149
if (materialSwap == null) return;
143150

151+
// Record the object before modifying it
152+
Undo.RecordObject(materialSwap, "Setup Material Swap");
153+
144154
// Set root reference
145155
materialSwap.Root.Set(rootObject);
146156

@@ -158,7 +168,7 @@ public static void SetupMaterialSwap(GameObject swapObject, GameObject rootObjec
158168
public static ModularAvatarMaterialSwap AddMaterialSwapComponentToObject(GameObject colorVariation, GameObject targetObject, List<(Material from, Material to)> swaps)
159169
{
160170
// Add Material Swap component directly to the colorVariation object
161-
var materialSwap = colorVariation.AddComponent<ModularAvatarMaterialSwap>();
171+
var materialSwap = Undo.AddComponent<ModularAvatarMaterialSwap>(colorVariation);
162172

163173
// Set target object reference
164174
materialSwap.Root.Set(targetObject);

0 commit comments

Comments
 (0)