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.

SpriteResolverInspector.cs 12KB

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