SpriteResolverInspector.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.U2D.Animation;
  5. using UnityEngine;
  6. using UnityEngine.Experimental.U2D.Animation;
  7. using UnityEngine.U2D.Animation;
  8. namespace UnityEditor.Experimental.U2D.Animation
  9. {
  10. [CustomEditor(typeof(SpriteResolver))]
  11. internal class SpriteResolverInspector : Editor
  12. {
  13. static class Style
  14. {
  15. public static GUIContent noSpriteLibContainer = EditorGUIUtility.TrTextContent("No Sprite Library Container Component found or Sprite Library has no categories.");
  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. }
  20. struct SpriteCategorySelectionList
  21. {
  22. public string categoryName;
  23. public int categoryNameHash;
  24. public string[] names;
  25. public int[] nameHash;
  26. public Sprite[] sprites;
  27. }
  28. private SerializedProperty m_SpriteCategoryHash;
  29. private SerializedProperty m_SpritelabelHash;
  30. private SpriteSkin m_SpriteSkin;
  31. Dictionary<int, SpriteCategorySelectionList> m_SpriteLibSelection = new Dictionary<int, SpriteCategorySelectionList>();
  32. string[] m_CategorySelection;
  33. int[] m_CategorySelectionHash;
  34. int m_CategorySelectionIndex = 0;
  35. int m_PreviousCategoryHash = 0;
  36. int m_labelSelectionIndex = 0;
  37. int m_PreviouslabelHash = 0;
  38. SpriteSelectorWidget m_SpriteSelectorWidget = new SpriteSelectorWidget();
  39. public void OnEnable()
  40. {
  41. m_SpriteCategoryHash = serializedObject.FindProperty("m_CategoryHash");
  42. m_SpritelabelHash = serializedObject.FindProperty("m_labelHash");
  43. m_SpriteSkin = (target as SpriteResolver).GetComponent<SpriteSkin>();
  44. m_PreviousCategoryHash = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
  45. m_PreviouslabelHash = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
  46. UpdateSpriteLibrary();
  47. }
  48. SpriteResolver spriteResolver { get {return target as SpriteResolver; } }
  49. void UpdateSpriteLibrary()
  50. {
  51. m_SpriteLibSelection.Clear();
  52. int categoryHash = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
  53. int labelHash = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
  54. var spriteLib = spriteResolver.spriteLibrary;
  55. if (spriteLib != null)
  56. {
  57. foreach (var labels in spriteLib.labels)
  58. {
  59. if (!m_SpriteLibSelection.ContainsKey(labels.hash))
  60. {
  61. var nameHash = labels.categoryList.Select(x => x.hash).Distinct().ToArray();
  62. if (nameHash.Length > 0)
  63. {
  64. var selectionList = new SpriteCategorySelectionList()
  65. {
  66. names = nameHash.Select(x =>
  67. {
  68. var v = labels.categoryList.FirstOrDefault(y => y.hash == x);
  69. return v.name;
  70. }).ToArray(),
  71. nameHash = nameHash,
  72. sprites = nameHash.Select(x =>
  73. {
  74. var v = labels.categoryList.FirstOrDefault(y => y.hash == x);
  75. return v.sprite;
  76. }).ToArray(),
  77. categoryName = labels.name,
  78. categoryNameHash = labels.hash
  79. };
  80. m_SpriteLibSelection.Add(labels.hash, selectionList);
  81. }
  82. }
  83. }
  84. }
  85. m_CategorySelection = new string[1 + m_SpriteLibSelection.Keys.Count];
  86. m_CategorySelection[0] = TextContent.none;
  87. m_CategorySelectionHash = new int[1 + m_SpriteLibSelection.Keys.Count];
  88. m_CategorySelectionHash[0] = SpriteLibraryAsset.GetStringHash(TextContent.none);
  89. for (int i = 0; i < m_SpriteLibSelection.Keys.Count; ++i)
  90. {
  91. var selection = m_SpriteLibSelection[m_SpriteLibSelection.Keys.ElementAt(i)];
  92. m_CategorySelection[i + 1] = selection.categoryName;
  93. m_CategorySelectionHash[i + 1] = selection.categoryNameHash;
  94. if (selection.categoryNameHash == categoryHash)
  95. m_CategorySelectionIndex = i + 1;
  96. }
  97. ValidateCategorySelectionIndexValue();
  98. if (m_CategorySelectionIndex > 0)
  99. {
  100. m_SpriteSelectorWidget.UpdateContents(m_SpriteLibSelection[m_CategorySelectionHash[m_CategorySelectionIndex]].sprites);
  101. if (m_SpriteLibSelection.ContainsKey(categoryHash))
  102. {
  103. m_labelSelectionIndex = Array.FindIndex(m_SpriteLibSelection[categoryHash].nameHash, x => x == labelHash);
  104. }
  105. }
  106. spriteResolver.spriteLibChanged = false;
  107. }
  108. void ValidateCategorySelectionIndexValue()
  109. {
  110. if (m_CategorySelectionIndex < 0 || m_CategorySelectionHash.Length <= m_CategorySelectionIndex)
  111. m_CategorySelectionIndex = 0;
  112. }
  113. public override void OnInspectorGUI()
  114. {
  115. serializedObject.Update();
  116. if (spriteResolver.spriteLibChanged)
  117. UpdateSpriteLibrary();
  118. var currentlabelHashValue = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
  119. var currentCategoryHashValue = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
  120. m_CategorySelectionIndex = Array.FindIndex(m_CategorySelectionHash, x => x == currentCategoryHashValue);
  121. ValidateCategorySelectionIndexValue();
  122. if (m_CategorySelection.Length == 1)
  123. {
  124. EditorGUILayout.LabelField(Style.noSpriteLibContainer);
  125. }
  126. else
  127. {
  128. EditorGUI.BeginChangeCheck();
  129. m_CategorySelectionIndex = EditorGUILayout.Popup(Style.categoryLabel, m_CategorySelectionIndex, m_CategorySelection);
  130. if (m_CategorySelectionIndex != 0)
  131. {
  132. var selection = m_SpriteLibSelection[m_CategorySelectionHash[m_CategorySelectionIndex]];
  133. if (selection.names.Length <= 0)
  134. {
  135. EditorGUILayout.LabelField(Style.categoryIsEmptyLabel);
  136. }
  137. else
  138. {
  139. if (m_labelSelectionIndex < 0 || m_labelSelectionIndex >= selection.names.Length)
  140. m_labelSelectionIndex = 0;
  141. m_labelSelectionIndex = EditorGUILayout.Popup(Style.labelLabel, m_labelSelectionIndex, selection.names);
  142. m_labelSelectionIndex = m_SpriteSelectorWidget.ShowGUI(m_labelSelectionIndex);
  143. }
  144. }
  145. if (EditorGUI.EndChangeCheck())
  146. {
  147. currentCategoryHashValue = m_CategorySelectionHash[m_CategorySelectionIndex];
  148. if (m_SpriteLibSelection.ContainsKey(currentCategoryHashValue))
  149. {
  150. var hash = m_SpriteLibSelection[currentCategoryHashValue].nameHash;
  151. if (hash.Length > 0)
  152. {
  153. if (m_labelSelectionIndex < 0 || m_labelSelectionIndex >= hash.Length)
  154. m_labelSelectionIndex = 0;
  155. currentlabelHashValue = m_SpriteLibSelection[currentCategoryHashValue].nameHash[m_labelSelectionIndex];
  156. }
  157. }
  158. m_SpriteCategoryHash.floatValue = SpriteResolver.ConvertIntToFloat(currentCategoryHashValue);
  159. m_SpritelabelHash.floatValue = SpriteResolver.ConvertIntToFloat(currentlabelHashValue);
  160. serializedObject.ApplyModifiedProperties();
  161. var sf = target as SpriteResolver;
  162. if (m_SpriteSkin != null)
  163. m_SpriteSkin.ignoreNextSpriteChange = true;
  164. sf.ResolveSpriteToSpriteRenderer();
  165. }
  166. if (m_PreviousCategoryHash != currentCategoryHashValue)
  167. {
  168. if (m_SpriteLibSelection.ContainsKey(currentCategoryHashValue))
  169. {
  170. m_SpriteSelectorWidget.UpdateContents(m_SpriteLibSelection[currentCategoryHashValue].sprites);
  171. }
  172. m_PreviousCategoryHash = currentCategoryHashValue;
  173. }
  174. if (m_PreviouslabelHash != currentlabelHashValue)
  175. {
  176. if (m_SpriteLibSelection.ContainsKey(currentCategoryHashValue))
  177. m_labelSelectionIndex = Array.FindIndex(m_SpriteLibSelection[currentCategoryHashValue].nameHash, x => x == currentlabelHashValue);
  178. m_PreviouslabelHash = currentlabelHashValue;
  179. }
  180. serializedObject.ApplyModifiedProperties();
  181. if (m_SpriteSelectorWidget.NeedUpdatePreview())
  182. this.Repaint();
  183. }
  184. }
  185. }
  186. }