Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine.Assertions;
  6. using UnityEngine.Scripting.APIUpdating;
  7. namespace UnityEngine.U2D.Animation
  8. {
  9. internal interface INameHash
  10. {
  11. string name { get; set; }
  12. int hash { get; }
  13. }
  14. [Serializable]
  15. [MovedFrom("UnityEngine.Experimental.U2D.Animation")]
  16. internal class SpriteCategoryEntry : INameHash, ISpriteLibraryLabel
  17. {
  18. [SerializeField]
  19. string m_Name;
  20. [SerializeField]
  21. [HideInInspector]
  22. int m_Hash;
  23. [SerializeField]
  24. Sprite m_Sprite;
  25. public string name
  26. {
  27. get => m_Name;
  28. set
  29. {
  30. m_Name = value;
  31. m_Hash = SpriteLibraryUtility.GetStringHash(m_Name);
  32. }
  33. }
  34. public int hash => m_Hash;
  35. public Sprite sprite
  36. {
  37. get => m_Sprite;
  38. set => m_Sprite = value;
  39. }
  40. public void UpdateHash()
  41. {
  42. m_Hash = SpriteLibraryUtility.GetStringHash(m_Name);
  43. }
  44. }
  45. [Serializable]
  46. [MovedFrom("UnityEngine.Experimental.U2D.Animation")]
  47. internal class SpriteLibCategory : INameHash, ISpriteLibraryCategory
  48. {
  49. [SerializeField]
  50. string m_Name;
  51. [SerializeField]
  52. int m_Hash;
  53. [SerializeField]
  54. List<SpriteCategoryEntry> m_CategoryList;
  55. public string name
  56. {
  57. get { return m_Name; }
  58. set
  59. {
  60. m_Name = value;
  61. m_Hash = SpriteLibraryUtility.GetStringHash(m_Name);
  62. }
  63. }
  64. public int hash => m_Hash;
  65. public List<SpriteCategoryEntry> categoryList
  66. {
  67. get => m_CategoryList;
  68. set => m_CategoryList = value;
  69. }
  70. public IEnumerable<ISpriteLibraryLabel> labels => m_CategoryList;
  71. public void UpdateHash()
  72. {
  73. m_Hash = SpriteLibraryUtility.GetStringHash(m_Name);
  74. foreach (var s in m_CategoryList)
  75. s.UpdateHash();
  76. }
  77. internal void ValidateLabels(bool log = true)
  78. {
  79. SpriteLibraryAsset.RenameDuplicate(m_CategoryList,
  80. (originalName, newName)
  81. =>
  82. {
  83. if(log)
  84. Debug.LogWarning(string.Format("Label {0} renamed to {1} due to hash clash", originalName, newName));
  85. });
  86. }
  87. }
  88. /// <summary>
  89. /// A custom Asset that stores Sprites grouping.
  90. /// </summary>
  91. /// <Description>
  92. /// Sprites are grouped under a given category as categories. Each category and label needs to have
  93. /// a name specified so that it can be queried.
  94. /// </Description>
  95. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/AssetUpgrader.html%23upgrading-sprite-libraries")]
  96. [MovedFrom("UnityEngine.Experimental.U2D.Animation")]
  97. [Icon(IconUtility.IconPath + "Animation.SpriteLibrary.png")]
  98. public class SpriteLibraryAsset : ScriptableObject
  99. {
  100. [SerializeField]
  101. List<SpriteLibCategory> m_Labels = new List<SpriteLibCategory>();
  102. [SerializeField]
  103. long m_ModificationHash;
  104. [SerializeField]
  105. int m_Version;
  106. internal static SpriteLibraryAsset CreateAsset(List<SpriteLibCategory> categories, string assetName, long modificationHash)
  107. {
  108. var asset = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
  109. asset.m_Labels = categories;
  110. asset.ValidateCategories();
  111. asset.name = assetName;
  112. asset.UpdateHashes();
  113. asset.m_ModificationHash = modificationHash;
  114. asset.version = 1;
  115. return asset;
  116. }
  117. internal List<SpriteLibCategory> categories
  118. {
  119. get => m_Labels;
  120. set
  121. {
  122. m_Labels = value;
  123. ValidateCategories();
  124. }
  125. }
  126. /// <summary>
  127. /// Hash to quickly check if the library has any changes made to it.
  128. /// </summary>
  129. internal long modificationHash
  130. {
  131. get => m_ModificationHash;
  132. set => m_ModificationHash = value;
  133. }
  134. /// <summary>
  135. /// File version number.
  136. /// </summary>
  137. internal int version
  138. {
  139. set => m_Version = value;
  140. }
  141. void OnEnable()
  142. {
  143. if (m_Version < 1)
  144. UpdateToVersionOne();
  145. }
  146. void UpdateToVersionOne()
  147. {
  148. UpdateHashes();
  149. m_Version = 1;
  150. }
  151. internal Sprite GetSprite(int categoryHash, int labelHash)
  152. {
  153. var category = m_Labels.FirstOrDefault(x => x.hash == categoryHash);
  154. if (category != null)
  155. {
  156. var spriteLabel = category.categoryList.FirstOrDefault(x => x.hash == labelHash);
  157. if (spriteLabel != null)
  158. {
  159. return spriteLabel.sprite;
  160. }
  161. }
  162. return null;
  163. }
  164. internal Sprite GetSprite(int categoryHash, int labelHash, out bool validEntry)
  165. {
  166. SpriteLibCategory category = null;
  167. for (int i = 0; i < m_Labels.Count; ++i)
  168. {
  169. if (m_Labels[i].hash == categoryHash)
  170. {
  171. category = m_Labels[i];
  172. break;
  173. }
  174. }
  175. if (category != null)
  176. {
  177. SpriteCategoryEntry spritelabel = null;
  178. for (int i = 0; i < category.categoryList.Count; ++i)
  179. {
  180. if (category.categoryList[i].hash == labelHash)
  181. {
  182. spritelabel = category.categoryList[i];
  183. break;
  184. }
  185. }
  186. if (spritelabel != null)
  187. {
  188. validEntry = true;
  189. return spritelabel.sprite;
  190. }
  191. }
  192. validEntry = false;
  193. return null;
  194. }
  195. /// <summary>
  196. /// Returns the Sprite registered in the Asset given the Category and Label value.
  197. /// </summary>
  198. /// <param name="category">Category string value.</param>
  199. /// <param name="label">Label string value.</param>
  200. /// <returns></returns>
  201. public Sprite GetSprite(string category, string label)
  202. {
  203. var categoryHash = SpriteLibraryUtility.GetStringHash(category);
  204. var labelHash = SpriteLibraryUtility.GetStringHash(label);
  205. return GetSprite(categoryHash, labelHash);
  206. }
  207. /// <summary>
  208. /// Return all the Category names of the Sprite Library Asset that is associated.
  209. /// </summary>
  210. /// <returns>A Enumerable string value representing the name.</returns>
  211. public IEnumerable<string> GetCategoryNames()
  212. {
  213. return m_Labels.Select(x => x.name);
  214. }
  215. /// <summary>
  216. /// (Obsolete) Returns the labels' name for the given name.
  217. /// </summary>
  218. /// <param name="category">Category name.</param>
  219. /// <returns>A Enumerable string representing labels' name.</returns>
  220. [Obsolete("GetCategorylabelNames has been deprecated. Please use GetCategoryLabelNames (UnityUpgradable) -> GetCategoryLabelNames(*)")]
  221. public IEnumerable<string> GetCategorylabelNames(string category)
  222. {
  223. return GetCategoryLabelNames(category);
  224. }
  225. /// <summary>
  226. /// Returns the labels' name for the given name.
  227. /// </summary>
  228. /// <param name="category">Category name.</param>
  229. /// <returns>A Enumerable string representing labels' name.</returns>
  230. public IEnumerable<string> GetCategoryLabelNames(string category)
  231. {
  232. var label = m_Labels.FirstOrDefault(x => x.name == category);
  233. return label == null ? new string[0] : label.categoryList.Select(x => x.name);
  234. }
  235. /// <summary>
  236. /// Add or replace and existing Sprite into the given Category and Label.
  237. /// </summary>
  238. /// <param name="sprite">Sprite to add.</param>
  239. /// <param name="category">Category to add the Sprite to.</param>
  240. /// <param name="label">Label of the Category to add the Sprite to. If this parameter is null or an empty string, it will attempt to add a empty category.</param>
  241. public void AddCategoryLabel(Sprite sprite, string category, string label)
  242. {
  243. category = category.Trim();
  244. label = label?.Trim();
  245. if (string.IsNullOrEmpty(category))
  246. throw new ArgumentException("Cannot add empty or null Category string");
  247. var catHash = SpriteLibraryUtility.GetStringHash(category);
  248. SpriteCategoryEntry categorylabel = null;
  249. SpriteLibCategory libCategory = null;
  250. libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash);
  251. if (libCategory != null)
  252. {
  253. if(string.IsNullOrEmpty(label))
  254. throw new ArgumentException("Cannot add empty or null Label string");
  255. Assert.AreEqual(libCategory.name, category, "Category string hash clashes with another existing Category. Please use another string");
  256. var labelHash = SpriteLibraryUtility.GetStringHash(label);
  257. categorylabel = libCategory.categoryList.FirstOrDefault(y => y.hash == labelHash);
  258. if (categorylabel != null)
  259. {
  260. Assert.AreEqual(categorylabel.name, label, "Label string hash clashes with another existing label. Please use another string");
  261. categorylabel.sprite = sprite;
  262. }
  263. else
  264. {
  265. categorylabel = new SpriteCategoryEntry()
  266. {
  267. name = label,
  268. sprite = sprite
  269. };
  270. libCategory.categoryList.Add(categorylabel);
  271. }
  272. }
  273. else
  274. {
  275. var slc = new SpriteLibCategory()
  276. {
  277. categoryList = new List<SpriteCategoryEntry>(),
  278. name = category
  279. };
  280. if (!string.IsNullOrEmpty(label))
  281. {
  282. slc.categoryList.Add(new SpriteCategoryEntry()
  283. {
  284. name = label,
  285. sprite = sprite
  286. });
  287. }
  288. m_Labels.Add(slc);
  289. }
  290. #if UNITY_EDITOR
  291. EditorUtility.SetDirty(this);
  292. #endif
  293. }
  294. /// <summary>
  295. /// Remove a Label from a given Category.
  296. /// </summary>
  297. /// <param name="category">Category to remove from.</param>
  298. /// <param name="label">Label to remove.</param>
  299. /// <param name="deleteCategory">Indicate to remove the Category if it is empty.</param>
  300. public void RemoveCategoryLabel(string category, string label, bool deleteCategory)
  301. {
  302. var catHash = SpriteLibraryUtility.GetStringHash(category);
  303. SpriteLibCategory libCategory = null;
  304. libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash);
  305. if (libCategory != null)
  306. {
  307. var labelHash = SpriteLibraryUtility.GetStringHash(label);
  308. libCategory.categoryList.RemoveAll(x => x.hash == labelHash);
  309. if (deleteCategory && libCategory.categoryList.Count == 0)
  310. m_Labels.RemoveAll(x => x.hash == libCategory.hash);
  311. #if UNITY_EDITOR
  312. EditorUtility.SetDirty(this);
  313. #endif
  314. }
  315. }
  316. internal void UpdateHashes()
  317. {
  318. foreach (var e in m_Labels)
  319. e.UpdateHash();
  320. #if UNITY_EDITOR
  321. UnityEditor.EditorUtility.SetDirty(this);
  322. #endif
  323. }
  324. internal void ValidateCategories(bool log = true)
  325. {
  326. RenameDuplicate(m_Labels, (originalName, newName)
  327. =>
  328. {
  329. if(log)
  330. Debug.LogWarning($"Category {originalName} renamed to {newName} due to hash clash");
  331. });
  332. for (var i = 0; i < m_Labels.Count; ++i)
  333. {
  334. // Verify categories have no hash clash
  335. var category = m_Labels[i];
  336. // Verify labels have no clash
  337. category.ValidateLabels(log);
  338. }
  339. }
  340. internal static void RenameDuplicate(IEnumerable<INameHash> nameHashList, Action<string, string> onRename)
  341. {
  342. const int k_IncrementMax = 1000;
  343. for (var i = 0; i < nameHashList.Count(); ++i)
  344. {
  345. // Verify categories have no hash clash
  346. var category = nameHashList.ElementAt(i);
  347. var categoriesClash = nameHashList.Where(x => (x.hash == category.hash || x.name == category.name) && x != category);
  348. int increment = 0;
  349. for (int j = 0; j < categoriesClash.Count(); ++j)
  350. {
  351. var categoryClash = categoriesClash.ElementAt(j);
  352. while (increment < k_IncrementMax)
  353. {
  354. var name = categoryClash.name;
  355. name = $"{name}_{increment}";
  356. var nameHash = SpriteLibraryUtility.GetStringHash(name);
  357. var exist = nameHashList.FirstOrDefault(x => (x.hash == nameHash || x.name == name) && x != categoryClash);
  358. if (exist == null)
  359. {
  360. onRename(categoryClash.name, name);
  361. categoryClash.name = name;
  362. break;
  363. }
  364. ++increment;
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }