Skip to content

Commit 5ead3e2

Browse files
committed
don't undo if only converting prefab assets
- also show warning if exporting prefab instance with object overrides - warn that overwriting on export cannot be undone
1 parent 9f5a669 commit 5ead3e2

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

com.unity.formats.fbx/Editor/ConvertToNestedPrefab.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ public static ExportSettings ExportSettings
113113
get { return ExportSettings.instance; }
114114
}
115115

116+
/// <summary>
117+
/// Return true if the given set contains only prefab assets on disk,
118+
/// and nothing from the scene.
119+
/// </summary>
120+
/// <returns></returns>
121+
internal static bool SetContainsOnlyPrefabAssets(Object[] toConvert)
122+
{
123+
foreach (var obj in toConvert)
124+
{
125+
var go = ModelExporter.GetGameObject(obj);
126+
if (go != null && !PrefabUtility.IsPartOfPrefabAsset(go))
127+
{
128+
// return as soon as we find something that is not part of a prefab asset
129+
// on disk
130+
return false;
131+
}
132+
}
133+
return true;
134+
}
135+
116136
/// <summary>
117137
/// Create instantiated model prefabs from a selection of objects.
118138
///
@@ -153,9 +173,14 @@ public static GameObject[] CreateInstantiatedModelPrefab(
153173
return toExport.ToArray();
154174
}
155175

156-
Undo.IncrementCurrentGroup();
157-
int groupIndex = Undo.GetCurrentGroup();
158-
Undo.SetCurrentGroupName(UndoConversionCreateObject);
176+
bool onlyPrefabAssets = ConvertToNestedPrefab.SetContainsOnlyPrefabAssets(unityGameObjectsToConvert);
177+
int groupIndex = -1;
178+
if (!onlyPrefabAssets)
179+
{
180+
Undo.IncrementCurrentGroup();
181+
groupIndex = Undo.GetCurrentGroup();
182+
Undo.SetCurrentGroupName(UndoConversionCreateObject);
183+
}
159184
var converted = new List<GameObject>();
160185
var exportOptions = ExportSettings.instance.ConvertToPrefabSettings.info;
161186
foreach (var go in toExport)
@@ -166,8 +191,11 @@ public static GameObject[] CreateInstantiatedModelPrefab(
166191
converted.Add(convertedGO);
167192
}
168193
}
169-
Undo.CollapseUndoOperations(groupIndex);
170-
Undo.IncrementCurrentGroup();
194+
if (!onlyPrefabAssets && groupIndex >= 0)
195+
{
196+
Undo.CollapseUndoOperations(groupIndex);
197+
Undo.IncrementCurrentGroup();
198+
}
171199
return converted.ToArray();
172200
}
173201

com.unity.formats.fbx/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,15 @@ protected override bool Export()
191191
return true;
192192
}
193193

194-
Undo.IncrementCurrentGroup();
195-
int groupIndex = Undo.GetCurrentGroup();
196-
Undo.SetCurrentGroupName(ConvertToNestedPrefab.UndoConversionCreateObject);
194+
bool onlyPrefabAssets = ConvertToNestedPrefab.SetContainsOnlyPrefabAssets(GetToExport());
195+
int groupIndex = -1;
196+
// no need to undo if we aren't converting anything that's in the scene
197+
if (!onlyPrefabAssets)
198+
{
199+
Undo.IncrementCurrentGroup();
200+
groupIndex = Undo.GetCurrentGroup();
201+
Undo.SetCurrentGroupName(ConvertToNestedPrefab.UndoConversionCreateObject);
202+
}
197203
foreach (var obj in GetToExport())
198204
{
199205
// Convert, automatically choosing a file path that won't clobber any existing files.
@@ -202,8 +208,11 @@ protected override bool Export()
202208
go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.ConvertToPrefabSettings.info
203209
);
204210
}
205-
Undo.CollapseUndoOperations(groupIndex);
206-
Undo.IncrementCurrentGroup();
211+
if (!onlyPrefabAssets && groupIndex >= 0)
212+
{
213+
Undo.CollapseUndoOperations(groupIndex);
214+
Undo.IncrementCurrentGroup();
215+
}
207216
return true;
208217
}
209218

com.unity.formats.fbx/Editor/ExportModelEditorWindow.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,31 @@ public void OnPresetSelectionChanged()
156156
this.Repaint ();
157157
}
158158

159+
protected bool SelectionContainsPrefabInstanceWithAddedObjects()
160+
{
161+
var exportSet = GetToExport();
162+
Queue<Object> queue = new Queue<Object>(exportSet);
163+
while (queue.Count > 0)
164+
{
165+
var go = ModelExporter.GetGameObject(queue.Dequeue());
166+
if (!go)
167+
{
168+
continue;
169+
}
170+
171+
if(PrefabUtility.IsAnyPrefabInstanceRoot(go) && PrefabUtility.GetAddedGameObjects(go).Count > 0)
172+
{
173+
return true;
174+
}
175+
176+
foreach (Transform child in go.transform)
177+
{
178+
queue.Enqueue(child.gameObject);
179+
}
180+
}
181+
return false;
182+
}
183+
159184
protected abstract bool Export ();
160185

161186
/// <summary>
@@ -399,6 +424,13 @@ protected void OnGUI ()
399424
InnerEditor.OnInspectorGUI ();
400425
}
401426

427+
// if we are exporting or converting a prefab with overrides, then show a warning
428+
if (SelectionContainsPrefabInstanceWithAddedObjects())
429+
{
430+
EditorGUILayout.Space();
431+
EditorGUILayout.HelpBox("Prefab instance overrides will be exported", MessageType.Warning, true);
432+
}
433+
402434
GUILayout.FlexibleSpace ();
403435

404436
GUILayout.BeginHorizontal ();
@@ -430,7 +462,7 @@ protected bool OverwriteExistingFile(string filePath){
430462
if (System.IO.File.Exists (filePath)) {
431463
bool overwrite = UnityEditor.EditorUtility.DisplayDialog (
432464
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
433-
string.Format("File {0} already exists.", filePath),
465+
string.Format("File {0} already exists.\nOverwrite cannot be undone", filePath),
434466
"Overwrite", "Cancel");
435467
if (!overwrite) {
436468
if (GUI.changed) {

0 commit comments

Comments
 (0)