Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SpriteLibraryAssetInspector.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using UnityEditor.Callbacks;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. using UnityEngine.Scripting.APIUpdating;
  5. using UnityEngine.U2D.Animation;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. [CustomEditor(typeof(SpriteLibraryAsset))]
  9. [MovedFrom("UnityEditor.Experimental.U2D.Animation")]
  10. internal class SpriteLibraryAssetInspector : Editor
  11. {
  12. static class Style
  13. {
  14. public static GUIContent duplicateWarningText = EditorGUIUtility.TrTextContent("Duplicate name found or name hash clashes. Please use a different name");
  15. public static GUIContent duplicateWarning = EditorGUIUtility.TrIconContent("console.warnicon.sml", duplicateWarningText.text);
  16. public static GUIContent nameLabel = new GUIContent(TextContent.label);
  17. public static string categoryListLabel = TextContent.categoryList;
  18. public static int lineSpacing = 3;
  19. }
  20. SerializedProperty m_Labels;
  21. ReorderableList m_LabelReorderableList;
  22. bool m_UpdateHash = false;
  23. static readonly float k_ElementHeight = EditorGUIUtility.singleLineHeight * 3;
  24. protected override bool ShouldHideOpenButton() => true;
  25. [OnOpenAsset(OnOpenAssetAttributeMode.Execute)]
  26. public static bool ExecuteOpenSpriteLibraryAsset(int instanceID)
  27. {
  28. if (EditorUtility.InstanceIDToObject(instanceID) is SpriteLibraryAsset)
  29. return true;
  30. return false;
  31. }
  32. public void OnEnable()
  33. {
  34. m_Labels = serializedObject.FindProperty("m_Labels");
  35. m_LabelReorderableList = new ReorderableList(serializedObject, m_Labels, true, false, true, true);
  36. SetupOrderList();
  37. }
  38. public void OnDisable()
  39. {
  40. var sla = target as SpriteLibraryAsset;
  41. if (sla != null)
  42. sla.UpdateHashes();
  43. }
  44. float GetElementHeight(int index)
  45. {
  46. var property = m_Labels.GetArrayElementAtIndex(index);
  47. var spriteListProp = property.FindPropertyRelative("m_CategoryList");
  48. if (spriteListProp.isExpanded)
  49. return (spriteListProp.arraySize + 1) * (EditorGUIUtility.singleLineHeight + Style.lineSpacing) + k_ElementHeight;
  50. return k_ElementHeight;
  51. }
  52. void DrawElement(Rect rect, int index, bool selected, bool focused)
  53. {
  54. var property = m_Labels.GetArrayElementAtIndex(index);
  55. var catRect = new Rect(rect.x, rect.y, rect.width - k_ElementHeight, EditorGUIUtility.singleLineHeight);
  56. var vaRect = new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight, rect.width - k_ElementHeight, EditorGUIUtility.singleLineHeight);
  57. var categoryProp = property.FindPropertyRelative("m_Name");
  58. var spriteListProp = property.FindPropertyRelative("m_CategoryList");
  59. EditorGUI.BeginChangeCheck();
  60. var newCatName = EditorGUI.DelayedTextField(catRect, categoryProp.stringValue);
  61. if (EditorGUI.EndChangeCheck())
  62. {
  63. newCatName = newCatName.Trim();
  64. m_UpdateHash = true;
  65. if (categoryProp.stringValue != newCatName)
  66. {
  67. // Check if this nameLabel is already taken
  68. if (!IsNameInUsed(newCatName, m_Labels, "m_Name", 0))
  69. categoryProp.stringValue = newCatName;
  70. else
  71. Debug.LogWarning(Style.duplicateWarningText.text);
  72. }
  73. }
  74. spriteListProp.isExpanded = EditorGUI.Foldout(vaRect, spriteListProp.isExpanded, Style.categoryListLabel);
  75. if (spriteListProp.isExpanded)
  76. {
  77. EditorGUI.indentLevel++;
  78. var indentedRect = EditorGUI.IndentedRect(vaRect);
  79. var labelWidth = EditorGUIUtility.labelWidth;
  80. EditorGUIUtility.labelWidth = 40 + indentedRect.x - vaRect.x;
  81. indentedRect.y += EditorGUIUtility.singleLineHeight + Style.lineSpacing;
  82. var sizeRect = indentedRect;
  83. int size = EditorGUI.IntField(sizeRect, TextContent.size, spriteListProp.arraySize);
  84. if (size != spriteListProp.arraySize && size >= 0)
  85. spriteListProp.arraySize = size;
  86. indentedRect.y += EditorGUIUtility.singleLineHeight + Style.lineSpacing;
  87. DrawSpriteListProperty(indentedRect, spriteListProp);
  88. EditorGUIUtility.labelWidth = labelWidth;
  89. EditorGUI.indentLevel--;
  90. }
  91. }
  92. void DrawSpriteListProperty(Rect rect, SerializedProperty spriteListProp)
  93. {
  94. for (int i = 0; i < spriteListProp.arraySize; ++i)
  95. {
  96. var element = spriteListProp.GetArrayElementAtIndex(i);
  97. EditorGUI.BeginChangeCheck();
  98. var oldName = element.FindPropertyRelative("m_Name").stringValue;
  99. var nameRect = new Rect(rect.x, rect.y, rect.width / 2, EditorGUIUtility.singleLineHeight);
  100. bool nameDuplicate = IsNameInUsed(oldName, spriteListProp, "m_Name", 1);
  101. if (nameDuplicate)
  102. {
  103. nameRect.width -= 20;
  104. }
  105. var newName = EditorGUI.DelayedTextField(
  106. nameRect,
  107. Style.nameLabel,
  108. oldName);
  109. if (nameDuplicate)
  110. {
  111. nameRect.x += nameRect.width;
  112. nameRect.width = 20;
  113. GUI.Label(nameRect, Style.duplicateWarning);
  114. }
  115. if (EditorGUI.EndChangeCheck())
  116. {
  117. newName = newName.Trim();
  118. element.FindPropertyRelative("m_Name").stringValue = newName;
  119. }
  120. EditorGUI.PropertyField(new Rect(rect.x + rect.width / 2 + 5, rect.y, rect.width / 2, EditorGUIUtility.singleLineHeight),
  121. element.FindPropertyRelative("m_Sprite"));
  122. rect.y += EditorGUIUtility.singleLineHeight + Style.lineSpacing;
  123. }
  124. }
  125. public override void OnInspectorGUI()
  126. {
  127. serializedObject.Update();
  128. EditorGUI.BeginChangeCheck();
  129. if (EditorGUI.EndChangeCheck())
  130. SetupOrderList();
  131. m_UpdateHash = false;
  132. m_LabelReorderableList.DoLayoutList();
  133. serializedObject.ApplyModifiedProperties();
  134. if (m_UpdateHash)
  135. (target as SpriteLibraryAsset).UpdateHashes();
  136. }
  137. bool IsNameInUsed(string name, SerializedProperty property, string propertyField, int threshold)
  138. {
  139. int count = 0;
  140. var nameHash = SpriteLibraryUtility.GetStringHash(name);
  141. for (int i = 0; i < property.arraySize; ++i)
  142. {
  143. var sp = property.GetArrayElementAtIndex(i);
  144. var otherName = sp.FindPropertyRelative(propertyField).stringValue;
  145. var otherNameHash = SpriteLibraryUtility.GetStringHash(otherName);
  146. if (otherName == name || nameHash == otherNameHash)
  147. {
  148. count++;
  149. if (count > threshold)
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. void OnAddCallback(ReorderableList list)
  156. {
  157. var oldSize = m_Labels.arraySize;
  158. m_Labels.arraySize += 1;
  159. const string kNewCatName = "New Category";
  160. string newCatName = kNewCatName;
  161. int catNameIncrement = 1;
  162. while (true)
  163. {
  164. if (IsNameInUsed(newCatName, m_Labels, "m_Name", 0))
  165. newCatName = string.Format("{0} {1}", kNewCatName, catNameIncrement++);
  166. else
  167. break;
  168. }
  169. var sp = m_Labels.GetArrayElementAtIndex(oldSize);
  170. sp.FindPropertyRelative("m_Name").stringValue = newCatName;
  171. sp.FindPropertyRelative("m_Hash").intValue = SpriteLibraryUtility.GetStringHash(newCatName);
  172. }
  173. private void SetupOrderList()
  174. {
  175. m_LabelReorderableList.drawElementCallback = DrawElement;
  176. m_LabelReorderableList.elementHeight = k_ElementHeight;
  177. m_LabelReorderableList.elementHeightCallback = GetElementHeight;
  178. m_LabelReorderableList.onAddCallback = OnAddCallback;
  179. }
  180. }
  181. }