SpriteLibraryDataProvider.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Experimental.U2D.Animation;
  5. using UnityEngine.Serialization;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. /// <summary>
  9. /// Structure that defines a Sprite Library Category Label
  10. /// </summary>
  11. [Serializable]
  12. public struct SpriteCategoryLabel
  13. {
  14. [SerializeField]
  15. string m_Name;
  16. [SerializeField]
  17. string m_SpriteId;
  18. /// <summary>
  19. /// Get and set the name for the Sprite label
  20. /// </summary>
  21. public string name
  22. {
  23. get { return m_Name; }
  24. set { m_Name = value; }
  25. }
  26. /// <summary>
  27. /// Get and set the Sprite Id.
  28. /// </summary>
  29. public string spriteId
  30. {
  31. get { return m_SpriteId; }
  32. set { m_SpriteId = value; }
  33. }
  34. }
  35. /// <summary>
  36. /// Structure that defines a Sprite Library Category.
  37. /// </summary>
  38. [Serializable]
  39. public struct SpriteCategory
  40. {
  41. [SerializeField]
  42. [FormerlySerializedAs("name")]
  43. string m_Name;
  44. [SerializeField]
  45. List<SpriteCategoryLabel> m_Labels;
  46. /// <summary>
  47. /// Get and set the name for the Sprite Category
  48. /// </summary>
  49. public string name
  50. {
  51. get { return m_Name; }
  52. set { m_Name = value; }
  53. }
  54. /// <summary>
  55. /// Get and set the Sprites registered to this category.
  56. /// </summary>
  57. public List<SpriteCategoryLabel> labels
  58. {
  59. get { return m_Labels; }
  60. set { m_Labels = value; }
  61. }
  62. }
  63. /// <summary>
  64. /// A structure to hold a collection of SpriteCategory
  65. /// </summary>
  66. [Serializable]
  67. public struct SpriteCategoryList
  68. {
  69. [SerializeField]
  70. [FormerlySerializedAs("categories")]
  71. List<SpriteCategory> m_Categories;
  72. /// <summary>
  73. /// Get or set the a list of SpriteCategory
  74. /// </summary>
  75. public List<SpriteCategory> categories
  76. {
  77. get { return m_Categories; }
  78. set { m_Categories = value; }
  79. }
  80. }
  81. internal class SpriteCategoryListCacheObject : SkinningObject
  82. {
  83. [SerializeField]
  84. public List<SpriteCategory> categories = new List<SpriteCategory>();
  85. public void CopyFrom(SpriteCategoryList categoryList)
  86. {
  87. categories.Clear();
  88. foreach (var cat in categoryList.categories)
  89. {
  90. var spriteLibCategory = new SpriteCategory()
  91. {
  92. name = cat.name,
  93. labels = new List<SpriteCategoryLabel>(cat.labels)
  94. };
  95. categories.Add(spriteLibCategory);
  96. }
  97. }
  98. public SpriteCategoryList ToSpriteLibrary()
  99. {
  100. var spriteLibrary = new SpriteCategoryList();
  101. spriteLibrary.categories = new List<SpriteCategory>();
  102. foreach (var cat in categories)
  103. {
  104. var spriteLibCategory = new SpriteCategory()
  105. {
  106. name = cat.name,
  107. labels = new List<SpriteCategoryLabel>(cat.labels)
  108. };
  109. spriteLibrary.categories.Add(spriteLibCategory);
  110. }
  111. return spriteLibrary;
  112. }
  113. public void RemoveSpriteFromCategory(string sprite)
  114. {
  115. for (int i = 0; i < categories.Count; ++i)
  116. {
  117. var index = categories[i].labels.FindIndex(x => x.spriteId == sprite);
  118. if (index != -1)
  119. categories[i].labels.RemoveAt(index);
  120. }
  121. }
  122. public void AddSpriteToCategory(string category, SpriteCategoryLabel label)
  123. {
  124. if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(label.name))
  125. {
  126. // Remove sprite from name
  127. RemoveSpriteFromCategory(label.spriteId);
  128. }
  129. else
  130. {
  131. //find cateogry
  132. var categoryIndex = categories.FindIndex(x => x.name == category);
  133. if (categoryIndex == -1)
  134. {
  135. // check if the hash might clash
  136. var hash = SpriteLibraryAsset.GetStringHash(category);
  137. if (categories.FindIndex(x => x.name != category && SpriteLibraryAsset.GetStringHash(x.name) == hash) != -1)
  138. {
  139. Debug.LogError("Unable to add Sprite to new Category due to name hash clash");
  140. return;
  141. }
  142. }
  143. var insertCategory = categoryIndex != -1 ? categories[categoryIndex] : new SpriteCategory() { name = category, labels = new List<SpriteCategoryLabel>() };
  144. if (insertCategory.labels.FindIndex(x => x.spriteId == label.spriteId) == -1)
  145. insertCategory.labels.Add(label);
  146. // now remove everything that has this sprite
  147. foreach (var cat in categories)
  148. {
  149. if (cat.name != category)
  150. cat.labels.RemoveAll(x => x.spriteId == label.spriteId);
  151. }
  152. if (categoryIndex == -1)
  153. categories.Add(insertCategory);
  154. else
  155. categories[categoryIndex] = insertCategory;
  156. }
  157. }
  158. public void ChangeSpriteLabelName(string labelname, string sprite)
  159. {
  160. // find name which contain sprite
  161. var categoryIndex = -1;
  162. var spriteIndex = -1;
  163. for (int i = 0; i < categories.Count; ++i)
  164. {
  165. spriteIndex = categories[i].labels.FindIndex(x => x.spriteId == sprite);
  166. if (spriteIndex != -1)
  167. {
  168. categoryIndex = i;
  169. break;
  170. }
  171. }
  172. if (categoryIndex != -1 && spriteIndex != -1)
  173. {
  174. var cat = categories[categoryIndex];
  175. if (string.IsNullOrEmpty(labelname))
  176. {
  177. cat.labels.RemoveAt(spriteIndex);
  178. }
  179. else
  180. {
  181. var label = cat.labels[spriteIndex];
  182. label.name = labelname;
  183. cat.labels[spriteIndex] = label;
  184. }
  185. }
  186. }
  187. }
  188. /// <summary>An interface that allows Sprite Editor Modules to edit Sprite Library data for user custom importer.</summary>
  189. /// <remarks>Implement this interface for [[ScriptedImporter]] to leverage on Sprite Editor Modules to edit Sprite Library data.</remarks>
  190. public interface ISpriteLibDataProvider
  191. {
  192. /// <summary>
  193. /// Returns the SpriteCategoryList structure that represents the Sprite Library data.
  194. /// </summary>
  195. /// <returns>SpriteCategoryList data</returns>
  196. SpriteCategoryList GetSpriteCategoryList();
  197. /// <summary>
  198. /// Sets the SpriteCategoryList structure that represents the Sprite Library data to the data provider
  199. /// </summary>
  200. /// <param name="spriteCategoryList">Data to set</param>
  201. void SetSpriteCategoryList(SpriteCategoryList spriteCategoryList);
  202. }
  203. }