[unity] Add editor undo support for SkeletonRenderSeparator.

This commit is contained in:
pharan 2017-05-20 10:17:49 +08:00
parent 0bdd3d52e3
commit 69a0644445

View File

@ -31,6 +31,8 @@
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System.Collections.Generic;
using Spine.Unity; using Spine.Unity;
using Spine.Unity.Editor; using Spine.Unity.Editor;
@ -76,7 +78,6 @@ namespace Spine.Unity.Modules {
} }
public override void OnInspectorGUI () { public override void OnInspectorGUI () {
//JOHN: left todo: Add Undo support
var componentRenderers = component.partsRenderers; var componentRenderers = component.partsRenderers;
int totalParts; int totalParts;
@ -169,6 +170,7 @@ namespace Spine.Unity.Modules {
currentRenderers++; currentRenderers++;
} }
int extraRenderersNeeded = totalParts - currentRenderers; int extraRenderersNeeded = totalParts - currentRenderers;
if (component.enabled && component.SkeletonRenderer != null && extraRenderersNeeded > 0) { if (component.enabled && component.SkeletonRenderer != null && extraRenderersNeeded > 0) {
EditorGUILayout.HelpBox(string.Format("Insufficient parts renderers. Some parts will not be rendered."), MessageType.Warning); EditorGUILayout.HelpBox(string.Format("Insufficient parts renderers. Some parts will not be rendered."), MessageType.Warning);
string addMissingLabel = string.Format("Add the missing renderer{1} ({0}) ", extraRenderersNeeded, SpineInspectorUtility.PluralThenS(extraRenderersNeeded)); string addMissingLabel = string.Format("Add the missing renderer{1} ({0}) ", extraRenderersNeeded, SpineInspectorUtility.PluralThenS(extraRenderersNeeded));
@ -186,10 +188,11 @@ namespace Spine.Unity.Modules {
if (componentRenderers.Count > 0) { if (componentRenderers.Count > 0) {
if (GUILayout.Button("Clear Parts Renderers")) { if (GUILayout.Button("Clear Parts Renderers")) {
// Do you really want to destroy all? // Do you really want to destroy all?
if (EditorUtility.DisplayDialog("Destroy Renderers", "Do you really want to destroy all the Parts Renderer GameObjects in the list? (Undo will not work.)", "Destroy", "Cancel")) { Undo.RegisterCompleteObjectUndo(component, "Clear Parts Renderers");
if (EditorUtility.DisplayDialog("Destroy Renderers", "Do you really want to destroy all the Parts Renderer GameObjects in the list?", "Destroy", "Cancel")) {
foreach (var r in componentRenderers) { foreach (var r in componentRenderers) {
if (r != null) if (r != null)
DestroyImmediate(r.gameObject, allowDestroyingAssets: false); Undo.DestroyObjectImmediate(r.gameObject);
} }
componentRenderers.Clear(); componentRenderers.Clear();
// Do you also want to destroy orphans? (You monster.) // Do you also want to destroy orphans? (You monster.)
@ -223,11 +226,12 @@ namespace Spine.Unity.Modules {
if (userClearEntries) componentRenderers.RemoveAll(x => x == null); if (userClearEntries) componentRenderers.RemoveAll(x => x == null);
} }
Undo.RegisterCompleteObjectUndo(component, "Add Parts Renderers");
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
int index = componentRenderers.Count; int index = componentRenderers.Count;
var smr = SkeletonPartsRenderer.NewPartsRendererGameObject(component.transform, index.ToString()); var smr = SkeletonPartsRenderer.NewPartsRendererGameObject(component.transform, index.ToString());
Undo.RegisterCreatedObjectUndo(smr.gameObject, "New Parts Renderer GameObject.");
componentRenderers.Add(smr); componentRenderers.Add(smr);
EditorGUIUtility.PingObject(smr);
// increment renderer sorting order. // increment renderer sorting order.
if (index == 0) continue; if (index == 0) continue;
@ -258,7 +262,7 @@ namespace Spine.Unity.Modules {
if (orphans.Count > 0) { if (orphans.Count > 0) {
if (EditorUtility.DisplayDialog("Destroy Submesh Renderers", "Unassigned renderers were found. Do you want to delete them? (These may belong to another Render Separator in the same hierarchy. If you don't have another Render Separator component in the children of this GameObject, it's likely safe to delete. Warning: This operation cannot be undone.)", "Delete", "Cancel")) { if (EditorUtility.DisplayDialog("Destroy Submesh Renderers", "Unassigned renderers were found. Do you want to delete them? (These may belong to another Render Separator in the same hierarchy. If you don't have another Render Separator component in the children of this GameObject, it's likely safe to delete. Warning: This operation cannot be undone.)", "Delete", "Cancel")) {
foreach (var o in orphans) { foreach (var o in orphans) {
DestroyImmediate(o.gameObject, allowDestroyingAssets: false); Undo.DestroyObjectImmediate(o.gameObject);
} }
} }
} }