4 using System.Collections.Generic;
13 [MenuItem(
"Window/TextMeshPro/Sprite Importer",
false, 2026)]
14 public static void ShowFontAtlasCreatorWindow()
16 var window = GetWindow<TMP_SpriteAssetImporter>();
17 window.titleContent =
new GUIContent(
"Sprite Importer");
21 Texture2D m_SpriteAtlas;
22 SpriteAssetImportFormats m_SpriteDataFormat = SpriteAssetImportFormats.TexturePacker;
25 string m_CreationFeedback;
28 List<TMP_Sprite> m_SpriteInfoList =
new List<TMP_Sprite>();
43 void DrawEditorPanel()
46 GUILayout.Label(
"Import Settings", EditorStyles.boldLabel);
48 EditorGUI.BeginChangeCheck();
51 m_JsonFile = EditorGUILayout.ObjectField(
"Sprite Data Source", m_JsonFile, typeof(TextAsset),
false) as TextAsset;
53 m_SpriteDataFormat = (SpriteAssetImportFormats)EditorGUILayout.EnumPopup(
"Import Format", m_SpriteDataFormat);
56 m_SpriteAtlas = EditorGUILayout.ObjectField(
"Sprite Texture Atlas", m_SpriteAtlas, typeof(Texture2D),
false) as Texture2D;
58 if (EditorGUI.EndChangeCheck())
60 m_CreationFeedback =
string.Empty;
65 GUI.enabled = m_JsonFile !=
null && m_SpriteAtlas !=
null && m_SpriteDataFormat == SpriteAssetImportFormats.TexturePacker;
68 if (GUILayout.Button(
"Create Sprite Asset"))
70 m_CreationFeedback =
string.Empty;
73 if (m_JsonFile !=
null && m_SpriteDataFormat == SpriteAssetImportFormats.TexturePacker)
77 if (sprites !=
null && sprites.frames !=
null && sprites.frames.Count > 0)
79 int spriteCount = sprites.frames.Count;
82 m_CreationFeedback =
"<b>Import Results</b>\n--------------------\n";
83 m_CreationFeedback +=
"<color=#C0ffff><b>" + spriteCount +
"</b></color> Sprites were imported from file.";
96 GUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.Height(60));
98 EditorGUILayout.LabelField(m_CreationFeedback, TMP_UIStyleManager.label);
100 GUILayout.EndVertical();
103 GUI.enabled = m_JsonFile !=
null && m_SpriteAtlas && m_SpriteInfoList !=
null && m_SpriteInfoList.Count > 0;
104 if (GUILayout.Button(
"Save Sprite Asset") && m_JsonFile !=
null)
106 string filePath = EditorUtility.SaveFilePanel(
"Save Sprite Asset File",
new FileInfo(AssetDatabase.GetAssetPath(m_JsonFile)).DirectoryName, m_JsonFile.name,
"asset");
108 if (filePath.Length == 0)
124 List<TMP_Sprite> spriteInfoList =
new List<TMP_Sprite>();
126 for (
int i = 0; i < importedSprites.Count; i++)
131 sprite.name = Path.GetFileNameWithoutExtension(importedSprites[i].filename) ??
"";
132 sprite.hashCode = TMP_TextUtilities.GetSimpleHashCode(sprite.name);
136 int indexOfSeperator = sprite.name.IndexOf(
'-');
137 if (indexOfSeperator != -1)
138 unicode = TMP_TextUtilities.StringToInt(sprite.name.Substring(indexOfSeperator + 1));
140 unicode = TMP_TextUtilities.StringToInt(sprite.name);
142 sprite.unicode = unicode;
144 sprite.x = importedSprites[i].frame.x;
145 sprite.y = m_SpriteAtlas.height - (importedSprites[i].frame.y + importedSprites[i].frame.h);
146 sprite.width = importedSprites[i].frame.w;
147 sprite.height = importedSprites[i].frame.h;
150 sprite.pivot = importedSprites[i].pivot;
153 sprite.xAdvance = sprite.width;
155 sprite.xOffset = 0 - (sprite.width * sprite.pivot.x);
156 sprite.yOffset = sprite.height - (sprite.height * sprite.pivot.y);
158 spriteInfoList.Add(sprite);
161 return spriteInfoList;
171 filePath = filePath.Substring(0, filePath.Length - 6);
173 string dataPath = Application.dataPath;
175 if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1)
177 Debug.LogError(
"You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath +
"\"");
181 string relativeAssetPath = filePath.Substring(dataPath.Length - 6);
182 string dirName = Path.GetDirectoryName(relativeAssetPath);
183 string fileName = Path.GetFileNameWithoutExtension(relativeAssetPath);
184 string pathNoExt = dirName +
"/" + fileName;
188 m_SpriteAsset = CreateInstance<TMP_SpriteAsset>();
189 AssetDatabase.CreateAsset(m_SpriteAsset, pathNoExt +
".asset");
192 m_SpriteAsset.
hashCode = TMP_TextUtilities.GetSimpleHashCode(m_SpriteAsset.name);
195 m_SpriteAsset.spriteSheet = m_SpriteAtlas;
196 m_SpriteAsset.spriteInfoList = m_SpriteInfoList;
209 Shader shader = Shader.Find(
"TextMeshPro/Sprite");
210 Material material =
new Material(shader);
211 material.SetTexture(ShaderUtilities.ID_MainTex, spriteAsset.spriteSheet);
214 material.hideFlags = HideFlags.HideInHierarchy;
215 AssetDatabase.AddObjectToAsset(material, spriteAsset);
224 EditorWindow editorWindow =
this;
226 Vector2 currentWindowSize = editorWindow.minSize;
228 editorWindow.minSize =
new Vector2(Mathf.Max(230, currentWindowSize.x), Mathf.Max(300, currentWindowSize.y));
int hashCode
HashCode based on the name of the asset.
void SetEditorWindowSize()
Limits the minimum size of the editor window.
static void AddDefaultMaterial(TMP_SpriteAsset spriteAsset)
Create and add new default material to sprite asset.
void SaveSpriteAsset(string filePath)
Material material
The material used by this asset.
List< TMP_Sprite > CreateSpriteInfoList(TexturePacker.SpriteDataObject spriteDataObject)