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.

PSDImporterAPI.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using System;
  2. using UnityEditor.AssetImporters;
  3. using UnityEditor.U2D.Sprites;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.PSD
  6. {
  7. public partial class PSDImporter : ScriptedImporter, ISpriteEditorDataProvider
  8. {
  9. /// <summary>
  10. /// Set this to true if you want texture data to be readable from scripts. Set it to false to prevent scripts from reading texture data.
  11. /// <br/><br/>In order for Texture2D.GetPixel, Texture2D.GetPixels, ImageConversion.EncodeToEXR, ImageConversion.EncodeToJPG, ImageConversion.EncodeToPNG and similar functions to work, the Texture must be readable from scripts. The isReadable setting determines whether scripts can access texture data through these functions.
  12. /// <br/><br/>Textures are not set as readable by default.
  13. /// <br/><br/>When a Texture is not readable, it consumes much less memory because an uncompressed copy of the texture data in system memory is not required after the texture is uploaded to the graphics API. Readable Textures require an uncompressed system memory copy of the texture data so that once edited, the updated texture data can be uploaded to the graphics API.
  14. /// </summary>
  15. public bool isReadable
  16. {
  17. get => m_TextureImporterSettings.readable;
  18. set
  19. {
  20. m_TextureImporterSettings.readable = value;
  21. SetDirty();
  22. }
  23. }
  24. /// <summary>
  25. /// Anisotropic filtering level of the texture.
  26. /// </summary>
  27. public int anisoLevel
  28. {
  29. get => m_TextureImporterSettings.aniso;
  30. set
  31. {
  32. m_TextureImporterSettings.aniso = value;
  33. SetDirty();
  34. }
  35. }
  36. /// <summary>
  37. /// Keeps texture borders the same when generating mipmaps.
  38. /// </summary>
  39. public bool borderMipmap
  40. {
  41. get => m_TextureImporterSettings.borderMipmap;
  42. set
  43. {
  44. m_TextureImporterSettings.borderMipmap = value;
  45. SetDirty();
  46. }
  47. }
  48. /// <summary>
  49. /// Fades out mip levels to a gray color.
  50. /// </summary>
  51. public bool fadeout
  52. {
  53. get => m_TextureImporterSettings.fadeOut;
  54. set
  55. {
  56. m_TextureImporterSettings.fadeOut = value;
  57. SetDirty();
  58. }
  59. }
  60. /// <summary>
  61. /// Filtering mode of the texture.
  62. /// </summary>
  63. public FilterMode filterMode
  64. {
  65. get => m_TextureImporterSettings.filterMode;
  66. set
  67. {
  68. m_TextureImporterSettings.filterMode = value;
  69. SetDirty();
  70. }
  71. }
  72. /// <summary>
  73. /// Mip map bias of the texture.
  74. /// </summary>
  75. public float mipMapBias
  76. {
  77. get => m_TextureImporterSettings.mipmapBias;
  78. set
  79. {
  80. m_TextureImporterSettings.mipmapBias = value;
  81. SetDirty();
  82. }
  83. }
  84. /// <summary>
  85. /// Generate Mip Maps.
  86. /// <br/><br/>Select this to enable mip-map generation. Mipmaps are smaller versions of the Texture that get used when the Texture is very small on screen.
  87. /// </summary>
  88. public bool mipmapEnabled
  89. {
  90. get => m_TextureImporterSettings.mipmapEnabled;
  91. set
  92. {
  93. m_TextureImporterSettings.mipmapEnabled = value;
  94. SetDirty();
  95. }
  96. }
  97. /// <summary>
  98. /// Mip level where texture is faded out completely.
  99. /// </summary>
  100. public int mipmapFadeDistanceEnd
  101. {
  102. get => m_TextureImporterSettings.mipmapFadeDistanceEnd;
  103. set
  104. {
  105. m_TextureImporterSettings.mipmapFadeDistanceEnd = value;
  106. SetDirty();
  107. }
  108. }
  109. /// <summary>
  110. /// Mip level where texture begins to fade out.
  111. /// </summary>
  112. public int mipmapFadeDistanceStart
  113. {
  114. get => m_TextureImporterSettings.mipmapFadeDistanceStart;
  115. set
  116. {
  117. m_TextureImporterSettings.mipmapFadeDistanceEnd = value;
  118. SetDirty();
  119. }
  120. }
  121. /// <summary>
  122. /// Enable mipmap streaming for the texture.
  123. /// <br/><br/>Only load larger mipmaps as needed to render the current game cameras. Requires texture streaming to be enabled in quality settings.
  124. /// </summary>
  125. public bool streamingMipmaps
  126. {
  127. get => m_TextureImporterSettings.streamingMipmaps;
  128. set
  129. {
  130. m_TextureImporterSettings.streamingMipmaps = value;
  131. SetDirty();
  132. }
  133. }
  134. /// <summary>
  135. /// Mipmap streaming priority when there's contention for resources. Positive numbers represent higher priority. Valid range is -128 to 127.
  136. /// </summary>
  137. public int streamingMipmapsPriority
  138. {
  139. get => m_TextureImporterSettings.streamingMipmapsPriority;
  140. set
  141. {
  142. m_TextureImporterSettings.streamingMipmapsPriority = Mathf.Clamp(value, -128, 127);
  143. SetDirty();
  144. }
  145. }
  146. /// <summary>
  147. /// Mip level where texture is faded out completely.
  148. /// </summary>
  149. public TextureImporterMipFilter mipmapFilter
  150. {
  151. get => m_TextureImporterSettings.mipmapFilter;
  152. set
  153. {
  154. m_TextureImporterSettings.mipmapFilter = value;
  155. SetDirty();
  156. }
  157. }
  158. /// <summary>
  159. /// Enables or disables coverage-preserving alpha mipmapping.
  160. /// <br/><br/>Enable this to rescale the alpha values of computed mipmaps so coverage is preserved. This means a higher percentage of pixels passes the alpha test and lower mipmap levels do not become more transparent. This is disabled by default (set to false).
  161. /// </summary>
  162. public bool mipMapsPreserveCoverage
  163. {
  164. get => m_TextureImporterSettings.mipMapsPreserveCoverage;
  165. set
  166. {
  167. m_TextureImporterSettings.mipMapsPreserveCoverage = value;
  168. SetDirty();
  169. }
  170. }
  171. /// <summary>
  172. /// Selects Single or Manual import mode for Sprite textures.
  173. /// </summary>
  174. /// <value>Valid values are SpriteImportMode.Multiple or SpriteImportMode.Single.</value>
  175. /// <exception cref="ArgumentException">Exception when non valid values are set.</exception>
  176. public SpriteImportMode spriteImportMode
  177. {
  178. get { return (SpriteImportMode)m_TextureImporterSettings.spriteMode; }
  179. set
  180. {
  181. if (value == SpriteImportMode.Multiple || value == SpriteImportMode.Single)
  182. {
  183. m_TextureImporterSettings.spriteMode = (int)value;
  184. SetDirty();
  185. }
  186. else
  187. throw new ArgumentException("Invalid value. Valid values are SpriteImportMode.Multiple or SpriteImportMode.Single");
  188. }
  189. }
  190. /// <summary>
  191. /// Sets the type of mesh to ge generated for each Sprites.
  192. /// </summary>
  193. public SpriteMeshType spriteMeshType
  194. {
  195. get { return m_TextureImporterSettings.spriteMeshType; }
  196. set
  197. {
  198. m_TextureImporterSettings.spriteMeshType = value;
  199. SetDirty();
  200. }
  201. }
  202. /// <summary>
  203. /// Which type of texture are we dealing with here.
  204. /// </summary>
  205. /// <value>Valid values are TextureImporterType.Default or TextureImporterType.Sprite.</value>
  206. /// <exception cref="ArgumentException">Exception when non valid values are set.</exception>
  207. public TextureImporterType textureType
  208. {
  209. get { return (TextureImporterType)m_TextureImporterSettings.textureType; }
  210. set
  211. {
  212. if (value == TextureImporterType.Sprite || value == TextureImporterType.Default)
  213. {
  214. m_TextureImporterSettings.textureType = value;
  215. SetDirty();
  216. }
  217. else
  218. throw new ArgumentException("Invalid value. Valid values are TextureImporterType.Sprite or TextureImporterType.Default");
  219. }
  220. }
  221. /// <summary>
  222. /// Texture coordinate wrapping mode.
  223. /// <br/><br/>Using wrapMode sets the same wrapping mode on all axes. Different per-axis wrap modes can be set using wrapModeU, wrapModeV, wrapModeW. Querying the value returns the U axis wrap mode (same as wrapModeU getter).
  224. /// </summary>
  225. public TextureWrapMode wrapMode
  226. {
  227. get => m_TextureImporterSettings.wrapMode;
  228. set
  229. {
  230. m_TextureImporterSettings.wrapMode = value;
  231. SetDirty();
  232. }
  233. }
  234. /// <summary>
  235. /// Texture U coordinate wrapping mode.
  236. /// <br/><br/>Controls wrapping mode along texture U (horizontal) axis.
  237. /// </summary>
  238. public TextureWrapMode wrapModeU
  239. {
  240. get => m_TextureImporterSettings.wrapModeU;
  241. set
  242. {
  243. m_TextureImporterSettings.wrapModeU = value;
  244. SetDirty();
  245. }
  246. }
  247. /// <summary>
  248. /// Texture V coordinate wrapping mode.
  249. /// <br/><br/>Controls wrapping mode along texture V (vertical) axis.
  250. /// </summary>
  251. public TextureWrapMode wrapModeV
  252. {
  253. get => m_TextureImporterSettings.wrapModeV;
  254. set
  255. {
  256. m_TextureImporterSettings.wrapModeV = value;
  257. SetDirty();
  258. }
  259. }
  260. /// <summary>
  261. /// Texture W coordinate wrapping mode for Texture3D.
  262. /// <br/><br/>Controls wrapping mode along texture W (depth, only relevant for Texture3D) axis.
  263. /// </summary>
  264. public TextureWrapMode wrapModeW
  265. {
  266. get => m_TextureImporterSettings.wrapModeW;
  267. set
  268. {
  269. m_TextureImporterSettings.wrapModeW = value;
  270. SetDirty();
  271. }
  272. }
  273. /// <summary>
  274. /// The number of pixels in the sprite that correspond to one unit in world space.
  275. /// </summary>
  276. public float spritePixelsPerUnit
  277. {
  278. get => m_TextureImporterSettings.spritePixelsPerUnit;
  279. set
  280. {
  281. m_TextureImporterSettings.spritePixelsPerUnit = value;
  282. SetDirty();
  283. }
  284. }
  285. /// <summary>
  286. /// Retrieves the platform settings used by the importer for a given build target.
  287. /// </summary>
  288. /// <param name="buildTarget">The build target to query.</param>
  289. /// <returns>TextureImporterPlatformSettings used for importing the texture for the build target.</returns>
  290. public TextureImporterPlatformSettings GetImporterPlatformSettings(BuildTarget buildTarget)
  291. {
  292. return TextureImporterUtilities.GetPlatformTextureSettings(buildTarget, in m_PlatformSettings);
  293. }
  294. /// <summary>
  295. /// Sets the platform settings used by the importer for a given build target.
  296. /// </summary>
  297. /// <param name="setting">TextureImporterPlatformSettings to be used by the importer for the build target indicated by TextureImporterPlatformSettings.</param>
  298. public void SetImporterPlatformSettings(TextureImporterPlatformSettings setting)
  299. {
  300. SetPlatformTextureSettings(setting);
  301. SetDirty();
  302. }
  303. /// <summary>
  304. /// Secondary textures for the imported Sprites.
  305. /// </summary>
  306. public SecondarySpriteTexture[] secondarySpriteTextures
  307. {
  308. get => secondaryTextures;
  309. set
  310. {
  311. secondaryTextures = value;
  312. SetDirty();
  313. }
  314. }
  315. /// <summary>
  316. /// Sets if importer should generate a prefab as sub-asset.
  317. /// To generate a Prefab useMosaicMode needs to be set to true and importer needs to be set to import
  318. /// Sprites in multiple mode.
  319. /// </summary>
  320. public bool useCharacterMode
  321. {
  322. get => m_CharacterMode;
  323. set
  324. {
  325. m_CharacterMode = value;
  326. SetDirty();
  327. }
  328. }
  329. /// <summary>
  330. /// Sets if importer should generate a mosaic texture from the source layers.
  331. /// To generate such texture, the importer needs to be set to import Sprites in multiple mode.
  332. /// </summary>
  333. public bool useMosaicMode
  334. {
  335. get => m_MosaicLayers;
  336. set
  337. {
  338. m_MosaicLayers = value;
  339. SetDirty();
  340. }
  341. }
  342. /// <summary>
  343. /// Sets the padding between each Sprites in the mosaic texture.
  344. /// </summary>
  345. public uint mosiacPadding
  346. {
  347. get => (uint)m_Padding;
  348. set
  349. {
  350. m_Padding = (int)value;
  351. SetDirty();
  352. }
  353. }
  354. /// <summary>
  355. /// Sets the value to increase the Sprite size by.
  356. /// </summary>
  357. public ushort spriteSizeExpand
  358. {
  359. get => m_SpriteSizeExpand;
  360. set
  361. {
  362. m_SpriteSizeExpand = value;
  363. m_SpriteSizeExpandChanged = true;
  364. SetDirty();
  365. }
  366. }
  367. internal TextureImporterSwizzle swizzleR
  368. {
  369. get => m_TextureImporterSettings.swizzleR;
  370. set
  371. {
  372. m_TextureImporterSettings.swizzleR = value;
  373. SetDirty();
  374. }
  375. }
  376. internal TextureImporterSwizzle swizzleG
  377. {
  378. get => m_TextureImporterSettings.swizzleG;
  379. set
  380. {
  381. m_TextureImporterSettings.swizzleG = value;
  382. SetDirty();
  383. }
  384. }
  385. internal TextureImporterSwizzle swizzleB
  386. {
  387. get => m_TextureImporterSettings.swizzleB;
  388. set
  389. {
  390. m_TextureImporterSettings.swizzleB = value;
  391. SetDirty();
  392. }
  393. }
  394. internal TextureImporterSwizzle swizzleA
  395. {
  396. get => m_TextureImporterSettings.swizzleA;
  397. set
  398. {
  399. m_TextureImporterSettings.swizzleA = value;
  400. SetDirty();
  401. }
  402. }
  403. internal bool sRGBTexture
  404. {
  405. get => m_TextureImporterSettings.sRGBTexture;
  406. set
  407. {
  408. m_TextureImporterSettings.sRGBTexture = value;
  409. SetDirty();
  410. }
  411. }
  412. void SetDirty()
  413. {
  414. EditorUtility.SetDirty(this);
  415. }
  416. }
  417. }