Skip to content

Commit b6c78e8

Browse files
committed
Add destroy button
1 parent a8efbe2 commit b6c78e8

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

UInspectorPlus/InspectorPlus.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private void OnGUI() {
5757
if (GUI.changed)
5858
IterateDrawers<ComponentMethodDrawer>(methodDrawer => methodDrawer.Filter = searchText);
5959
GUILayout.Space(8);
60+
EditorGUI.BeginDisabledGroup(instanceIds == null || instanceIds.Length == 0);
61+
if (GUILayout.Button(EditorGUIUtility.IconContent("TreeEditor.Trash", "Destroy Selection"),
62+
EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
63+
DestroyAll();
64+
EditorGUI.EndDisabledGroup();
6065
GUILayout.EndHorizontal();
6166
GUI.changed = false;
6267
scrollPos = GUILayout.BeginScrollView(scrollPos);
@@ -195,5 +200,56 @@ private void UpdateValues(bool updateProps) {
195200
drawerGroup.UpdateValues(updateProps);
196201
Repaint();
197202
}
203+
204+
private void DestroyAll() {
205+
int[] instanceIds = this.instanceIds;
206+
if (instanceIds == null || instanceIds.Length == 0)
207+
return;
208+
bool deleteAll = false, showError = true;
209+
HashSet<int> remainObjects = new HashSet<int>(instanceIds);
210+
foreach (int id in instanceIds) {
211+
try {
212+
UnityObject obj = EditorUtility.InstanceIDToObject(id);
213+
if (obj == null) continue;
214+
bool deleteThis = deleteAll;
215+
if (!deleteAll)
216+
switch (EditorUtility.DisplayDialogComplex(
217+
"Destroy object",
218+
string.Format("Destroy {0} {1} (Instance ID: {2})?", obj.GetType(), obj.name, id),
219+
"Yes", "No", "Yes to all"
220+
)) {
221+
case 0: deleteThis = true; break;
222+
case 1: deleteThis = false; break;
223+
case 2: {
224+
deleteAll = true;
225+
deleteThis = true;
226+
break;
227+
}
228+
}
229+
if (deleteThis) {
230+
DestroyImmediate(obj);
231+
remainObjects.Remove(id);
232+
}
233+
} catch (Exception ex) {
234+
Debug.LogException(ex);
235+
if (showError)
236+
switch (EditorUtility.DisplayDialogComplex(
237+
string.Format("Error while destroying object {0}", id),
238+
ex.Message,
239+
"Continue", "Stop", "Don't show again"
240+
)) {
241+
case 0: break;
242+
case 1: return;
243+
case 2: showError = false; break;
244+
}
245+
}
246+
}
247+
int[] nextInstanceIds = this.instanceIds;
248+
if (nextInstanceIds.Length != remainObjects.Count)
249+
this.instanceIds = nextInstanceIds = new int[remainObjects.Count];
250+
remainObjects.CopyTo(nextInstanceIds);
251+
Selection.instanceIDs = this.instanceIds;
252+
OnSelectionChange();
253+
}
198254
}
199255
}

0 commit comments

Comments
 (0)