暫無描述
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.

SpriteResolverInspector.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Scripting.APIUpdating;
  6. using UnityEngine.U2D.Animation;
  7. namespace UnityEditor.U2D.Animation
  8. {
  9. [CustomEditor(typeof(SpriteResolver))]
  10. [MovedFrom("UnityEditor.Experimental.U2D.Animation")]
  11. internal class SpriteResolverInspector : Editor
  12. {
  13. static class Style
  14. {
  15. public static GUIContent categoryLabel = EditorGUIUtility.TrTextContent("Category");
  16. public static GUIContent labelLabel = EditorGUIUtility.TrTextContent("Label");
  17. public static GUIContent categoryIsEmptyLabel = EditorGUIUtility.TrTextContent("Category is Empty");
  18. public static GUIContent noCategory = EditorGUIUtility.TrTextContent("No Category");
  19. public static string[] emptyCategoryDropDownOption = new[] { Style.categoryIsEmptyLabel.text };
  20. }
  21. struct SpriteCategorySelectionList
  22. {
  23. public string categoryName;
  24. public string[] entryNames;
  25. public Sprite[] sprites;
  26. }
  27. SerializedProperty m_SpriteKey;
  28. SerializedProperty m_LabelHash;
  29. SerializedProperty m_CategoryHash;
  30. SpriteSkin m_SpriteSkin;
  31. Dictionary<string, SpriteCategorySelectionList> m_SpriteLibSelection = new Dictionary<string, SpriteCategorySelectionList>();
  32. string[] m_CategorySelection;
  33. int m_CategorySelectionIndex = 0;
  34. int m_LabelSelectionIndex = 0;
  35. string m_PreviousCategoryValue;
  36. string m_PreviousLabelValue;
  37. bool m_IgnoreNextDeserializeCallback;
  38. bool m_ReInitOnNextGUI;
  39. SpriteSelectorWidget m_SpriteSelectorWidget = new SpriteSelectorWidget();
  40. public void OnEnable()
  41. {
  42. m_SpriteKey = serializedObject.FindProperty("m_SpriteKey");
  43. m_LabelHash = serializedObject.FindProperty("m_labelHash");
  44. m_CategoryHash = serializedObject.FindProperty("m_CategoryHash");
  45. m_SpriteSkin = (target as SpriteResolver).GetComponent<SpriteSkin>();
  46. UpdateSpriteLibrary();
  47. spriteResolver.onDeserializedCallback += SpriteResolverDeserializedCallback;
  48. }
  49. void SpriteResolverDeserializedCallback()
  50. {
  51. if (!m_IgnoreNextDeserializeCallback)
  52. {
  53. m_ReInitOnNextGUI = true;
  54. }
  55. }
  56. SpriteResolver spriteResolver => target as SpriteResolver;
  57. void GetCategoryAndLabelStringValue(out string categoryName, out string labelName)
  58. {
  59. categoryName = null;
  60. labelName = null;
  61. var spriteLib = spriteResolver.spriteLibrary;
  62. if (spriteLib != null)
  63. {
  64. int entryHash = SpriteLibraryUtility.Convert32BitTo30BitHash(SpriteResolver.ConvertFloatToInt(m_SpriteKey.floatValue));
  65. spriteLib.GetCategoryAndEntryNameFromHash(entryHash, out categoryName, out labelName);
  66. if (string.IsNullOrEmpty(categoryName) || string.IsNullOrEmpty(labelName))
  67. {
  68. int labelHash = SpriteLibraryUtility.Convert32BitTo30BitHash(SpriteResolver.ConvertFloatToInt(m_LabelHash.floatValue));
  69. int categoryHash = SpriteLibraryUtility.Convert32BitTo30BitHash(SpriteResolver.ConvertFloatToInt(m_CategoryHash.floatValue));
  70. m_SpriteKey.floatValue = SpriteResolver.ConvertCategoryLabelHashToSpriteKey(spriteLib, categoryHash, labelHash);
  71. entryHash = SpriteLibraryUtility.Convert32BitTo30BitHash(SpriteResolver.ConvertFloatToInt(m_SpriteKey.floatValue));
  72. spriteLib.GetCategoryAndEntryNameFromHash(entryHash, out categoryName, out labelName);
  73. }
  74. }
  75. }
  76. void UpdateSpriteLibrary()
  77. {
  78. m_SpriteLibSelection.Clear();
  79. var spriteLib = spriteResolver.spriteLibrary;
  80. string categoryName ="", labelName ="";
  81. if (spriteLib != null)
  82. {
  83. GetCategoryAndLabelStringValue(out categoryName, out labelName);
  84. var enumerator = spriteLib.categoryNames;
  85. foreach(var category in spriteLib.categoryNames)
  86. {
  87. if (!m_SpriteLibSelection.ContainsKey(category))
  88. {
  89. var entries = spriteLib.GetEntryNames(category);
  90. if (entries == null)
  91. entries = new string[0];
  92. var selectionList = new SpriteCategorySelectionList()
  93. {
  94. entryNames = entries.ToArray(),
  95. sprites = entries.Select(x =>
  96. {
  97. return spriteLib.GetSprite(category, x);
  98. }).ToArray(),
  99. categoryName = category,
  100. };
  101. m_SpriteLibSelection.Add(category, selectionList);
  102. }
  103. }
  104. }
  105. m_CategorySelection = new string[1 + m_SpriteLibSelection.Keys.Count];
  106. m_CategorySelection[0] = Style.noCategory.text;
  107. for (int i = 0; i < m_SpriteLibSelection.Keys.Count; ++i)
  108. {
  109. var selection = m_SpriteLibSelection[m_SpriteLibSelection.Keys.ElementAt(i)];
  110. m_CategorySelection[i + 1] = selection.categoryName;
  111. if (selection.categoryName == categoryName)
  112. m_CategorySelectionIndex = i + 1;
  113. }
  114. ValidateCategorySelectionIndexValue();
  115. if (m_CategorySelectionIndex > 0)
  116. {
  117. categoryName = m_CategorySelection[m_CategorySelectionIndex];
  118. m_SpriteSelectorWidget.UpdateContents(
  119. m_SpriteLibSelection[m_CategorySelection[m_CategorySelectionIndex]].sprites);
  120. if (m_SpriteLibSelection.ContainsKey(categoryName))
  121. {
  122. var labelIndex = Array.FindIndex(m_SpriteLibSelection[categoryName].entryNames,
  123. x => x == labelName);
  124. if (labelIndex >= 0 ||
  125. m_SpriteLibSelection[categoryName].entryNames.Length <= m_LabelSelectionIndex)
  126. {
  127. m_LabelSelectionIndex = labelIndex;
  128. }
  129. }
  130. }
  131. else
  132. {
  133. m_SpriteSelectorWidget.UpdateContents(new Sprite[0]);
  134. }
  135. spriteResolver.spriteLibChanged = false;
  136. }
  137. void ValidateCategorySelectionIndexValue()
  138. {
  139. if (m_CategorySelectionIndex < 0 || m_CategorySelection.Length <= m_CategorySelectionIndex)
  140. m_CategorySelectionIndex = 0;
  141. }
  142. public override void OnInspectorGUI()
  143. {
  144. serializedObject.Update();
  145. if (m_ReInitOnNextGUI)
  146. {
  147. m_ReInitOnNextGUI = false;
  148. UpdateSpriteLibrary();
  149. }
  150. if (spriteResolver.spriteLibChanged)
  151. UpdateSpriteLibrary();
  152. GetCategoryAndLabelStringValue(out var currentCategoryValue, out var currentLabelValue);
  153. var catIndex = Array.FindIndex(m_CategorySelection, x => x == currentCategoryValue);
  154. if (catIndex >= 0)
  155. m_CategorySelectionIndex = catIndex;
  156. ValidateCategorySelectionIndexValue();
  157. EditorGUI.BeginChangeCheck();
  158. using (new EditorGUI.DisabledScope(m_CategorySelection.Length <= 1))
  159. m_CategorySelectionIndex = EditorGUILayout.Popup(Style.categoryLabel, m_CategorySelectionIndex, m_CategorySelection);
  160. SpriteCategorySelectionList selection;
  161. m_SpriteLibSelection.TryGetValue(m_CategorySelection[m_CategorySelectionIndex], out selection);
  162. var entryNames = Style.emptyCategoryDropDownOption;
  163. if (selection.entryNames != null)
  164. entryNames = selection.entryNames;
  165. if (m_LabelSelectionIndex < 0 || m_LabelSelectionIndex >= entryNames.Length)
  166. m_LabelSelectionIndex = 0;
  167. using (new EditorGUI.DisabledScope(m_CategorySelectionIndex == 0 || entryNames.Length == 0))
  168. {
  169. if (entryNames.Length == 0)
  170. {
  171. m_LabelSelectionIndex = EditorGUILayout.Popup(Style.labelLabel, 0, new [] {Style.categoryIsEmptyLabel});
  172. }
  173. else
  174. {
  175. m_LabelSelectionIndex = EditorGUILayout.Popup(Style.labelLabel, m_LabelSelectionIndex, entryNames);
  176. }
  177. }
  178. m_LabelSelectionIndex = m_SpriteSelectorWidget.ShowGUI(m_LabelSelectionIndex);
  179. if (EditorGUI.EndChangeCheck())
  180. {
  181. currentCategoryValue = m_CategorySelection[m_CategorySelectionIndex];
  182. if (m_SpriteLibSelection.ContainsKey(currentCategoryValue))
  183. {
  184. var hash = m_SpriteLibSelection[currentCategoryValue].entryNames;
  185. if (hash.Length > 0)
  186. {
  187. if (m_LabelSelectionIndex < 0 || m_LabelSelectionIndex >= hash.Length)
  188. m_LabelSelectionIndex = 0;
  189. currentLabelValue = m_SpriteLibSelection[currentCategoryValue].entryNames[m_LabelSelectionIndex];
  190. }
  191. }
  192. m_SpriteKey.floatValue = SpriteResolver.ConvertIntToFloat(SpriteLibrary.GetHashForCategoryAndEntry(currentCategoryValue, currentLabelValue));
  193. ApplyModifiedProperty();
  194. var sf = target as SpriteResolver;
  195. if (m_SpriteSkin != null)
  196. m_SpriteSkin.ignoreNextSpriteChange = true;
  197. sf.ResolveSpriteToSpriteRenderer();
  198. }
  199. if (m_PreviousCategoryValue != currentCategoryValue)
  200. {
  201. if (!string.IsNullOrEmpty(currentCategoryValue))
  202. {
  203. if (m_SpriteLibSelection.ContainsKey(currentCategoryValue))
  204. {
  205. m_SpriteSelectorWidget.UpdateContents(m_SpriteLibSelection[currentCategoryValue].sprites);
  206. }
  207. else
  208. m_SpriteSelectorWidget.UpdateContents(new Sprite[0]);
  209. this.Repaint();
  210. }
  211. m_PreviousCategoryValue = currentCategoryValue;
  212. }
  213. if (!string.IsNullOrEmpty(currentLabelValue) && m_PreviousLabelValue != currentLabelValue)
  214. {
  215. if (m_SpriteLibSelection.ContainsKey(currentCategoryValue))
  216. m_LabelSelectionIndex = Array.FindIndex(m_SpriteLibSelection[currentCategoryValue].entryNames, x => x == currentLabelValue);
  217. m_PreviousLabelValue = currentLabelValue;
  218. }
  219. ApplyModifiedProperty();
  220. if (m_SpriteSelectorWidget.NeedUpdatePreview())
  221. this.Repaint();
  222. }
  223. void ApplyModifiedProperty()
  224. {
  225. m_IgnoreNextDeserializeCallback = true;
  226. serializedObject.ApplyModifiedProperties();
  227. m_IgnoreNextDeserializeCallback = false;
  228. }
  229. }
  230. }