PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/Spine/Editor/spine-unity/Modules/SkeletonRenderSeparator/Editor/SkeletonRenderSeparatorInspector.cs

https://gitlab.com/hoangduy09100/falling-box
C# | 323 lines | 228 code | 49 blank | 46 comment | 80 complexity | ce02c2654c6569620ce9c04fcde6b975 MD5 | raw file
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
  30. #define NEW_PREFAB_SYSTEM
  31. #endif
  32. using UnityEngine;
  33. using UnityEditor;
  34. using System.Collections.Generic;
  35. using Spine.Unity;
  36. using Spine.Unity.Editor;
  37. namespace Spine.Unity.Examples {
  38. [CustomEditor(typeof(SkeletonRenderSeparator))]
  39. public class SkeletonRenderSeparatorInspector : UnityEditor.Editor {
  40. SkeletonRenderSeparator component;
  41. // Properties
  42. SerializedProperty skeletonRenderer_, copyPropertyBlock_, copyMeshRendererFlags_, partsRenderers_;
  43. static bool partsRenderersExpanded = false;
  44. // For separator field.
  45. SerializedObject skeletonRendererSerializedObject;
  46. SerializedProperty separatorNamesProp;
  47. static bool skeletonRendererExpanded = true;
  48. bool slotsReapplyRequired = false;
  49. bool partsRendererInitRequired = false;
  50. void OnEnable () {
  51. if (component == null)
  52. component = target as SkeletonRenderSeparator;
  53. skeletonRenderer_ = serializedObject.FindProperty("skeletonRenderer");
  54. copyPropertyBlock_ = serializedObject.FindProperty("copyPropertyBlock");
  55. copyMeshRendererFlags_ = serializedObject.FindProperty("copyMeshRendererFlags");
  56. var partsRenderers = component.partsRenderers;
  57. partsRenderers_ = serializedObject.FindProperty("partsRenderers");
  58. partsRenderers_.isExpanded = partsRenderersExpanded || // last state
  59. partsRenderers.Contains(null) || // null items found
  60. partsRenderers.Count < 1 || // no parts renderers
  61. (skeletonRenderer_.objectReferenceValue != null && SkeletonRendererSeparatorCount + 1 > partsRenderers.Count); // not enough parts renderers
  62. }
  63. int SkeletonRendererSeparatorCount {
  64. get {
  65. if (Application.isPlaying)
  66. return component.SkeletonRenderer.separatorSlots.Count;
  67. else
  68. return separatorNamesProp == null ? 0 : separatorNamesProp.arraySize;
  69. }
  70. }
  71. public override void OnInspectorGUI () {
  72. // Restore mesh part for undo logic after undo of "Add Parts Renderer".
  73. // Triggers regeneration and assignment of the mesh filter's mesh.
  74. bool isMeshFilterAlwaysNull = false;
  75. #if UNITY_EDITOR && NEW_PREFAB_SYSTEM
  76. // Don't store mesh or material at the prefab, otherwise it will permanently reload
  77. var prefabType = UnityEditor.PrefabUtility.GetPrefabAssetType(component);
  78. if (UnityEditor.PrefabUtility.IsPartOfPrefabAsset(component) &&
  79. (prefabType == UnityEditor.PrefabAssetType.Regular || prefabType == UnityEditor.PrefabAssetType.Variant)) {
  80. isMeshFilterAlwaysNull = true;
  81. }
  82. #endif
  83. if (!isMeshFilterAlwaysNull && component.GetComponent<MeshFilter>() && component.GetComponent<MeshFilter>().sharedMesh == null) {
  84. component.OnDisable();
  85. component.OnEnable();
  86. }
  87. var componentRenderers = component.partsRenderers;
  88. int totalParts;
  89. using (new SpineInspectorUtility.LabelWidthScope()) {
  90. bool componentEnabled = component.enabled;
  91. bool checkBox = EditorGUILayout.Toggle("Enable Separator", componentEnabled);
  92. if (checkBox != componentEnabled)
  93. component.enabled = checkBox;
  94. if (component.SkeletonRenderer.disableRenderingOnOverride && !component.enabled)
  95. EditorGUILayout.HelpBox("By default, SkeletonRenderer's MeshRenderer is disabled while the SkeletonRenderSeparator takes over rendering. It is re-enabled when SkeletonRenderSeparator is disabled.", MessageType.Info);
  96. EditorGUILayout.PropertyField(copyPropertyBlock_);
  97. EditorGUILayout.PropertyField(copyMeshRendererFlags_);
  98. }
  99. // SkeletonRenderer Box
  100. using (new SpineInspectorUtility.BoxScope(false)) {
  101. // Fancy SkeletonRenderer foldout reference field
  102. {
  103. EditorGUI.indentLevel++;
  104. EditorGUI.BeginChangeCheck();
  105. var foldoutSkeletonRendererRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
  106. EditorGUI.PropertyField(foldoutSkeletonRendererRect, skeletonRenderer_);
  107. if (EditorGUI.EndChangeCheck())
  108. serializedObject.ApplyModifiedProperties();
  109. if (component.SkeletonRenderer != null) {
  110. skeletonRendererExpanded = EditorGUI.Foldout(foldoutSkeletonRendererRect, skeletonRendererExpanded, "");
  111. }
  112. EditorGUI.indentLevel--;
  113. }
  114. int separatorCount = 0;
  115. EditorGUI.BeginChangeCheck();
  116. if (component.SkeletonRenderer != null) {
  117. // Separators from SkeletonRenderer
  118. {
  119. bool skeletonRendererMismatch = skeletonRendererSerializedObject != null && skeletonRendererSerializedObject.targetObject != component.SkeletonRenderer;
  120. if (separatorNamesProp == null || skeletonRendererMismatch) {
  121. if (component.SkeletonRenderer != null) {
  122. skeletonRendererSerializedObject = new SerializedObject(component.SkeletonRenderer);
  123. separatorNamesProp = skeletonRendererSerializedObject.FindProperty("separatorSlotNames");
  124. separatorNamesProp.isExpanded = true;
  125. }
  126. }
  127. if (separatorNamesProp != null) {
  128. if (skeletonRendererExpanded) {
  129. EditorGUI.indentLevel++;
  130. SkeletonRendererInspector.SeparatorsField(separatorNamesProp);
  131. EditorGUI.indentLevel--;
  132. }
  133. separatorCount = this.SkeletonRendererSeparatorCount;
  134. }
  135. }
  136. if (SkeletonRendererSeparatorCount == 0) {
  137. EditorGUILayout.HelpBox("Separators are empty. Change the size to 1 and choose a slot if you want the render to be separated.", MessageType.Info);
  138. }
  139. }
  140. if (EditorGUI.EndChangeCheck()) {
  141. skeletonRendererSerializedObject.ApplyModifiedProperties();
  142. if (!Application.isPlaying)
  143. slotsReapplyRequired = true;
  144. }
  145. totalParts = separatorCount + 1;
  146. var counterStyle = skeletonRendererExpanded ? EditorStyles.label : EditorStyles.miniLabel;
  147. EditorGUILayout.LabelField(string.Format("{0}: separates into {1}.", SpineInspectorUtility.Pluralize(separatorCount, "separator", "separators"), SpineInspectorUtility.Pluralize(totalParts, "part", "parts") ), counterStyle);
  148. }
  149. // Parts renderers
  150. using (new SpineInspectorUtility.BoxScope(false)) {
  151. EditorGUI.indentLevel++;
  152. EditorGUILayout.PropertyField(this.partsRenderers_, true);
  153. EditorGUI.indentLevel--;
  154. // Null items warning
  155. bool nullItemsFound = componentRenderers.Contains(null);
  156. if (nullItemsFound)
  157. EditorGUILayout.HelpBox("Some items in the parts renderers list are null and may cause problems.\n\nYou can right-click on that element and choose 'Delete Array Element' to remove it.", MessageType.Warning);
  158. // (Button) Match Separators count
  159. if (separatorNamesProp != null) {
  160. int currentRenderers = 0;
  161. foreach (var r in componentRenderers) {
  162. if (r != null)
  163. currentRenderers++;
  164. }
  165. int extraRenderersNeeded = totalParts - currentRenderers;
  166. if (component.enabled && component.SkeletonRenderer != null && extraRenderersNeeded > 0) {
  167. EditorGUILayout.HelpBox(string.Format("Insufficient parts renderers. Some parts will not be rendered."), MessageType.Warning);
  168. string addMissingLabel = string.Format("Add the missing renderer{1} ({0}) ", extraRenderersNeeded, SpineInspectorUtility.PluralThenS(extraRenderersNeeded));
  169. if (GUILayout.Button(addMissingLabel, GUILayout.Height(30f))) {
  170. AddPartsRenderer(extraRenderersNeeded);
  171. DetectOrphanedPartsRenderers(component);
  172. partsRendererInitRequired = true;
  173. }
  174. }
  175. }
  176. if (partsRenderers_.isExpanded != partsRenderersExpanded) partsRenderersExpanded = partsRenderers_.isExpanded;
  177. if (partsRenderers_.isExpanded) {
  178. using (new EditorGUILayout.HorizontalScope()) {
  179. // (Button) Destroy Renderers button
  180. if (componentRenderers.Count > 0) {
  181. if (GUILayout.Button("Clear Parts Renderers")) {
  182. // Do you really want to destroy all?
  183. Undo.RegisterCompleteObjectUndo(component, "Clear Parts Renderers");
  184. if (EditorUtility.DisplayDialog("Destroy Renderers", "Do you really want to destroy all the Parts Renderer GameObjects in the list?", "Destroy", "Cancel")) {
  185. foreach (var r in componentRenderers) {
  186. if (r != null)
  187. Undo.DestroyObjectImmediate(r.gameObject);
  188. }
  189. componentRenderers.Clear();
  190. // Do you also want to destroy orphans? (You monster.)
  191. DetectOrphanedPartsRenderers(component);
  192. }
  193. }
  194. }
  195. // (Button) Add Part Renderer button
  196. if (GUILayout.Button("Add Parts Renderer")) {
  197. AddPartsRenderer(1);
  198. partsRendererInitRequired = true;
  199. }
  200. }
  201. }
  202. }
  203. serializedObject.ApplyModifiedProperties();
  204. if (partsRendererInitRequired) {
  205. Undo.RegisterCompleteObjectUndo(component.GetComponent<MeshRenderer>(), "Add Parts Renderers");
  206. component.OnEnable();
  207. partsRendererInitRequired = false;
  208. }
  209. if (slotsReapplyRequired && UnityEngine.Event.current.type == EventType.Repaint) {
  210. component.SkeletonRenderer.ReapplySeparatorSlotNames();
  211. component.SkeletonRenderer.LateUpdate();
  212. SceneView.RepaintAll();
  213. slotsReapplyRequired = false;
  214. }
  215. }
  216. public void AddPartsRenderer (int count) {
  217. var componentRenderers = component.partsRenderers;
  218. bool emptyFound = componentRenderers.Contains(null);
  219. if (emptyFound) {
  220. bool userClearEntries = EditorUtility.DisplayDialog("Empty entries found", "Null entries found. Do you want to remove null entries before adding the new renderer? ", "Clear Empty Entries", "Don't Clear");
  221. if (userClearEntries) componentRenderers.RemoveAll(x => x == null);
  222. }
  223. Undo.RegisterCompleteObjectUndo(component, "Add Parts Renderers");
  224. for (int i = 0; i < count; i++) {
  225. int index = componentRenderers.Count;
  226. var smr = SkeletonPartsRenderer.NewPartsRendererGameObject(component.transform, index.ToString());
  227. Undo.RegisterCreatedObjectUndo(smr.gameObject, "New Parts Renderer GameObject.");
  228. componentRenderers.Add(smr);
  229. // increment renderer sorting order.
  230. if (index == 0) continue;
  231. var prev = componentRenderers[index - 1]; if (prev == null) continue;
  232. var prevMeshRenderer = prev.GetComponent<MeshRenderer>();
  233. var currentMeshRenderer = smr.GetComponent<MeshRenderer>();
  234. if (prevMeshRenderer == null || currentMeshRenderer == null) continue;
  235. int prevSortingLayer = prevMeshRenderer.sortingLayerID;
  236. int prevSortingOrder = prevMeshRenderer.sortingOrder;
  237. currentMeshRenderer.sortingLayerID = prevSortingLayer;
  238. currentMeshRenderer.sortingOrder = prevSortingOrder + SkeletonRenderSeparator.DefaultSortingOrderIncrement;
  239. }
  240. }
  241. /// <summary>Detects orphaned parts renderers and offers to delete them.</summary>
  242. public void DetectOrphanedPartsRenderers (SkeletonRenderSeparator component) {
  243. var children = component.GetComponentsInChildren<SkeletonPartsRenderer>();
  244. var orphans = new System.Collections.Generic.List<SkeletonPartsRenderer>();
  245. foreach (var r in children) {
  246. if (!component.partsRenderers.Contains(r))
  247. orphans.Add(r);
  248. }
  249. if (orphans.Count > 0) {
  250. 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")) {
  251. foreach (var o in orphans) {
  252. Undo.DestroyObjectImmediate(o.gameObject);
  253. }
  254. }
  255. }
  256. }
  257. #region SkeletonRenderer Context Menu Item
  258. [MenuItem ("CONTEXT/SkeletonRenderer/Add Skeleton Render Separator")]
  259. static void AddRenderSeparatorComponent (MenuCommand cmd) {
  260. var skeletonRenderer = cmd.context as SkeletonRenderer;
  261. var newComponent = skeletonRenderer.gameObject.AddComponent<SkeletonRenderSeparator>();
  262. Undo.RegisterCreatedObjectUndo(newComponent, "Add SkeletonRenderSeparator");
  263. }
  264. // Validate
  265. [MenuItem ("CONTEXT/SkeletonRenderer/Add Skeleton Render Separator", true)]
  266. static bool ValidateAddRenderSeparatorComponent (MenuCommand cmd) {
  267. var skeletonRenderer = cmd.context as SkeletonRenderer;
  268. var separator = skeletonRenderer.GetComponent<SkeletonRenderSeparator>();
  269. bool separatorNotOnObject = separator == null;
  270. return separatorNotOnObject;
  271. }
  272. #endregion
  273. }
  274. }