暫無描述
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.

ISpriteEditorDataProvider.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.U2D;
  5. using UnityObject = UnityEngine.Object;
  6. namespace UnityEditor.U2D.Sprites
  7. {
  8. /// <summary>An interface that allows Sprite Editor Window to edit Sprite data for user custom importer.</summary>
  9. /// <remarks> Use this interface to edit Sprite data.
  10. /// <code>
  11. /// using UnityEditor;
  12. /// using UnityEditor.U2D.Sprites;
  13. /// using UnityEngine;
  14. ///
  15. /// public class PivotUpdater : AssetPostprocessor
  16. /// {
  17. /// private void OnPreprocessTexture()
  18. /// {
  19. /// var factory = new SpriteDataProviderFactories();
  20. /// factory.Init();
  21. /// var dataProvider = factory.GetSpriteEditorDataProviderFromObject(assetImporter);
  22. /// dataProvider.InitSpriteEditorDataProvider();
  23. ///
  24. /// SetPivot(dataProvider, new Vector2(0.5f, 0.5f));
  25. ///
  26. /// dataProvider.Apply();
  27. /// }
  28. ///
  29. /// static void SetPivot(ISpriteEditorDataProvider dataProvider, Vector2 pivot)
  30. /// {
  31. /// var spriteRects = dataProvider.GetSpriteRects();
  32. /// foreach (var rect in spriteRects)
  33. /// {
  34. /// rect.pivot = pivot;
  35. /// rect.alignment = SpriteAlignment.Custom;
  36. /// }
  37. /// dataProvider.SetSpriteRects(spriteRects);
  38. /// }
  39. /// }
  40. /// </code>
  41. /// </remarks>
  42. public interface ISpriteEditorDataProvider
  43. {
  44. /// <summary>SpriteImportMode to indicate how Sprite data will be imported.</summary>
  45. SpriteImportMode spriteImportMode { get; }
  46. /// <summary>The number of pixels in the sprite that correspond to one unit in world space.</summary>
  47. float pixelsPerUnit { get; }
  48. /// <summary>The object that this data provider is acquiring its data from.</summary>
  49. UnityObject targetObject { get; }
  50. /// <summary>Returns an array of SpriteRect representing Sprite data the provider has.</summary>
  51. /// <returns>Array of SpriteRect.</returns>
  52. SpriteRect[] GetSpriteRects();
  53. /// <summary>Sets the data provider's current SpriteRect.</summary>
  54. /// <param name="spriteRects">Updated array of SpriteRect.</param>
  55. void SetSpriteRects(SpriteRect[] spriteRects);
  56. /// <summary>Applying any changed data.</summary>
  57. void Apply();
  58. /// <summary>Allows the data provider to initialize any data if needed.</summary>
  59. void InitSpriteEditorDataProvider();
  60. /// <summary>Gets other data providers that might be supported by ISpriteEditorDataProvider.targetObject.</summary>
  61. /// <typeparam name="T">The data provider type to acquire.</typeparam>
  62. /// <returns>Data provider type.</returns>
  63. T GetDataProvider<T>() where T : class;
  64. /// <summary>Queries if ISpriteEditorDataProvider.targetObject supports the data provider type.</summary>
  65. /// <param name="type">Data provider type.</param>
  66. /// <returns>True if supports, false otherwise.</returns>
  67. bool HasDataProvider(Type type);
  68. }
  69. /// <summary>
  70. /// Data Provider interface that deals with Sprite Bone data.
  71. /// </summary>
  72. public interface ISpriteBoneDataProvider
  73. {
  74. /// <summary>
  75. /// Returns the list of SpriteBone for the corresponding Sprite ID.
  76. /// </summary>
  77. /// <param name="guid">Sprite ID.</param>
  78. /// <returns>The list of SpriteBone associated with the Sprite</returns>
  79. List<SpriteBone> GetBones(GUID guid);
  80. /// <summary>Sets a new set of SpriteBone for the corresponding Sprite ID.</summary>
  81. /// <param name = "guid" > Sprite ID.</param>
  82. /// <param name = "bones" > List of SpriteBone to associate with the Sprite.</param>
  83. void SetBones(GUID guid, List<SpriteBone> bones);
  84. }
  85. /// <summary>
  86. /// Data provider that provides data for ID to be used given a SpriteRect's name.
  87. /// </summary>
  88. /// <remarks>
  89. /// The name and ID pair is used to allow mapping back a previous created SpriteRect.
  90. /// </remarks>
  91. public interface ISpriteNameFileIdDataProvider
  92. {
  93. /// <summary>Returns an IEnumerable of SpriteNameFileIdPair representing the name and file id pairs the provider has.</summary>
  94. /// <returns>Name and file id pairs.</returns>
  95. IEnumerable<SpriteNameFileIdPair> GetNameFileIdPairs();
  96. /// <summary>Sets the data provider's current NameFileIdPair.</summary>
  97. /// <param name="nameFileIdPairs">Updated IEnumerable of SpriteNameFileIdPair.</param>
  98. void SetNameFileIdPairs(IEnumerable<SpriteNameFileIdPair> nameFileIdPairs);
  99. }
  100. /// <summary>Data provider that provides the outline data for SpriteRect.</summary>
  101. /// <remarks>The outline data is used to tessellate a Sprite's mesh.</remarks>
  102. public interface ISpriteOutlineDataProvider
  103. {
  104. /// <summary>Given a GUID, returns the outline data used for tessellating the SpriteRect.</summary>
  105. /// <param name="guid">GUID of the SpriteRect.</param>
  106. /// <returns>Outline data for theSpriteRect.</returns>
  107. List<Vector2[]> GetOutlines(GUID guid);
  108. /// <summary>Given a GUID, sets the outline data used for tessellating the SpriteRect.</summary>
  109. /// <param name="guid">GUID of the SpriteRect.</param>
  110. /// <param name="data">Outline data for theSpriteRect.</param>
  111. void SetOutlines(GUID guid, List<Vector2[]> data);
  112. /// <summary>Given a GUID, returns the tessellation detail.Tessellation value should be between 0 to 1.</summary>
  113. /// <param name = "guid" > GUID of the SpriteRect.</param>
  114. /// <returns>The tessellation value.</returns>
  115. float GetTessellationDetail(GUID guid);
  116. /// <summary>Given a GUID, sets the tessellation detail.Tessellation value should be between 0 to 1.</summary>
  117. /// <param name = "guid" > GUID of the SpriteRect.</param>
  118. /// <param name="value">The tessellation value.</param>
  119. void SetTessellationDetail(GUID guid, float value);
  120. }
  121. /// <summary>Data provider that provides the Physics outline data for SpriteRect.</summary>
  122. /// <remarks>Uses the outline data to generate the Sprite's Physics shape for Polygon Collider 2D.</remarks>
  123. public interface ISpritePhysicsOutlineDataProvider
  124. {
  125. /// <summary>Given a GUID, returns the Physics outline data used for the SpriteRect.</summary>
  126. /// <param name="guid">GUID of the SpriteRect.</param>
  127. /// <returns>Physics outline data for the SpriteRect.</returns>
  128. List<Vector2[]> GetOutlines(GUID guid);
  129. /// <summary>Given a GUID, sets the Physics outline data used for the SpriteRect.</summary>
  130. /// <param name="guid">GUID of the SpriteRect.</param>
  131. /// <param name="data">Physics outline data for the SpriteRect.</param>
  132. void SetOutlines(GUID guid, List<Vector2[]> data);
  133. /// <summary>Given a GUID, returns the tessellation detail.Tessellation value should be between 0 to 1.</summary>
  134. /// <param name = "guid" > GUID of the SpriteRect.</param>
  135. /// <returns>The tessellation value.</returns>
  136. float GetTessellationDetail(GUID guid);
  137. /// <summary>Given a GUID, sets the tessellation detail.Tessellation value should be between 0 to 1.</summary>
  138. /// <param name = "guid" > GUID of the SpriteRect.</param>
  139. /// <param name="value">The tessellation value.</param>
  140. void SetTessellationDetail(GUID guid, float value);
  141. }
  142. /// <summary>Data provider that provides texture data needed for Sprite Editor Window.</summary>
  143. public interface ITextureDataProvider
  144. {
  145. /// <summary>Texture2D representation of the data provider.</summary>
  146. Texture2D texture { get; }
  147. /// <summary>Texture2D that represents the preview for ITextureDataProvider.texture.</summary>
  148. Texture2D previewTexture { get; }
  149. /// <summary>The actual width and height of the texture data.</summary>
  150. /// <param name="width">Out value for width.</param>
  151. /// <param name="height">Out value for height.</param>
  152. void GetTextureActualWidthAndHeight(out int width , out int height);
  153. /// <summary>Readable version of ITextureProvider.texture.</summary>
  154. /// <returns>Texture2D that is readable.</returns>
  155. Texture2D GetReadableTexture2D();
  156. }
  157. /// <summary>Data provider that provides secoondary texture data needed for Sprite Editor Window.</summary>
  158. public interface ISecondaryTextureDataProvider
  159. {
  160. /// <summary>
  161. /// Get set method for an array of SecondarySpriteTexture in the Data Provider
  162. /// </summary>
  163. SecondarySpriteTexture[] textures { get; set; }
  164. }
  165. /// <summary>A structure that contains meta data about vertices in a Sprite.</summary>
  166. [Serializable]
  167. public struct Vertex2DMetaData
  168. {
  169. /// <summary>The position of the vertex.</summary>
  170. public Vector2 position;
  171. /// <summary>The BoneWeight of the vertex.</summary>
  172. public BoneWeight boneWeight;
  173. }
  174. /// <summary>Data Provider interface that deals with Sprite mesh data.</summary>
  175. public interface ISpriteMeshDataProvider
  176. {
  177. /// <summary>Returns the list of vertex datas for the corresponding Sprite ID.</summary>
  178. /// <param name = "guid" > Sprite ID.</param>
  179. Vertex2DMetaData[] GetVertices(GUID guid);
  180. /// <summary>Sets a new list of vertices for the corresponding Sprite ID.</summary>
  181. /// <param name = "guid" > Sprite ID.</param>
  182. void SetVertices(GUID guid, Vertex2DMetaData[] vertices);
  183. /// <summary>Returns the list of mesh index for the corresponding Sprite ID.</summary>
  184. /// <param name = "guid" > Sprite ID.</param>
  185. int[] GetIndices(GUID guid);
  186. /// <summary>Sets a new list of indices for the corresponding Sprite ID.</summary>
  187. /// <param name = "guid" > Sprite ID.</param>
  188. void SetIndices(GUID guid, int[] indices);
  189. /// <summary>Returns the list of mesh edges for the corresponding Sprite ID.</summary>
  190. /// <param name = "guid" > Sprite ID.</param>
  191. Vector2Int[] GetEdges(GUID guid);
  192. /// <summary>Sets a new list of edges for the corresponding Sprite ID.</summary>
  193. /// <param name = "guid" > Sprite ID.</param>
  194. void SetEdges(GUID guid, Vector2Int[] edges);
  195. }
  196. /// <summary>
  197. /// Data Provider interface that allows to get and set custom Sprite data.
  198. /// </summary>
  199. internal interface ISpriteCustomDataProvider
  200. {
  201. /// <summary>
  202. /// Gets all available keys.
  203. /// </summary>
  204. /// <returns>Collection of all available keys.</returns>
  205. public IEnumerable<string> GetKeys();
  206. /// <summary>
  207. /// Sets the custom data at the given key.
  208. /// </summary>
  209. /// <param name="key">Name of the key.</param>
  210. /// <param name="data">Value.</param>
  211. public void SetData(string key, string data);
  212. /// <summary>
  213. /// Removes the custom data at the given key.
  214. /// </summary>
  215. /// <param name="key">Name of the key.</param>
  216. public void RemoveData(string key);
  217. /// <summary>
  218. /// Gets the custom data at the given key.
  219. /// </summary>
  220. /// <param name="key">Name of the key.</param>
  221. /// <param name="data">Value.</param>
  222. /// <returns>True if the value retrieved successfully.</returns>
  223. public bool GetData(string key, out string data);
  224. }
  225. }