TMP_SpriteAssetMenu.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using UnityEngine.U2D;
  4. using UnityEditor;
  5. using System.Linq;
  6. using System.IO;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace TMPro.EditorUtilities
  10. {
  11. public static class TMP_SpriteAssetMenu
  12. {
  13. // Add a Context Menu to the Sprite Asset Editor Panel to Create and Add a Default Material.
  14. [MenuItem("CONTEXT/TMP_SpriteAsset/Add Default Material", false, 2200)]
  15. static void CopyTexture(MenuCommand command)
  16. {
  17. TMP_SpriteAsset spriteAsset = (TMP_SpriteAsset)command.context;
  18. // Make sure the sprite asset already contains a default material
  19. if (spriteAsset != null && spriteAsset.material == null)
  20. {
  21. // Add new default material for sprite asset.
  22. AddDefaultMaterial(spriteAsset);
  23. }
  24. }
  25. // Add a Context Menu to the Sprite Asset Editor Panel to update existing sprite assets.
  26. [MenuItem("CONTEXT/TMP_SpriteAsset/Update Sprite Asset", false, 2100)]
  27. static void UpdateSpriteAsset(MenuCommand command)
  28. {
  29. TMP_SpriteAsset spriteAsset = (TMP_SpriteAsset)command.context;
  30. if (spriteAsset == null)
  31. return;
  32. UpdateSpriteAsset(spriteAsset);
  33. }
  34. internal static void UpdateSpriteAsset(TMP_SpriteAsset spriteAsset)
  35. {
  36. // Get a list of all the sprites contained in the texture referenced by the sprite asset.
  37. // This only works if the texture is set to sprite mode.
  38. string filePath = AssetDatabase.GetAssetPath(spriteAsset.spriteSheet);
  39. if (string.IsNullOrEmpty(filePath))
  40. return;
  41. // Get all the sprites defined in the sprite sheet texture referenced by this sprite asset.
  42. Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(filePath).Select(x => x as Sprite).Where(x => x != null).ToArray();
  43. // Return if sprite sheet texture does not have any sprites defined in it.
  44. if (sprites.Length == 0)
  45. {
  46. Debug.Log("Sprite Asset <color=#FFFF80>[" + spriteAsset.name + "]</color>'s atlas texture does not appear to have any sprites defined in it. Use the Unity Sprite Editor to define sprites for this texture.", spriteAsset.spriteSheet);
  47. return;
  48. }
  49. List<TMP_SpriteGlyph> spriteGlyphTable = spriteAsset.spriteGlyphTable;
  50. // Find available glpyh indexes
  51. uint[] existingGlyphIndexes = spriteGlyphTable.Select(x => x.index).ToArray();
  52. List<uint> availableGlyphIndexes = new List<uint>();
  53. uint lastGlyphIndex = existingGlyphIndexes.Length > 0 ? existingGlyphIndexes.Last() : 0;
  54. int elementIndex = 0;
  55. for (uint i = 0; i < lastGlyphIndex; i++)
  56. {
  57. uint existingGlyphIndex = existingGlyphIndexes[elementIndex];
  58. if (i == existingGlyphIndex)
  59. elementIndex += 1;
  60. else
  61. availableGlyphIndexes.Add(i);
  62. }
  63. // Iterate over sprites contained in the updated sprite sheet to identify new and / or modified sprites.
  64. for (int i = 0; i < sprites.Length; i++)
  65. {
  66. Sprite sprite = sprites[i];
  67. // Check if current sprites is already contained in the sprite glyph table of the sprite asset.
  68. TMP_SpriteGlyph spriteGlyph = spriteGlyphTable.FirstOrDefault(x => x.sprite == sprite);
  69. if (spriteGlyph != null)
  70. {
  71. // update existing sprite glyph
  72. if (spriteGlyph.glyphRect.x != sprite.rect.x || spriteGlyph.glyphRect.y != sprite.rect.y || spriteGlyph.glyphRect.width != sprite.rect.width || spriteGlyph.glyphRect.height != sprite.rect.height)
  73. spriteGlyph.glyphRect = new GlyphRect(sprite.rect);
  74. }
  75. else
  76. {
  77. TMP_SpriteCharacter spriteCharacter;
  78. // Check if this sprite potentially exists under the same name in the sprite character table.
  79. if (spriteAsset.spriteCharacterTable != null && spriteAsset.spriteCharacterTable.Count > 0)
  80. {
  81. spriteCharacter = spriteAsset.spriteCharacterTable.FirstOrDefault(x => x.name == sprite.name);
  82. spriteGlyph = spriteCharacter != null ? spriteGlyphTable[(int)spriteCharacter.glyphIndex] : null;
  83. if (spriteGlyph != null)
  84. {
  85. // Update sprite reference and data
  86. spriteGlyph.sprite = sprite;
  87. if (spriteGlyph.glyphRect.x != sprite.rect.x || spriteGlyph.glyphRect.y != sprite.rect.y || spriteGlyph.glyphRect.width != sprite.rect.width || spriteGlyph.glyphRect.height != sprite.rect.height)
  88. spriteGlyph.glyphRect = new GlyphRect(sprite.rect);
  89. }
  90. }
  91. // Add new Sprite Glyph to the table
  92. spriteGlyph = new TMP_SpriteGlyph();
  93. // Get available glyph index
  94. if (availableGlyphIndexes.Count > 0)
  95. {
  96. spriteGlyph.index = availableGlyphIndexes[0];
  97. availableGlyphIndexes.RemoveAt(0);
  98. }
  99. else
  100. spriteGlyph.index = (uint)spriteGlyphTable.Count;
  101. spriteGlyph.metrics = new GlyphMetrics(sprite.rect.width, sprite.rect.height, -sprite.pivot.x, sprite.rect.height - sprite.pivot.y, sprite.rect.width);
  102. spriteGlyph.glyphRect = new GlyphRect(sprite.rect);
  103. spriteGlyph.scale = 1.0f;
  104. spriteGlyph.sprite = sprite;
  105. spriteGlyphTable.Add(spriteGlyph);
  106. spriteCharacter = new TMP_SpriteCharacter(0xFFFE, spriteGlyph);
  107. spriteCharacter.name = sprite.name;
  108. spriteCharacter.scale = 1.0f;
  109. spriteAsset.spriteCharacterTable.Add(spriteCharacter);
  110. }
  111. }
  112. // Update Sprite Character Table to replace unicode 0x0 by 0xFFFE
  113. for (int i = 0; i < spriteAsset.spriteCharacterTable.Count; i++)
  114. {
  115. TMP_SpriteCharacter spriteCharacter = spriteAsset.spriteCharacterTable[i];
  116. if (spriteCharacter.unicode == 0)
  117. spriteCharacter.unicode = 0xFFFE;
  118. }
  119. // Sort glyph table by glyph index
  120. spriteAsset.SortGlyphTable();
  121. spriteAsset.UpdateLookupTables();
  122. TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, spriteAsset);
  123. }
  124. [MenuItem("Assets/Create/TextMeshPro/Sprite Asset", false, 110)]
  125. public static void CreateSpriteAsset()
  126. {
  127. Object target = Selection.activeObject;
  128. if (target == null || target.GetType() != typeof(Texture2D)) // && target.GetType() != typeof(SpriteAtlas)))
  129. {
  130. Debug.LogWarning("A texture must first be selected in order to create a TextMesh Pro Sprite Asset.");
  131. return;
  132. }
  133. // Get the path to the selected asset.
  134. string filePathWithName = AssetDatabase.GetAssetPath(target);
  135. string fileNameWithExtension = Path.GetFileName(filePathWithName);
  136. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName);
  137. string filePath = filePathWithName.Replace(fileNameWithExtension, "");
  138. // Create new Sprite Asset
  139. TMP_SpriteAsset spriteAsset = ScriptableObject.CreateInstance<TMP_SpriteAsset>();
  140. AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset");
  141. spriteAsset.version = "1.1.0";
  142. // Compute the hash code for the sprite asset.
  143. spriteAsset.hashCode = TMP_TextUtilities.GetSimpleHashCode(spriteAsset.name);
  144. List<TMP_SpriteGlyph> spriteGlyphTable = new List<TMP_SpriteGlyph>();
  145. List<TMP_SpriteCharacter> spriteCharacterTable = new List<TMP_SpriteCharacter>();
  146. if (target.GetType() == typeof(Texture2D))
  147. {
  148. Texture2D sourceTex = target as Texture2D;
  149. // Assign new Sprite Sheet texture to the Sprite Asset.
  150. spriteAsset.spriteSheet = sourceTex;
  151. PopulateSpriteTables(sourceTex, ref spriteCharacterTable, ref spriteGlyphTable);
  152. spriteAsset.spriteCharacterTable = spriteCharacterTable;
  153. spriteAsset.spriteGlyphTable = spriteGlyphTable;
  154. // Add new default material for sprite asset.
  155. AddDefaultMaterial(spriteAsset);
  156. }
  157. else if (target.GetType() == typeof(SpriteAtlas))
  158. {
  159. //SpriteAtlas spriteAtlas = target as SpriteAtlas;
  160. //PopulateSpriteTables(spriteAtlas, ref spriteCharacterTable, ref spriteGlyphTable);
  161. //spriteAsset.spriteCharacterTable = spriteCharacterTable;
  162. //spriteAsset.spriteGlyphTable = spriteGlyphTable;
  163. //spriteAsset.spriteSheet = spriteGlyphTable[0].sprite.texture;
  164. //// Add new default material for sprite asset.
  165. //AddDefaultMaterial(spriteAsset);
  166. }
  167. // Update Lookup tables.
  168. spriteAsset.UpdateLookupTables();
  169. // Get the Sprites contained in the Sprite Sheet
  170. EditorUtility.SetDirty(spriteAsset);
  171. //spriteAsset.sprites = sprites;
  172. // Set source texture back to Not Readable.
  173. //texImporter.isReadable = false;
  174. AssetDatabase.SaveAssets();
  175. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(spriteAsset)); // Re-import font asset to get the new updated version.
  176. //AssetDatabase.Refresh();
  177. }
  178. private static void PopulateSpriteTables(Texture source, ref List<TMP_SpriteCharacter> spriteCharacterTable, ref List<TMP_SpriteGlyph> spriteGlyphTable)
  179. {
  180. //Debug.Log("Creating new Sprite Asset.");
  181. string filePath = AssetDatabase.GetAssetPath(source);
  182. // Get all the Sprites sorted by Index
  183. Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(filePath).Select(x => x as Sprite).Where(x => x != null).OrderByDescending(x => x.rect.y).ThenBy(x => x.rect.x).ToArray();
  184. for (int i = 0; i < sprites.Length; i++)
  185. {
  186. Sprite sprite = sprites[i];
  187. TMP_SpriteGlyph spriteGlyph = new TMP_SpriteGlyph();
  188. spriteGlyph.index = (uint)i;
  189. spriteGlyph.metrics = new GlyphMetrics(sprite.rect.width, sprite.rect.height, -sprite.pivot.x, sprite.rect.height - sprite.pivot.y, sprite.rect.width);
  190. spriteGlyph.glyphRect = new GlyphRect(sprite.rect);
  191. spriteGlyph.scale = 1.0f;
  192. spriteGlyph.sprite = sprite;
  193. spriteGlyphTable.Add(spriteGlyph);
  194. TMP_SpriteCharacter spriteCharacter = new TMP_SpriteCharacter(0xFFFE, spriteGlyph);
  195. spriteCharacter.name = sprite.name;
  196. spriteCharacter.scale = 1.0f;
  197. spriteCharacterTable.Add(spriteCharacter);
  198. }
  199. }
  200. private static void PopulateSpriteTables(SpriteAtlas spriteAtlas, ref List<TMP_SpriteCharacter> spriteCharacterTable, ref List<TMP_SpriteGlyph> spriteGlyphTable)
  201. {
  202. // Get number of sprites contained in the sprite atlas.
  203. int spriteCount = spriteAtlas.spriteCount;
  204. Sprite[] sprites = new Sprite[spriteCount];
  205. // Get all the sprites
  206. spriteAtlas.GetSprites(sprites);
  207. for (int i = 0; i < sprites.Length; i++)
  208. {
  209. Sprite sprite = sprites[i];
  210. TMP_SpriteGlyph spriteGlyph = new TMP_SpriteGlyph();
  211. spriteGlyph.index = (uint)i;
  212. spriteGlyph.metrics = new GlyphMetrics(sprite.textureRect.width, sprite.textureRect.height, -sprite.pivot.x, sprite.textureRect.height - sprite.pivot.y, sprite.textureRect.width);
  213. spriteGlyph.glyphRect = new GlyphRect(sprite.textureRect);
  214. spriteGlyph.scale = 1.0f;
  215. spriteGlyph.sprite = sprite;
  216. spriteGlyphTable.Add(spriteGlyph);
  217. TMP_SpriteCharacter spriteCharacter = new TMP_SpriteCharacter(0xFFFE, spriteGlyph);
  218. spriteCharacter.name = sprite.name;
  219. spriteCharacter.scale = 1.0f;
  220. spriteCharacterTable.Add(spriteCharacter);
  221. }
  222. }
  223. /// <summary>
  224. /// Create and add new default material to sprite asset.
  225. /// </summary>
  226. /// <param name="spriteAsset"></param>
  227. private static void AddDefaultMaterial(TMP_SpriteAsset spriteAsset)
  228. {
  229. Shader shader = Shader.Find("TextMeshPro/Sprite");
  230. Material material = new Material(shader);
  231. material.SetTexture(ShaderUtilities.ID_MainTex, spriteAsset.spriteSheet);
  232. spriteAsset.material = material;
  233. material.hideFlags = HideFlags.HideInHierarchy;
  234. AssetDatabase.AddObjectToAsset(material, spriteAsset);
  235. }
  236. // Update existing SpriteInfo
  237. private static List<TMP_Sprite> UpdateSpriteInfo(TMP_SpriteAsset spriteAsset)
  238. {
  239. //Debug.Log("Updating Sprite Asset.");
  240. string filePath = AssetDatabase.GetAssetPath(spriteAsset.spriteSheet);
  241. // Get all the Sprites sorted Left to Right / Top to Bottom
  242. Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(filePath).Select(x => x as Sprite).Where(x => x != null).OrderByDescending(x => x.rect.y).ThenBy(x => x.rect.x).ToArray();
  243. for (int i = 0; i < sprites.Length; i++)
  244. {
  245. Sprite sprite = sprites[i];
  246. // Check if the sprite is already contained in the SpriteInfoList
  247. int index = -1;
  248. if (spriteAsset.spriteInfoList.Count > i && spriteAsset.spriteInfoList[i].sprite != null)
  249. index = spriteAsset.spriteInfoList.FindIndex(item => item.sprite.GetInstanceID() == sprite.GetInstanceID());
  250. // Use existing SpriteInfo if it already exists
  251. TMP_Sprite spriteInfo = index == -1 ? new TMP_Sprite() : spriteAsset.spriteInfoList[index];
  252. Rect spriteRect = sprite.rect;
  253. spriteInfo.x = spriteRect.x;
  254. spriteInfo.y = spriteRect.y;
  255. spriteInfo.width = spriteRect.width;
  256. spriteInfo.height = spriteRect.height;
  257. // Get Sprite Pivot
  258. Vector2 pivot = new Vector2(0 - (sprite.bounds.min.x) / (sprite.bounds.extents.x * 2), 0 - (sprite.bounds.min.y) / (sprite.bounds.extents.y * 2));
  259. // The position of the pivot influences the Offset position.
  260. spriteInfo.pivot = new Vector2(0 - pivot.x * spriteRect.width, spriteRect.height - pivot.y * spriteRect.height);
  261. if (index == -1)
  262. {
  263. // Find the next available index for this Sprite
  264. int[] ids = spriteAsset.spriteInfoList.Select(item => item.id).ToArray();
  265. int id = 0;
  266. for (int j = 0; j < ids.Length; j++ )
  267. {
  268. if (ids[0] != 0) break;
  269. if (j > 0 && (ids[j] - ids[j - 1]) > 1)
  270. {
  271. id = ids[j - 1] + 1;
  272. break;
  273. }
  274. id = j + 1;
  275. }
  276. spriteInfo.sprite = sprite;
  277. spriteInfo.name = sprite.name;
  278. spriteInfo.hashCode = TMP_TextUtilities.GetSimpleHashCode(spriteInfo.name);
  279. spriteInfo.id = id;
  280. spriteInfo.xAdvance = spriteRect.width;
  281. spriteInfo.scale = 1.0f;
  282. spriteInfo.xOffset = spriteInfo.pivot.x;
  283. spriteInfo.yOffset = spriteInfo.pivot.y;
  284. spriteAsset.spriteInfoList.Add(spriteInfo);
  285. // Sort the Sprites by ID
  286. spriteAsset.spriteInfoList = spriteAsset.spriteInfoList.OrderBy(s => s.id).ToList();
  287. }
  288. else
  289. {
  290. spriteAsset.spriteInfoList[index] = spriteInfo;
  291. }
  292. }
  293. return spriteAsset.spriteInfoList;
  294. }
  295. }
  296. }