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.

SpriteLibraryUtilitiesEditor.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.U2D.Animation;
  7. using Object = UnityEngine.Object;
  8. namespace UnityEditor.U2D.Animation
  9. {
  10. internal static class SpriteLibraryUtilitiesEditor
  11. {
  12. public static void ExportSpriteLibraryToAssetFile(SpriteLibrary spriteLibrary, string savePath)
  13. {
  14. Debug.Assert(!string.IsNullOrEmpty(savePath) && !string.IsNullOrEmpty(Path.GetFileName(savePath)));
  15. var serializedObject = new SerializedObject(spriteLibrary);
  16. var masterLibraryProperty = serializedObject.FindProperty(SpriteLibraryComponentPropertyString.spriteLibraryAsset);
  17. var libraryProperty = serializedObject.FindProperty(SpriteLibraryComponentPropertyString.library);
  18. var masterLibraryPath = masterLibraryProperty.objectReferenceValue != null ? AssetDatabase.GetAssetPath(masterLibraryProperty.objectReferenceValue) : "";
  19. var overrides = new List<SpriteLibCategoryOverride>();
  20. CopySpriteLibraryToOverride(overrides, libraryProperty);
  21. var assetToSave = ScriptableObject.CreateInstance<SpriteLibrarySourceAsset>();
  22. assetToSave.SetLibrary(overrides);
  23. if (!string.IsNullOrEmpty(masterLibraryPath))
  24. assetToSave.SetPrimaryLibraryGUID(AssetDatabase.AssetPathToGUID(masterLibraryPath));
  25. SpriteLibrarySourceAssetImporter.SaveSpriteLibrarySourceAsset(assetToSave, savePath);
  26. Object.DestroyImmediate(assetToSave);
  27. AssetDatabase.ImportAsset(savePath);
  28. var savedAsset = AssetDatabase.LoadAssetAtPath<SpriteLibraryAsset>(savePath);
  29. if (savedAsset == null)
  30. {
  31. Debug.LogError($"Failed to export Sprite Library Asset to {savePath} asset.");
  32. return;
  33. }
  34. libraryProperty.ClearArray();
  35. masterLibraryProperty.objectReferenceValue = savedAsset;
  36. serializedObject.ApplyModifiedProperties();
  37. serializedObject.Update();
  38. }
  39. public static void CopySpriteLibraryToOverride(IList<SpriteLibCategoryOverride> destination, SerializedProperty library)
  40. {
  41. if (destination == null || library == null || library.arraySize == 0)
  42. return;
  43. destination.Clear();
  44. var categoryEntries = library.GetArrayElementAtIndex(0);
  45. for (var i = 0; i < library.arraySize; ++i)
  46. {
  47. var overrideCategory = new SpriteLibCategoryOverride()
  48. {
  49. categoryList = new List<SpriteCategoryEntry>(),
  50. entryOverrideCount = 0,
  51. fromMain = false,
  52. name = categoryEntries.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue,
  53. overrideEntries = new List<SpriteCategoryEntryOverride>()
  54. };
  55. var entries = categoryEntries.FindPropertyRelative(SpriteLibraryPropertyString.categoryList);
  56. var overrideCategoryEntries = overrideCategory.overrideEntries;
  57. if (entries.arraySize > 0)
  58. {
  59. var entry = entries.GetArrayElementAtIndex(0);
  60. for (var j = 0; j < entries.arraySize; ++j)
  61. {
  62. overrideCategoryEntries.Add(new SpriteCategoryEntryOverride()
  63. {
  64. fromMain = false,
  65. name = entry.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue,
  66. sprite = (Sprite)entry.FindPropertyRelative(SpriteLibraryPropertyString.sprite).objectReferenceValue,
  67. spriteOverride = (Sprite)entry.FindPropertyRelative(SpriteLibraryPropertyString.sprite).objectReferenceValue
  68. });
  69. entry.Next(false);
  70. }
  71. }
  72. destination.Add(overrideCategory);
  73. categoryEntries.Next(false);
  74. }
  75. }
  76. public static void UpdateLibraryWithNewMainLibrary(SpriteLibraryAsset newMainLibrary, SerializedProperty destLibrary)
  77. {
  78. var emptyStringArray = Array.Empty<string>();
  79. var newCategories = newMainLibrary != null ? newMainLibrary.GetCategoryNames().ToArray() : emptyStringArray;
  80. // populate new primary
  81. var newCategoryIndex = 0;
  82. foreach (var newCategory in newCategories)
  83. {
  84. SerializedProperty existingCategory = null;
  85. if (destLibrary.arraySize > 0)
  86. {
  87. var cat = destLibrary.GetArrayElementAtIndex(0);
  88. for (var i = 0; i < destLibrary.arraySize; ++i)
  89. {
  90. if (cat.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue == newCategory)
  91. {
  92. existingCategory = cat;
  93. if (i != newCategoryIndex)
  94. destLibrary.MoveArrayElement(i, newCategoryIndex);
  95. break;
  96. }
  97. cat.Next(false);
  98. }
  99. }
  100. if (existingCategory != null)
  101. {
  102. if (!existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue)
  103. existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue = true;
  104. }
  105. else
  106. {
  107. destLibrary.InsertArrayElementAtIndex(newCategoryIndex);
  108. existingCategory = destLibrary.GetArrayElementAtIndex(newCategoryIndex);
  109. SetPropertyName(existingCategory, newCategory);
  110. existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue = true;
  111. existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntryCount).intValue = 0;
  112. existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntries).arraySize = 0;
  113. }
  114. newCategoryIndex++;
  115. var newEntries = newMainLibrary.GetCategoryLabelNames(newCategory);
  116. var entries = existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntries);
  117. var newEntryIndex = 0;
  118. foreach (var newEntry in newEntries)
  119. {
  120. SerializedProperty cacheEntry = null;
  121. if (entries.arraySize > 0)
  122. {
  123. var ent = entries.GetArrayElementAtIndex(0);
  124. for (int j = 0; j < entries.arraySize; ++j)
  125. {
  126. if (ent.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue == newEntry)
  127. {
  128. cacheEntry = ent;
  129. if (j != newEntryIndex)
  130. entries.MoveArrayElement(j, newEntryIndex);
  131. break;
  132. }
  133. ent.Next(false);
  134. }
  135. }
  136. var mainSprite = newMainLibrary.GetSprite(newCategory, newEntry);
  137. if (cacheEntry == null)
  138. {
  139. entries.InsertArrayElementAtIndex(newEntryIndex);
  140. cacheEntry = entries.GetArrayElementAtIndex(newEntryIndex);
  141. SetPropertyName(cacheEntry, newEntry);
  142. cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.spriteOverride)
  143. .objectReferenceValue = mainSprite;
  144. }
  145. ++newEntryIndex;
  146. if (!cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue)
  147. cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue = true;
  148. if (cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.sprite).objectReferenceValue != mainSprite)
  149. cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.sprite).objectReferenceValue = mainSprite;
  150. }
  151. }
  152. // Remove any library or entry that is not in primary and not overridden
  153. for (var i = 0; i < destLibrary.arraySize; ++i)
  154. {
  155. var categoryProperty = destLibrary.GetArrayElementAtIndex(i);
  156. var categoryEntriesProperty = categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntries);
  157. var categoryFromMainProperty = categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.fromMain);
  158. var categoryName = categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue;
  159. var categoryInPrimary = newCategories.Contains(categoryName);
  160. var entriesInPrimary = categoryInPrimary ? newMainLibrary.GetCategoryLabelNames(categoryName) : emptyStringArray;
  161. var categoryOverride = 0;
  162. for (var j = 0; j < categoryEntriesProperty.arraySize; ++j)
  163. {
  164. var entry = categoryEntriesProperty.GetArrayElementAtIndex(j);
  165. var entryName = entry.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue;
  166. var entryInPrimary = entriesInPrimary.Contains(entryName);
  167. var entryFromMainProperty = entry.FindPropertyRelative(SpriteLibraryPropertyString.fromMain);
  168. var overrideSpriteProperty = entry.FindPropertyRelative(SpriteLibraryPropertyString.spriteOverride);
  169. var spriteProperty = entry.FindPropertyRelative(SpriteLibraryPropertyString.sprite);
  170. if (!entryInPrimary)
  171. {
  172. // Entry no longer in new primary.
  173. // Check for override and set it to us
  174. if (entryFromMainProperty.boolValue)
  175. {
  176. if (overrideSpriteProperty.objectReferenceValue == spriteProperty.objectReferenceValue)
  177. {
  178. categoryEntriesProperty.DeleteArrayElementAtIndex(j);
  179. --j;
  180. continue;
  181. }
  182. }
  183. if (entryFromMainProperty.boolValue)
  184. entryFromMainProperty.boolValue = false;
  185. if (spriteProperty.objectReferenceValue != overrideSpriteProperty.objectReferenceValue)
  186. spriteProperty.objectReferenceValue = overrideSpriteProperty.objectReferenceValue;
  187. ++categoryOverride;
  188. }
  189. else
  190. {
  191. // Check if sprite has been override
  192. if (spriteProperty.objectReferenceValue != overrideSpriteProperty.objectReferenceValue)
  193. ++categoryOverride;
  194. }
  195. }
  196. if (!categoryInPrimary && categoryEntriesProperty.arraySize == 0 && categoryFromMainProperty.boolValue)
  197. {
  198. destLibrary.DeleteArrayElementAtIndex(i);
  199. --i;
  200. continue;
  201. }
  202. // since there is override, and we removed the main. This category now
  203. // belows to the library
  204. if (!categoryInPrimary)
  205. {
  206. if (categoryFromMainProperty.boolValue)
  207. categoryFromMainProperty.boolValue = false;
  208. }
  209. else
  210. {
  211. if (categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntryCount).intValue != categoryOverride)
  212. categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntryCount).intValue = categoryOverride;
  213. }
  214. }
  215. }
  216. static void SetPropertyName(SerializedProperty sp, string newName)
  217. {
  218. sp.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue = newName;
  219. sp.FindPropertyRelative(SpriteLibraryPropertyString.hash).intValue = SpriteLibraryUtility.GetStringHash(newName);
  220. }
  221. }
  222. }