No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpriteLibraryAssetInspector.cs 9.2KB

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