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

SpriteLibraryInspector.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Scripting.APIUpdating;
  5. using UnityEngine.U2D.Animation;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. [CustomEditor(typeof(SpriteLibrary))]
  9. [CanEditMultipleObjects]
  10. [MovedFrom("UnityEditor.Experimental.U2D.Animation")]
  11. internal class SpriteLibraryInspector : Editor
  12. {
  13. static class Style
  14. {
  15. public static readonly string createNew = L10n.Tr("New");
  16. public static readonly string exportToAssetInfo = L10n.Tr($"There are overrides in this Sprite Library. To save the override data, select 'Export to Sprite Library Asset'. Learn mode about creating and modifying overrides <a href={k_OverrideEntriesDocsLink}>here</a>.");
  17. public static readonly string exportToAsset = L10n.Tr("Export to Sprite Library Asset");
  18. public static readonly string selectSaveLocation = L10n.Tr("Select save location");
  19. public static readonly string selectSaveLocationMessage = L10n.Tr("Select save location for the Sprite Library Asset in your project.");
  20. public static readonly string openInSpriteLibraryEditor = L10n.Tr("Open Sprite Library Editor");
  21. public static readonly string exportIncorrectSavePath = L10n.Tr("Asset save path must be inside the Assets folder.");
  22. }
  23. SerializedProperty m_MasterLibraryProperty;
  24. SpriteLibraryAsset m_MasterLibraryObject;
  25. Dictionary<Object, SerializedObject> m_CachedSerializedObjects;
  26. List<SpriteResolver> m_CachedResolvers;
  27. const string k_RootFolderName = "Assets";
  28. const string k_OverrideEntriesDocsLink = "\"https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/SL-component.html%23overriding-entries\"";
  29. public void OnEnable()
  30. {
  31. m_MasterLibraryProperty = serializedObject.FindProperty(SpriteLibraryComponentPropertyString.spriteLibraryAsset);
  32. UpdateMasterLibraryReference();
  33. CacheSerializedObjects();
  34. CacheResolvers();
  35. }
  36. public override void OnInspectorGUI()
  37. {
  38. serializedObject.Update();
  39. UpdateMasterLibraryReference();
  40. EditorGUILayout.BeginHorizontal();
  41. EditorGUI.BeginChangeCheck();
  42. EditorGUILayout.PropertyField(m_MasterLibraryProperty);
  43. if (EditorGUI.EndChangeCheck())
  44. {
  45. serializedObject.ApplyModifiedProperties();
  46. UpdateMasterLibraryReference();
  47. UpdateSpriteResolvers();
  48. }
  49. if (m_MasterLibraryObject == null && !m_MasterLibraryProperty.hasMultipleDifferentValues)
  50. {
  51. if (GUILayout.Button(Style.createNew) && HandleCreateNewAsset())
  52. {
  53. UpdateMasterLibraryReference();
  54. UpdateSpriteResolvers();
  55. }
  56. }
  57. EditorGUILayout.EndHorizontal();
  58. EditorGUI.BeginDisabledGroup(m_MasterLibraryObject == null || m_MasterLibraryProperty.hasMultipleDifferentValues);
  59. if (GUILayout.Button(Style.openInSpriteLibraryEditor))
  60. {
  61. Selection.objects = new Object[] { m_MasterLibraryObject };
  62. SpriteLibraryEditor.SpriteLibraryEditorWindow.OpenWindow();
  63. }
  64. EditorGUI.EndDisabledGroup();
  65. if (targets.Any(t => HasLocalOverride(m_CachedSerializedObjects[t])))
  66. {
  67. EditorStyles.helpBox.richText = true;
  68. EditorGUILayout.HelpBox(Style.exportToAssetInfo, MessageType.Info);
  69. if (GUILayout.Button(Style.exportToAsset) && HandleExportOverrides())
  70. {
  71. UpdateMasterLibraryReference();
  72. UpdateSpriteResolvers();
  73. }
  74. }
  75. }
  76. void CacheSerializedObjects()
  77. {
  78. m_CachedSerializedObjects = new Dictionary<Object, SerializedObject>();
  79. foreach (var t in targets)
  80. m_CachedSerializedObjects[t] = new SerializedObject(t);
  81. }
  82. void CacheResolvers()
  83. {
  84. m_CachedResolvers = new List<SpriteResolver>();
  85. foreach (var t in targets)
  86. {
  87. var sl = (SpriteLibrary)t;
  88. var resolvers = sl.GetComponentsInChildren<SpriteResolver>();
  89. m_CachedResolvers.AddRange(resolvers);
  90. }
  91. }
  92. void UpdateMasterLibraryReference()
  93. {
  94. serializedObject.Update();
  95. m_MasterLibraryObject = (SpriteLibraryAsset)m_MasterLibraryProperty.objectReferenceValue;
  96. }
  97. void UpdateSpriteResolvers()
  98. {
  99. foreach (var resolver in m_CachedResolvers)
  100. {
  101. resolver.ResolveSpriteToSpriteRenderer();
  102. resolver.spriteLibChanged = true;
  103. }
  104. }
  105. bool HandleCreateNewAsset()
  106. {
  107. var createPath = GetFileSavePath(target.name);
  108. if (!string.IsNullOrEmpty(createPath))
  109. {
  110. var emptyLibrary = CreateInstance<SpriteLibrarySourceAsset>();
  111. SpriteLibrarySourceAssetImporter.SaveSpriteLibrarySourceAsset(emptyLibrary, createPath);
  112. DestroyImmediate(emptyLibrary);
  113. AssetDatabase.ImportAsset(createPath);
  114. var newLibraryAsset = AssetDatabase.LoadAssetAtPath<SpriteLibraryAsset>(createPath);
  115. if (newLibraryAsset != null)
  116. {
  117. m_MasterLibraryProperty.objectReferenceValue = newLibraryAsset;
  118. serializedObject.ApplyModifiedProperties();
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. bool HandleExportOverrides()
  125. {
  126. if (targets.Length == 1)
  127. {
  128. var exportPath = GetFileSavePath(target.name);
  129. if (!string.IsNullOrEmpty(exportPath))
  130. {
  131. SpriteLibraryUtilitiesEditor.ExportSpriteLibraryToAssetFile(target as SpriteLibrary, exportPath);
  132. return true;
  133. }
  134. }
  135. else
  136. {
  137. var exportDirectory = GetSaveDirectory();
  138. if (!string.IsNullOrEmpty(exportDirectory))
  139. {
  140. foreach (var t in targets)
  141. {
  142. if (HasLocalOverride(m_CachedSerializedObjects[t]))
  143. {
  144. var exportPath = $"{exportDirectory}/{t.name}{SpriteLibrarySourceAsset.extension}";
  145. exportPath = AssetDatabase.GenerateUniqueAssetPath(exportPath);
  146. SpriteLibraryUtilitiesEditor.ExportSpriteLibraryToAssetFile(t as SpriteLibrary, exportPath);
  147. }
  148. }
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. static string GetFileSavePath(string suggestedFileName)
  155. {
  156. var title = $"{Style.selectSaveLocation} ({suggestedFileName})";
  157. var defaultName = suggestedFileName + SpriteLibrarySourceAsset.extension;
  158. var extension = SpriteLibrarySourceAsset.extension.Substring(1);
  159. var path = EditorUtility.SaveFilePanelInProject(title, defaultName, extension, Style.selectSaveLocationMessage);
  160. return path;
  161. }
  162. static string GetSaveDirectory()
  163. {
  164. var saveDirectory = EditorUtility.SaveFolderPanel(Style.selectSaveLocation, k_RootFolderName, "");
  165. if (string.IsNullOrEmpty(saveDirectory))
  166. return string.Empty;
  167. saveDirectory = GetPathRelativeToAssetsRoot(saveDirectory);
  168. if (string.IsNullOrEmpty(saveDirectory))
  169. {
  170. Debug.Log(Style.exportIncorrectSavePath);
  171. return string.Empty;
  172. }
  173. return saveDirectory;
  174. }
  175. static string GetPathRelativeToAssetsRoot(string path)
  176. {
  177. if (string.IsNullOrWhiteSpace(path) || !path.StartsWith(Application.dataPath))
  178. return string.Empty;
  179. var pathStartIndex = path.IndexOf(k_RootFolderName);
  180. return pathStartIndex == -1 ? string.Empty : path.Substring(pathStartIndex);
  181. }
  182. static bool HasLocalOverride(SerializedObject serializedObject)
  183. {
  184. serializedObject.Update();
  185. var library = serializedObject.FindProperty(SpriteLibraryComponentPropertyString.library);
  186. return library != null && library.arraySize > 0;
  187. }
  188. }
  189. }