暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteLibrarySourceAssetImporter.cs 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEditor.AssetImporters;
  4. using UnityEngine;
  5. using UnityEngine.U2D.Animation;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. /// <summary>
  9. /// A ScriptedImporter that imports .spriteLib extension file to generate
  10. /// SpriteLibraryAsset
  11. /// </summary>
  12. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/SL-Asset.html")]
  13. [ScriptedImporter(22100000, "spriteLib", AllowCaching = true)]
  14. public class SpriteLibrarySourceAssetImporter : ScriptedImporter
  15. {
  16. /// <summary>
  17. /// Implementation of ScriptedImporter.OnImportAsset
  18. /// </summary>
  19. /// <param name="ctx">
  20. /// This argument contains all the contextual information needed to process the import
  21. /// event and is also used by the custom importer to store the resulting Unity Asset.
  22. /// </param>
  23. public override void OnImportAsset(AssetImportContext ctx)
  24. {
  25. var spriteLib = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
  26. spriteLib.name = Path.GetFileNameWithoutExtension(assetPath);
  27. var sourceAsset = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(assetPath);
  28. if (sourceAsset?.Length > 0)
  29. {
  30. var sourceLibraryAsset = sourceAsset[0] as SpriteLibrarySourceAsset;
  31. if (sourceLibraryAsset != null)
  32. {
  33. if (!HasValidMainLibrary(sourceLibraryAsset, assetPath))
  34. sourceLibraryAsset.SetPrimaryLibraryGUID(string.Empty);
  35. UpdateSpriteLibrarySourceAssetLibraryWithMainAsset(sourceLibraryAsset);
  36. foreach (var cat in sourceLibraryAsset.library)
  37. {
  38. spriteLib.AddCategoryLabel(null, cat.name, null);
  39. foreach (var entry in cat.overrideEntries)
  40. {
  41. spriteLib.AddCategoryLabel(entry.spriteOverride, cat.name, entry.name);
  42. }
  43. }
  44. spriteLib.modificationHash = sourceLibraryAsset.modificationHash;
  45. spriteLib.version = sourceLibraryAsset.version;
  46. if (!string.IsNullOrEmpty(sourceLibraryAsset.primaryLibraryGUID))
  47. ctx.DependsOnArtifact(AssetDatabase.GUIDToAssetPath(sourceLibraryAsset.primaryLibraryGUID));
  48. }
  49. }
  50. ctx.AddObjectToAsset("SpriteLib", spriteLib, EditorIconUtility.LoadIconResource("Animation.SpriteLibrary", "ComponentIcons", "ComponentIcons"));
  51. }
  52. internal static void UpdateSpriteLibrarySourceAssetLibraryWithMainAsset(SpriteLibrarySourceAsset sourceLibraryAsset)
  53. {
  54. var so = new SerializedObject(sourceLibraryAsset);
  55. var library = so.FindProperty(SpriteLibrarySourceAssetPropertyString.library);
  56. var mainLibraryAssetAssetPath = AssetDatabase.GUIDToAssetPath(sourceLibraryAsset.primaryLibraryGUID);
  57. var mainLibraryAsset = AssetDatabase.LoadAssetAtPath<SpriteLibraryAsset>(mainLibraryAssetAssetPath);
  58. SpriteLibraryUtilitiesEditor.UpdateLibraryWithNewMainLibrary(mainLibraryAsset, library);
  59. if (so.hasModifiedProperties)
  60. {
  61. var modHashProperty = so.FindProperty(SpriteLibrarySourceAssetPropertyString.modificationHash);
  62. modHashProperty.longValue = mainLibraryAsset != null ? mainLibraryAsset.modificationHash : SpriteLibraryUtility.GenerateHash();
  63. so.ApplyModifiedPropertiesWithoutUndo();
  64. }
  65. }
  66. internal static bool HasValidMainLibrary(SpriteLibrarySourceAsset sourceLibraryAsset, string assetPath)
  67. {
  68. if (string.IsNullOrEmpty(sourceLibraryAsset.primaryLibraryGUID))
  69. return false;
  70. var primaryLibraryPath = AssetDatabase.GUIDToAssetPath(sourceLibraryAsset.primaryLibraryGUID);
  71. if (assetPath == primaryLibraryPath)
  72. return false;
  73. var primaryAssetParentChain = GetAssetParentChain(AssetDatabase.LoadAssetAtPath<SpriteLibraryAsset>(primaryLibraryPath));
  74. foreach (var parentLibrary in primaryAssetParentChain)
  75. {
  76. var parentPath = AssetDatabase.GetAssetPath(parentLibrary);
  77. if (parentPath == assetPath)
  78. return false;
  79. }
  80. return true;
  81. }
  82. internal static SpriteLibrarySourceAsset LoadSpriteLibrarySourceAsset(string path)
  83. {
  84. var loadedObjects = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(path);
  85. foreach (var obj in loadedObjects)
  86. {
  87. if (obj is SpriteLibrarySourceAsset)
  88. return (SpriteLibrarySourceAsset)obj;
  89. }
  90. return null;
  91. }
  92. internal static void SaveSpriteLibrarySourceAsset(SpriteLibrarySourceAsset obj, string path)
  93. {
  94. if (!HasValidMainLibrary(obj, path))
  95. obj.SetPrimaryLibraryGUID(string.Empty);
  96. UnityEditorInternal.InternalEditorUtility.SaveToSerializedFileAndForget(new[] { obj }, path, true);
  97. }
  98. [MenuItem("internal:Assets/Convert to SpriteLibrarySourceAsset", true)]
  99. static bool ConvertToSpriteLibrarySourceAssetValidate()
  100. {
  101. foreach (var obj in Selection.objects)
  102. {
  103. if (obj is SpriteLibraryAsset)
  104. return true;
  105. }
  106. return false;
  107. }
  108. [MenuItem("internal:Assets/Convert to SpriteLibrarySourceAsset")]
  109. static void ConvertToSourceAsset()
  110. {
  111. foreach (var obj in Selection.objects)
  112. {
  113. if (obj is SpriteLibraryAsset)
  114. {
  115. var asset = (SpriteLibraryAsset)obj;
  116. var path = AssetDatabase.GetAssetPath(asset);
  117. var currentAssetPath = Path.GetDirectoryName(path);
  118. var fileName = Path.GetFileNameWithoutExtension(path);
  119. var convertFileName = fileName + SpriteLibrarySourceAsset.extension;
  120. convertFileName = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(currentAssetPath, convertFileName));
  121. var convertAsset = ScriptableObject.CreateInstance<SpriteLibrarySourceAsset>();
  122. convertAsset.SetLibrary(new List<SpriteLibCategoryOverride>(asset.categories.Count));
  123. for (var i = 0; i < asset.categories.Count; ++i)
  124. {
  125. var category = asset.categories[i];
  126. var newCategory = new SpriteLibCategoryOverride()
  127. {
  128. overrideEntries = new List<SpriteCategoryEntryOverride>(category.categoryList.Count),
  129. name = category.name,
  130. entryOverrideCount = 0,
  131. fromMain = false
  132. };
  133. convertAsset.AddCategory(newCategory);
  134. for (var j = 0; j < category.categoryList.Count; ++j)
  135. {
  136. newCategory.overrideEntries.Add(new SpriteCategoryEntryOverride()
  137. {
  138. name = category.categoryList[j].name,
  139. sprite = null,
  140. fromMain = false,
  141. spriteOverride = category.categoryList[j].sprite
  142. });
  143. }
  144. }
  145. SaveSpriteLibrarySourceAsset(convertAsset, convertFileName);
  146. }
  147. }
  148. AssetDatabase.Refresh();
  149. }
  150. internal static SpriteLibraryAsset GetAssetParent(SpriteLibraryAsset asset)
  151. {
  152. var currentAssetPath = AssetDatabase.GetAssetPath(asset);
  153. if (AssetImporter.GetAtPath(currentAssetPath) is SpriteLibrarySourceAssetImporter)
  154. {
  155. var sourceAsset = LoadSpriteLibrarySourceAsset(currentAssetPath);
  156. var primaryLibraryId = sourceAsset != null ? sourceAsset.primaryLibraryGUID : null;
  157. if (primaryLibraryId != null)
  158. {
  159. var primaryLibraryAssetAssetPath = AssetDatabase.GUIDToAssetPath(primaryLibraryId);
  160. return AssetDatabase.LoadAssetAtPath<SpriteLibraryAsset>(primaryLibraryAssetAssetPath);
  161. }
  162. }
  163. return null;
  164. }
  165. internal static List<SpriteLibraryAsset> GetAssetParentChain(SpriteLibraryAsset asset)
  166. {
  167. var chain = new List<SpriteLibraryAsset>();
  168. if (asset != null)
  169. {
  170. var parent = GetAssetParent(asset);
  171. while (parent != null && !chain.Contains(parent))
  172. {
  173. chain.Add(parent);
  174. parent = GetAssetParent(parent);
  175. }
  176. }
  177. return chain;
  178. }
  179. internal static SpriteLibraryAsset GetAssetFromSelection()
  180. {
  181. foreach (var selectedObject in Selection.objects)
  182. {
  183. var selectedAsset = selectedObject as SpriteLibraryAsset;
  184. if (selectedAsset != null)
  185. return selectedAsset;
  186. }
  187. return null;
  188. }
  189. }
  190. }