private void Execute() { var assets = AssetDatabase.FindAssets("t:Sprite", new string[] { m_Path }); if (assets.Length == 0) { Debug.Log("Could not find any assets!"); return; } for (int i = 0; i < m_SpriteAsset.spriteInfoList.Count; i++) { var info = m_SpriteAsset.spriteInfoList[i]; var path = AssetDatabase.GUIDToAssetPath(assets[i]); var fileName = Path.GetFileNameWithoutExtension(path); try { if (!string.IsNullOrEmpty(fileName)) { info.unicode = int.Parse(fileName, System.Globalization.NumberStyles.HexNumber); } } catch (Exception) { Debug.Log($"Could not replace unicode for asset ath path: {path}"); } } m_SpriteAsset.UpdateLookupTables(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
private IEnumerator LoadUserSpritesIE() { if (!File.Exists(Application.persistentDataPath + "/user_sprites.png")) { byte[] bits = placeholderUserSprites.EncodeToPNG(); File.WriteAllBytes(Application.persistentDataPath + "/user_sprites.png", bits); } yield return(new WaitForEndOfFrame()); if (!File.Exists(Application.persistentDataPath + "/user_sprites.png")) { yield break; } WWW image = new WWW(Application.persistentDataPath + "/user_sprites.png"); yield return(image); // Create Texture stex = new Texture2D(1, 1); image.LoadImageIntoTexture(stex); // adjust texture settings stex.name = "loaded_sprites"; stex.Apply(); // Apply to sprite asset spriteAsset.spriteSheet = stex; spriteAsset.UpdateLookupTables(); }
private static void PushSprite(EmojiTexture emojiTex) { if (currentEmojiIndex >= SHEET_TILES * SHEET_TILES) { var newSheet = CreateTMP_SpriteAsset(); rootEmojiAsset.fallbackSpriteAssets.Add(newSheet); currentEmojiAsset = newSheet; currentEmojiIndex = 0; } int row = currentEmojiIndex % SHEET_TILES; int column = currentEmojiIndex / SHEET_TILES; if (EmojiTexture.CanCopyTextures) { Graphics.CopyTexture(emojiTex, 0, 0, 0, 0, EMOJI_SIZE, EMOJI_SIZE, currentEmojiAsset.spriteSheet, 0, 0, row * EMOJI_SIZE, (SHEET_SIZE)-((column + 1) * EMOJI_SIZE)); } else { //If we can't copy on the GPU, we copy on the CPU var pixels = ((Texture2D)emojiTex).GetPixels32(0); ((Texture2D)currentEmojiAsset.spriteSheet).SetPixels32( row * EMOJI_SIZE, (SHEET_SIZE)-((column + 1) * EMOJI_SIZE), EMOJI_SIZE, EMOJI_SIZE, pixels, 0); //Free CPU copy of the texture (mark as non readable) if it's full var makeNoLongerReadable = currentEmojiIndex == SHEET_TILES * SHEET_TILES - 1; if (makeNoLongerReadable) { ((Texture2D)currentEmojiAsset.spriteSheet).Apply(false, makeNoLongerReadable); } textureNeedsApply = !makeNoLongerReadable; } TMP_Sprite tmp_Sprite = new TMP_Sprite(); tmp_Sprite.hashCode = TMP_TextUtilities.GetSimpleHashCode(emojiTex.Text); tmp_Sprite.height = EMOJI_SIZE; tmp_Sprite.id = currentEmojiIndex; tmp_Sprite.name = emojiTex.Text; tmp_Sprite.pivot = Vector2.one * 0.5f; tmp_Sprite.scale = 1; tmp_Sprite.unicode = emojiTexture.Unicode; tmp_Sprite.width = EMOJI_SIZE; tmp_Sprite.x = row * EMOJI_SIZE; tmp_Sprite.xAdvance = EMOJI_SIZE; tmp_Sprite.xOffset = 0; tmp_Sprite.y = (SHEET_SIZE)-((column + 1) * EMOJI_SIZE); tmp_Sprite.yOffset = EMOJI_SIZE * 0.9f; currentEmojiAsset.spriteInfoList.Add(tmp_Sprite); currentEmojiAsset.UpdateLookupTables(); currentEmojiIndex++; }
void FixLookupTable(TMP_SpriteAsset spriteAtlas, float globalGlyphScale, bool fixOffset) { #if TMP_1_4_0_OR_NEWER if (spriteAtlas == null) { return; } var changed = false; for (int i = 0; i < spriteAtlas.spriteCharacterTable.Count; i++) { var sprite = spriteAtlas.spriteCharacterTable[i]; var requiredIndex = (uint)i; if (sprite != null && sprite.glyphIndex != requiredIndex) { sprite.glyphIndex = requiredIndex; changed = true; } } //Update GlyphTable Index (fix bug in some textmesh pro versions) and Update Global Scale for (int i = 0; i < spriteAtlas.spriteGlyphTable.Count; i++) { var glyph = spriteAtlas.spriteGlyphTable[i]; //glyph.index = (uint)i; //Change metrics if required if (glyph != null && m_CreatedWithEmojiJson) { changed = true; glyph.scale = globalGlyphScale; //Change metrics based in Scale var newOffset = glyph.metrics.height * 0.75f; var metrics = glyph.metrics; //Fix offset scale (really dont know why this bug happens so we must fix this) metrics.horizontalBearingY = newOffset; glyph.metrics = metrics; } } if (changed) { spriteAtlas.UpdateLookupTables(); } EditorUtility.SetDirty(spriteAtlas); #endif }
void SaveSpriteAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1) { 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 + "\""); return; } string relativeAssetPath = filePath.Substring(dataPath.Length - 6); string dirName = Path.GetDirectoryName(relativeAssetPath); string fileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string pathNoExt = dirName + "/" + fileName; // Create new Sprite Asset using this texture m_SpriteAsset = CreateInstance <TMP_EmojiSpriteAsset>(); AssetDatabase.CreateAsset(m_SpriteAsset, pathNoExt + ".asset"); // Compute the hash code for the sprite asset. m_SpriteAsset.hashCode = TMP_TextUtilities.GetSimpleHashCode(m_SpriteAsset.name); // Assign new Sprite Sheet texture to the Sprite Asset. m_SpriteAsset.spriteSheet = m_SpriteAtlas; m_SpriteAsset.spriteInfoList = m_SpriteInfoList; // Add new default material for sprite asset. AddDefaultMaterial(m_SpriteAsset); #if TMP_1_4_0_OR_NEWER //Upgrade Lookup tables (we must set Version to Empty before) FieldInfo versionField = typeof(TMP_SpriteAsset).GetField("m_Version", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (versionField != null) { versionField.SetValue(m_SpriteAsset, string.Empty); } m_SpriteAsset.UpdateLookupTables(); if (m_SpriteInfoList != null) { FixLookupTable(m_SpriteAsset, m_globalGlyphScale, m_CreatedWithEmojiJson); } #endif }
public override void OnInspectorGUI() { //Debug.Log("OnInspectorGUI Called."); Event currentEvent = Event.current; string evt_cmd = currentEvent.commandName; // Get Current Event CommandName to check for Undo Events serializedObject.Update(); // TEXTMESHPRO SPRITE INFO PANEL #region Display Sprite Asset Face Info Rect rect = EditorGUILayout.GetControlRect(false, 24); GUI.Label(rect, new GUIContent("<b>Face Info</b> - v" + m_SpriteAsset.version), TMP_UIStyleManager.sectionHeader); rect.x += rect.width - 132f; rect.y += 2; rect.width = 130f; rect.height = 18f; if (GUI.Button(rect, new GUIContent("Update Sprite Asset"))) { TMP_SpriteAssetMenu.UpdateSpriteAsset(m_SpriteAsset); } EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(m_PointSizeProperty); EditorGUILayout.PropertyField(m_ScaleProperty); //EditorGUILayout.PropertyField(m_LineHeightProperty); EditorGUILayout.PropertyField(m_AscentLineProperty); EditorGUILayout.PropertyField(m_BaselineProperty); EditorGUILayout.PropertyField(m_DescentLineProperty); EditorGUILayout.Space(); #endregion // ATLAS TEXTURE & MATERIAL #region Display Atlas Texture and Material rect = EditorGUILayout.GetControlRect(false, 24); if (GUI.Button(rect, new GUIContent("<b>Atlas & Material</b>"), TMP_UIStyleManager.sectionHeader)) { UI_PanelState.spriteAtlasInfoPanel = !UI_PanelState.spriteAtlasInfoPanel; } GUI.Label(rect, (UI_PanelState.spriteAtlasInfoPanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel); if (UI_PanelState.spriteAtlasInfoPanel) { EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(m_spriteAtlas_prop, new GUIContent("Sprite Atlas")); if (EditorGUI.EndChangeCheck()) { // Assign the new sprite atlas texture to the current material Texture2D tex = m_spriteAtlas_prop.objectReferenceValue as Texture2D; if (tex != null) { Material mat = m_material_prop.objectReferenceValue as Material; if (mat != null) { mat.mainTexture = tex; } } } EditorGUILayout.PropertyField(m_material_prop, new GUIContent("Default Material")); EditorGUILayout.Space(); } #endregion // FALLBACK SPRITE ASSETS #region Display Sprite Fallbacks rect = EditorGUILayout.GetControlRect(false, 24); EditorGUI.indentLevel = 0; if (GUI.Button(rect, new GUIContent("<b>Fallback Sprite Assets</b>", "Select the Sprite Assets that will be searched and used as fallback when a given sprite is missing from this sprite asset."), TMP_UIStyleManager.sectionHeader)) { UI_PanelState.fallbackSpriteAssetPanel = !UI_PanelState.fallbackSpriteAssetPanel; } GUI.Label(rect, (UI_PanelState.fallbackSpriteAssetPanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel); if (UI_PanelState.fallbackSpriteAssetPanel) { m_fallbackSpriteAssetList.DoLayoutList(); EditorGUILayout.Space(); } #endregion // SPRITE CHARACTER TABLE #region Display Sprite Character Table EditorGUI.indentLevel = 0; rect = EditorGUILayout.GetControlRect(false, 24); if (GUI.Button(rect, new GUIContent("<b>Sprite Character Table</b>", "List of sprite characters contained in this sprite asset."), TMP_UIStyleManager.sectionHeader)) { UI_PanelState.spriteCharacterTablePanel = !UI_PanelState.spriteCharacterTablePanel; } GUI.Label(rect, (UI_PanelState.spriteCharacterTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel); if (UI_PanelState.spriteCharacterTablePanel) { int arraySize = m_SpriteCharacterTableProperty.arraySize; int itemsPerPage = 10; // Display Glyph Management Tools EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.ExpandWidth(true)); { // Search Bar implementation #region DISPLAY SEARCH BAR EditorGUILayout.BeginHorizontal(); { EditorGUIUtility.labelWidth = 110f; EditorGUI.BeginChangeCheck(); string searchPattern = EditorGUILayout.TextField("Sprite Search", m_CharacterSearchPattern, "SearchTextField"); if (EditorGUI.EndChangeCheck() || m_IsCharacterSearchDirty) { if (string.IsNullOrEmpty(searchPattern) == false) { //GUIUtility.keyboardControl = 0; m_CharacterSearchPattern = searchPattern.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim(); // Search Glyph Table for potential matches SearchCharacterTable(m_CharacterSearchPattern, ref m_CharacterSearchList); } else { m_CharacterSearchPattern = null; } m_IsCharacterSearchDirty = false; } string styleName = string.IsNullOrEmpty(m_CharacterSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton"; if (GUILayout.Button(GUIContent.none, styleName)) { GUIUtility.keyboardControl = 0; m_CharacterSearchPattern = string.Empty; } } EditorGUILayout.EndHorizontal(); #endregion // Display Page Navigation if (!string.IsNullOrEmpty(m_CharacterSearchPattern)) { arraySize = m_CharacterSearchList.Count; } // Display Page Navigation DisplayPageNavigation(ref m_CurrentCharacterPage, arraySize, itemsPerPage); } EditorGUILayout.EndVertical(); if (arraySize > 0) { // Display each SpriteInfo entry using the SpriteInfo property drawer. for (int i = itemsPerPage * m_CurrentCharacterPage; i < arraySize && i < itemsPerPage * (m_CurrentCharacterPage + 1); i++) { // Define the start of the selection region of the element. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); int elementIndex = i; if (!string.IsNullOrEmpty(m_CharacterSearchPattern)) { elementIndex = m_CharacterSearchList[i]; } SerializedProperty spriteCharacterProperty = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(elementIndex); EditorGUILayout.BeginVertical(EditorStyles.helpBox); { EditorGUI.BeginDisabledGroup(i != m_selectedElement); { EditorGUILayout.PropertyField(spriteCharacterProperty); } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndVertical(); // Define the end of the selection region of the element. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); // Check for Item selection Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y); if (DoSelectionCheck(selectionArea)) { if (m_selectedElement == i) { m_selectedElement = -1; } else { m_selectedElement = i; GUIUtility.keyboardControl = 0; } } // Draw & Handle Section Area if (m_selectedElement == i) { // Draw selection highlight TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255)); // Draw options to MoveUp, MoveDown, Add or Remove Sprites Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f); controlRect.width /= 8; // Move sprite up. bool guiEnabled = GUI.enabled; if (i == 0) { GUI.enabled = false; } if (GUI.Button(controlRect, "Up")) { SwapCharacterElements(i, i - 1); } GUI.enabled = guiEnabled; // Move sprite down. controlRect.x += controlRect.width; if (i == arraySize - 1) { GUI.enabled = false; } if (GUI.Button(controlRect, "Down")) { SwapCharacterElements(i, i + 1); } GUI.enabled = guiEnabled; // Move sprite to new index controlRect.x += controlRect.width * 2; //if (i == arraySize - 1) { GUI.enabled = false; } m_moveToIndex = EditorGUI.IntField(controlRect, m_moveToIndex); controlRect.x -= controlRect.width; if (GUI.Button(controlRect, "Goto")) { MoveCharacterToIndex(i, m_moveToIndex); } //controlRect.x += controlRect.width; GUI.enabled = guiEnabled; // Add new Sprite controlRect.x += controlRect.width * 4; if (GUI.Button(controlRect, "+")) { m_SpriteCharacterTableProperty.arraySize += 1; int index = m_SpriteCharacterTableProperty.arraySize - 1; SerializedProperty spriteInfo_prop = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(index); // Copy properties of the selected element CopyCharacterSerializedProperty(m_SpriteCharacterTableProperty.GetArrayElementAtIndex(elementIndex), ref spriteInfo_prop); //spriteInfo_prop.FindPropertyRelative("m_Index").intValue = index; serializedObject.ApplyModifiedProperties(); m_IsCharacterSearchDirty = true; } // Delete selected Sprite controlRect.x += controlRect.width; if (m_selectedElement == -1) { GUI.enabled = false; } if (GUI.Button(controlRect, "-")) { m_SpriteCharacterTableProperty.DeleteArrayElementAtIndex(elementIndex); m_selectedElement = -1; serializedObject.ApplyModifiedProperties(); m_IsCharacterSearchDirty = true; return; } } } } DisplayPageNavigation(ref m_CurrentCharacterPage, arraySize, itemsPerPage); EditorGUIUtility.labelWidth = 40f; EditorGUIUtility.fieldWidth = 20f; GUILayout.Space(5f); // GLOBAL TOOLS #region Global Tools /* * GUI.enabled = true; * EditorGUILayout.BeginVertical(EditorStyles.helpBox); * rect = EditorGUILayout.GetControlRect(false, 40); * * float width = (rect.width - 75f) / 4; * EditorGUI.LabelField(rect, "Global Offsets & Scale", EditorStyles.boldLabel); * * * rect.x += 70; * bool old_ChangedState = GUI.changed; * * GUI.changed = false; * m_xOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 0, rect.y + 20, width - 5f, 18), new GUIContent("OX:"), m_xOffset); * if (GUI.changed) UpdateGlobalProperty("m_HorizontalBearingX", m_xOffset); * * m_yOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 1, rect.y + 20, width - 5f, 18), new GUIContent("OY:"), m_yOffset); * if (GUI.changed) UpdateGlobalProperty("m_HorizontalBearingY", m_yOffset); * * m_xAdvance = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 2, rect.y + 20, width - 5f, 18), new GUIContent("ADV."), m_xAdvance); * if (GUI.changed) UpdateGlobalProperty("m_HorizontalAdvance", m_xAdvance); * * m_scale = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 3, rect.y + 20, width - 5f, 18), new GUIContent("SF."), m_scale); * if (GUI.changed) UpdateGlobalProperty("m_Scale", m_scale); * * EditorGUILayout.EndVertical(); * * GUI.changed = old_ChangedState; */ #endregion } #endregion // SPRITE GLYPH TABLE #region Display Sprite Glyph Table EditorGUI.indentLevel = 0; rect = EditorGUILayout.GetControlRect(false, 24); if (GUI.Button(rect, new GUIContent("<b>Sprite Glyph Table</b>", "A list of the SpriteGlyphs contained in this sprite asset."), TMP_UIStyleManager.sectionHeader)) { UI_PanelState.spriteGlyphTablePanel = !UI_PanelState.spriteGlyphTablePanel; } GUI.Label(rect, (UI_PanelState.spriteGlyphTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel); if (UI_PanelState.spriteGlyphTablePanel) { int arraySize = m_SpriteGlyphTableProperty.arraySize; int itemsPerPage = 10; // Display Glyph Management Tools EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.ExpandWidth(true)); { // Search Bar implementation #region DISPLAY SEARCH BAR EditorGUILayout.BeginHorizontal(); { EditorGUIUtility.labelWidth = 110f; EditorGUI.BeginChangeCheck(); string searchPattern = EditorGUILayout.TextField("Sprite Search", m_GlyphSearchPattern, "SearchTextField"); if (EditorGUI.EndChangeCheck() || m_IsGlyphSearchDirty) { if (string.IsNullOrEmpty(searchPattern) == false) { //GUIUtility.keyboardControl = 0; m_GlyphSearchPattern = searchPattern.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim(); // Search Glyph Table for potential matches SearchCharacterTable(m_GlyphSearchPattern, ref m_GlyphSearchList); } else { m_GlyphSearchPattern = null; } m_IsGlyphSearchDirty = false; } string styleName = string.IsNullOrEmpty(m_GlyphSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton"; if (GUILayout.Button(GUIContent.none, styleName)) { GUIUtility.keyboardControl = 0; m_GlyphSearchPattern = string.Empty; } } EditorGUILayout.EndHorizontal(); #endregion // Display Page Navigation if (!string.IsNullOrEmpty(m_GlyphSearchPattern)) { arraySize = m_GlyphSearchList.Count; } // Display Page Navigation DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage); } EditorGUILayout.EndVertical(); if (arraySize > 0) { // Display each SpriteInfo entry using the SpriteInfo property drawer. for (int i = itemsPerPage * m_CurrentGlyphPage; i < arraySize && i < itemsPerPage * (m_CurrentGlyphPage + 1); i++) { // Define the start of the selection region of the element. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); int elementIndex = i; if (!string.IsNullOrEmpty(m_GlyphSearchPattern)) { elementIndex = m_GlyphSearchList[i]; } SerializedProperty spriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(elementIndex); EditorGUILayout.BeginVertical(EditorStyles.helpBox); { EditorGUI.BeginDisabledGroup(i != m_selectedElement); { EditorGUILayout.PropertyField(spriteGlyphProperty); } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndVertical(); // Define the end of the selection region of the element. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); // Check for Item selection Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y); if (DoSelectionCheck(selectionArea)) { if (m_selectedElement == i) { m_selectedElement = -1; } else { m_selectedElement = i; GUIUtility.keyboardControl = 0; } } // Draw & Handle Section Area if (m_selectedElement == i) { // Draw selection highlight TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255)); // Draw options to MoveUp, MoveDown, Add or Remove Sprites Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f); controlRect.width /= 8; // Move sprite up. bool guiEnabled = GUI.enabled; if (i == 0) { GUI.enabled = false; } if (GUI.Button(controlRect, "Up")) { SwapGlyphElements(i, i - 1); } GUI.enabled = guiEnabled; // Move sprite down. controlRect.x += controlRect.width; if (i == arraySize - 1) { GUI.enabled = false; } if (GUI.Button(controlRect, "Down")) { SwapGlyphElements(i, i + 1); } GUI.enabled = guiEnabled; // Move sprite to new index controlRect.x += controlRect.width * 2; //if (i == arraySize - 1) { GUI.enabled = false; } m_moveToIndex = EditorGUI.IntField(controlRect, m_moveToIndex); controlRect.x -= controlRect.width; if (GUI.Button(controlRect, "Goto")) { MoveGlyphToIndex(i, m_moveToIndex); } //controlRect.x += controlRect.width; GUI.enabled = guiEnabled; // Add new Sprite controlRect.x += controlRect.width * 4; if (GUI.Button(controlRect, "+")) { m_SpriteGlyphTableProperty.arraySize += 1; int index = m_SpriteGlyphTableProperty.arraySize - 1; SerializedProperty newSpriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(index); // Copy properties of the selected element CopyGlyphSerializedProperty(m_SpriteGlyphTableProperty.GetArrayElementAtIndex(elementIndex), ref newSpriteGlyphProperty); newSpriteGlyphProperty.FindPropertyRelative("m_Index").intValue = index; serializedObject.ApplyModifiedProperties(); m_IsGlyphSearchDirty = true; //m_SpriteAsset.UpdateLookupTables(); } // Delete selected Sprite controlRect.x += controlRect.width; if (m_selectedElement == -1) { GUI.enabled = false; } if (GUI.Button(controlRect, "-")) { SerializedProperty selectedSpriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(elementIndex); int selectedGlyphIndex = selectedSpriteGlyphProperty.FindPropertyRelative("m_Index").intValue; m_SpriteGlyphTableProperty.DeleteArrayElementAtIndex(elementIndex); // Remove all Sprite Characters referencing this glyph. for (int j = 0; j < m_SpriteCharacterTableProperty.arraySize; j++) { int glyphIndex = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(j).FindPropertyRelative("m_GlyphIndex").intValue; if (glyphIndex == selectedGlyphIndex) { // Remove character m_SpriteCharacterTableProperty.DeleteArrayElementAtIndex(j); } } m_selectedElement = -1; serializedObject.ApplyModifiedProperties(); m_IsGlyphSearchDirty = true; //m_SpriteAsset.UpdateLookupTables(); return; } } } } DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage); EditorGUIUtility.labelWidth = 40f; EditorGUIUtility.fieldWidth = 20f; GUILayout.Space(5f); // GLOBAL TOOLS #region Global Tools GUI.enabled = true; EditorGUILayout.BeginVertical(EditorStyles.helpBox); rect = EditorGUILayout.GetControlRect(false, 40); float width = (rect.width - 75f) / 4; EditorGUI.LabelField(rect, "Global Offsets & Scale", EditorStyles.boldLabel); rect.x += 70; bool old_ChangedState = GUI.changed; GUI.changed = false; m_xOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 0, rect.y + 20, width - 5f, 18), new GUIContent("OX:"), m_xOffset); if (GUI.changed) { UpdateGlobalProperty("m_HorizontalBearingX", m_xOffset); } m_yOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 1, rect.y + 20, width - 5f, 18), new GUIContent("OY:"), m_yOffset); if (GUI.changed) { UpdateGlobalProperty("m_HorizontalBearingY", m_yOffset); } m_xAdvance = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 2, rect.y + 20, width - 5f, 18), new GUIContent("ADV."), m_xAdvance); if (GUI.changed) { UpdateGlobalProperty("m_HorizontalAdvance", m_xAdvance); } m_scale = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 3, rect.y + 20, width - 5f, 18), new GUIContent("SF."), m_scale); if (GUI.changed) { UpdateGlobalProperty("m_Scale", m_scale); } EditorGUILayout.EndVertical(); #endregion GUI.changed = old_ChangedState; } #endregion if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty) { if (m_SpriteAsset.m_IsSpriteAssetLookupTablesDirty || evt_cmd == k_UndoRedo) { m_SpriteAsset.UpdateLookupTables(); } TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, m_SpriteAsset); isAssetDirty = false; EditorUtility.SetDirty(target); } // Clear selection if mouse event was not consumed. GUI.enabled = true; if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0) { m_selectedElement = -1; } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //SerializedProperty prop_fileID = property.FindPropertyRelative("fileID"); SerializedProperty prop_id = property.FindPropertyRelative("id"); SerializedProperty prop_name = property.FindPropertyRelative("name"); SerializedProperty prop_hashCode = property.FindPropertyRelative("hashCode"); SerializedProperty prop_unicode = property.FindPropertyRelative("unicode"); SerializedProperty prop_x = property.FindPropertyRelative("x"); SerializedProperty prop_y = property.FindPropertyRelative("y"); SerializedProperty prop_width = property.FindPropertyRelative("width"); SerializedProperty prop_height = property.FindPropertyRelative("height"); SerializedProperty prop_xOffset = property.FindPropertyRelative("xOffset"); SerializedProperty prop_yOffset = property.FindPropertyRelative("yOffset"); SerializedProperty prop_xAdvance = property.FindPropertyRelative("xAdvance"); SerializedProperty prop_scale = property.FindPropertyRelative("scale"); SerializedProperty prop_sprite = property.FindPropertyRelative("sprite"); // Get a reference to the sprite texture Texture tex = (property.serializedObject.targetObject as TMP_SpriteAsset).spriteSheet; // Return if we don't have a texture assigned to the sprite asset. if (tex == null) { Debug.LogWarning("Please assign a valid Sprite Atlas texture to the [" + property.serializedObject.targetObject.name + "] Sprite Asset.", property.serializedObject.targetObject); return; } Vector2 spriteTexPosition = new Vector2(position.x, position.y); Vector2 spriteSize = new Vector2(65, 65); if (prop_width.floatValue >= prop_height.floatValue) { spriteSize.y = prop_height.floatValue * spriteSize.x / prop_width.floatValue; spriteTexPosition.y += (spriteSize.x - spriteSize.y) / 2; } else { spriteSize.x = prop_width.floatValue * spriteSize.y / prop_height.floatValue; spriteTexPosition.x += (spriteSize.y - spriteSize.x) / 2; } // Compute the normalized texture coordinates Rect texCoords = new Rect(prop_x.floatValue / tex.width, prop_y.floatValue / tex.height, prop_width.floatValue / tex.width, prop_height.floatValue / tex.height); GUI.DrawTextureWithTexCoords(new Rect(spriteTexPosition.x + 5, spriteTexPosition.y + 2.5f, spriteSize.x, spriteSize.y), tex, texCoords, true); // We get Rect since a valid position may not be provided by the caller. Rect rect = new Rect(position.x, position.y, position.width, 49); rect.x += 70; bool isEnabled = GUI.enabled; GUI.enabled = false; EditorGUIUtility.labelWidth = 30f; EditorGUI.PropertyField(new Rect(rect.x + 5f, rect.y, 65f, 18), prop_id, new GUIContent("ID:")); GUI.enabled = isEnabled; EditorGUI.BeginChangeCheck(); EditorGUIUtility.labelWidth = 55f; GUI.SetNextControlName("Unicode Input"); string unicode = EditorGUI.TextField(new Rect(rect.x + 75f, rect.y, 105, 18), "Unicode:", prop_unicode.intValue.ToString("X")); if (GUI.GetNameOfFocusedControl() == "Unicode Input") { //Filter out unwanted characters. char chr = Event.current.character; if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F')) { Event.current.character = '\0'; } if (EditorGUI.EndChangeCheck()) { prop_unicode.intValue = TMP_TextUtilities.StringToInt(unicode); property.serializedObject.ApplyModifiedProperties(); TMP_SpriteAsset spriteAsset = property.serializedObject.targetObject as TMP_SpriteAsset; spriteAsset.UpdateLookupTables(); } } EditorGUIUtility.labelWidth = 45f; EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(new Rect(rect.x + 185f, rect.y, rect.width - 260, 18), prop_name, new GUIContent("Name: " + prop_name.stringValue)); if (EditorGUI.EndChangeCheck()) { Sprite sprite = prop_sprite.objectReferenceValue as Sprite; if (sprite != null) { sprite.name = prop_name.stringValue; } // Recompute hashCode for new name prop_hashCode.intValue = TMP_TextUtilities.GetSimpleHashCode(prop_name.stringValue); // Check to make sure for duplicates property.serializedObject.ApplyModifiedProperties(); // Dictionary needs to be updated since HashCode has changed. //TMP_StyleSheet.Instance.LoadStyleDictionary(); } EditorGUIUtility.labelWidth = 30f; EditorGUIUtility.fieldWidth = 10f; //GUI.enabled = false; float width = (rect.width - 75f) / 4; EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 0, rect.y + 22, width - 5f, 18), prop_x, new GUIContent("X:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 1, rect.y + 22, width - 5f, 18), prop_y, new GUIContent("Y:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 2, rect.y + 22, width - 5f, 18), prop_width, new GUIContent("W:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 3, rect.y + 22, width - 5f, 18), prop_height, new GUIContent("H:")); //GUI.enabled = true; EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 0, rect.y + 44, width - 5f, 18), prop_xOffset, new GUIContent("OX:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 1, rect.y + 44, width - 5f, 18), prop_yOffset, new GUIContent("OY:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 2, rect.y + 44, width - 5f, 18), prop_xAdvance, new GUIContent("Adv.")); EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 3, rect.y + 44, width - 5f, 18), prop_scale, new GUIContent("SF.")); if (EditorGUI.EndChangeCheck()) { //Sprite sprite = prop_sprite.objectReferenceValue as Sprite; //sprite = Sprite.Create(sprite.texture, sprite.rect, new Vector2(0.1f, 0.8f)); //prop_sprite.objectReferenceValue = sprite; //Debug.Log(sprite.bounds); } }
internal static void UpdateSpriteAsset(TMP_SpriteAsset spriteAsset) { // Get a list of all the sprites contained in the texture referenced by the sprite asset. // This only works if the texture is set to sprite mode. string filePath = AssetDatabase.GetAssetPath(spriteAsset.spriteSheet); if (string.IsNullOrEmpty(filePath)) { return; } // Get all the sprites defined in the sprite sheet texture referenced by this sprite asset. Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(filePath).Select(x => x as Sprite).Where(x => x != null).ToArray(); // Return if sprite sheet texture does not have any sprites defined in it. if (sprites.Length == 0) { 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); return; } List <TMP_SpriteGlyph> spriteGlyphTable = spriteAsset.spriteGlyphTable; // Find available glpyh indexes uint[] existingGlyphIndexes = spriteGlyphTable.Select(x => x.index).ToArray(); List <uint> availableGlyphIndexes = new List <uint>(); uint lastGlyphIndex = existingGlyphIndexes.Length > 0 ? existingGlyphIndexes.Last() : 0; int elementIndex = 0; for (uint i = 0; i < lastGlyphIndex; i++) { uint existingGlyphIndex = existingGlyphIndexes[elementIndex]; if (i == existingGlyphIndex) { elementIndex += 1; } else { availableGlyphIndexes.Add(i); } } // Iterate over sprites contained in the updated sprite sheet to identify new and / or modified sprites. for (int i = 0; i < sprites.Length; i++) { Sprite sprite = sprites[i]; // Check if current sprites is already contained in the sprite glyph table of the sprite asset. TMP_SpriteGlyph spriteGlyph = spriteGlyphTable.FirstOrDefault(x => x.sprite == sprite); if (spriteGlyph != null) { // update existing sprite glyph 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) { spriteGlyph.glyphRect = new GlyphRect(sprite.rect); } } else { TMP_SpriteCharacter spriteCharacter; // Check if this sprite potentially exists under the same name in the sprite character table. if (spriteAsset.spriteCharacterTable != null && spriteAsset.spriteCharacterTable.Count > 0) { spriteCharacter = spriteAsset.spriteCharacterTable.FirstOrDefault(x => x.name == sprite.name); spriteGlyph = spriteCharacter != null ? spriteGlyphTable[(int)spriteCharacter.glyphIndex] : null; if (spriteGlyph != null) { // Update sprite reference and data spriteGlyph.sprite = sprite; 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) { spriteGlyph.glyphRect = new GlyphRect(sprite.rect); } } } // Add new Sprite Glyph to the table spriteGlyph = new TMP_SpriteGlyph(); // Get available glyph index if (availableGlyphIndexes.Count > 0) { spriteGlyph.index = availableGlyphIndexes[0]; availableGlyphIndexes.RemoveAt(0); } else { spriteGlyph.index = (uint)spriteGlyphTable.Count; } spriteGlyph.metrics = new GlyphMetrics(sprite.rect.width, sprite.rect.height, -sprite.pivot.x, sprite.rect.height - sprite.pivot.y, sprite.rect.width); spriteGlyph.glyphRect = new GlyphRect(sprite.rect); spriteGlyph.scale = 1.0f; spriteGlyph.sprite = sprite; spriteGlyphTable.Add(spriteGlyph); spriteCharacter = new TMP_SpriteCharacter(0xFFFE, spriteGlyph); spriteCharacter.name = sprite.name; spriteCharacter.scale = 1.0f; spriteAsset.spriteCharacterTable.Add(spriteCharacter); } } // Update Sprite Character Table to replace unicode 0x0 by 0xFFFE for (int i = 0; i < spriteAsset.spriteCharacterTable.Count; i++) { TMP_SpriteCharacter spriteCharacter = spriteAsset.spriteCharacterTable[i]; if (spriteCharacter.unicode == 0) { spriteCharacter.unicode = 0xFFFE; } } // Sort glyph table by glyph index spriteAsset.SortGlyphTable(); spriteAsset.UpdateLookupTables(); TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, spriteAsset); }
public static void CreateSpriteAsset() { Object target = Selection.activeObject; if (target == null || target.GetType() != typeof(Texture2D)) // && target.GetType() != typeof(SpriteAtlas))) { Debug.LogWarning("A texture must first be selected in order to create a TextMesh Pro Sprite Asset."); return; } // Get the path to the selected asset. string filePathWithName = AssetDatabase.GetAssetPath(target); string fileNameWithExtension = Path.GetFileName(filePathWithName); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName); string filePath = filePathWithName.Replace(fileNameWithExtension, ""); // Create new Sprite Asset TMP_SpriteAsset spriteAsset = ScriptableObject.CreateInstance <TMP_SpriteAsset>(); AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset"); spriteAsset.version = "1.1.0"; // Compute the hash code for the sprite asset. spriteAsset.hashCode = TMP_TextUtilities.GetSimpleHashCode(spriteAsset.name); List <TMP_SpriteGlyph> spriteGlyphTable = new List <TMP_SpriteGlyph>(); List <TMP_SpriteCharacter> spriteCharacterTable = new List <TMP_SpriteCharacter>(); if (target.GetType() == typeof(Texture2D)) { Texture2D sourceTex = target as Texture2D; // Assign new Sprite Sheet texture to the Sprite Asset. spriteAsset.spriteSheet = sourceTex; PopulateSpriteTables(sourceTex, ref spriteCharacterTable, ref spriteGlyphTable); spriteAsset.spriteCharacterTable = spriteCharacterTable; spriteAsset.spriteGlyphTable = spriteGlyphTable; // Add new default material for sprite asset. AddDefaultMaterial(spriteAsset); } else if (target.GetType() == typeof(SpriteAtlas)) { //SpriteAtlas spriteAtlas = target as SpriteAtlas; //PopulateSpriteTables(spriteAtlas, ref spriteCharacterTable, ref spriteGlyphTable); //spriteAsset.spriteCharacterTable = spriteCharacterTable; //spriteAsset.spriteGlyphTable = spriteGlyphTable; //spriteAsset.spriteSheet = spriteGlyphTable[0].sprite.texture; //// Add new default material for sprite asset. //AddDefaultMaterial(spriteAsset); } // Update Lookup tables. spriteAsset.UpdateLookupTables(); // Get the Sprites contained in the Sprite Sheet EditorUtility.SetDirty(spriteAsset); //spriteAsset.sprites = sprites; // Set source texture back to Not Readable. //texImporter.isReadable = false; AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(spriteAsset)); // Re-import font asset to get the new updated version. //AssetDatabase.Refresh(); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { SerializedProperty property2 = property.FindPropertyRelative("id"); SerializedProperty serializedProperty = property.FindPropertyRelative("name"); SerializedProperty serializedProperty2 = property.FindPropertyRelative("hashCode"); SerializedProperty serializedProperty3 = property.FindPropertyRelative("unicode"); SerializedProperty serializedProperty4 = property.FindPropertyRelative("x"); SerializedProperty serializedProperty5 = property.FindPropertyRelative("y"); SerializedProperty serializedProperty6 = property.FindPropertyRelative("width"); SerializedProperty serializedProperty7 = property.FindPropertyRelative("height"); SerializedProperty property3 = property.FindPropertyRelative("xOffset"); SerializedProperty property4 = property.FindPropertyRelative("yOffset"); SerializedProperty property5 = property.FindPropertyRelative("xAdvance"); SerializedProperty property6 = property.FindPropertyRelative("scale"); SerializedProperty serializedProperty8 = property.FindPropertyRelative("sprite"); Texture spriteSheet = (property.serializedObject.targetObject as TMP_SpriteAsset).spriteSheet; if (spriteSheet == null) { Debug.LogWarning("Please assign a valid Sprite Atlas texture to the [" + property.serializedObject.targetObject.name + "] Sprite Asset.", property.serializedObject.targetObject); return; } Vector2 vector = new Vector2(65f, 65f); if (serializedProperty6.floatValue >= serializedProperty7.floatValue) { vector.y = serializedProperty7.floatValue * vector.x / serializedProperty6.floatValue; position.y += (vector.x - vector.y) / 2f; } else { vector.x = serializedProperty6.floatValue * vector.y / serializedProperty7.floatValue; position.x += (vector.y - vector.x) / 2f; } GUI.DrawTextureWithTexCoords(texCoords: new Rect(serializedProperty4.floatValue / (float)spriteSheet.width, serializedProperty5.floatValue / (float)spriteSheet.height, serializedProperty6.floatValue / (float)spriteSheet.width, serializedProperty7.floatValue / (float)spriteSheet.height), position: new Rect(position.x + 5f, position.y, vector.x, vector.y), image: spriteSheet, alphaBlend: true); Rect rect = new Rect(position.x, position.y, position.width, 49f); rect.x += 70f; bool enabled = GUI.enabled; GUI.enabled = false; EditorGUIUtility.labelWidth = 30f; EditorGUI.PropertyField(new Rect(rect.x + 5f, rect.y, 65f, 18f), property2, new GUIContent("ID:")); GUI.enabled = enabled; EditorGUI.BeginChangeCheck(); EditorGUIUtility.labelWidth = 55f; GUI.SetNextControlName("Unicode Input"); string s = EditorGUI.TextField(new Rect(rect.x + 75f, rect.y, 105f, 18f), "Unicode:", serializedProperty3.intValue.ToString("X")); if (GUI.GetNameOfFocusedControl() == "Unicode Input") { char character = Event.current.character; if ((character < '0' || character > '9') && (character < 'a' || character > 'f') && (character < 'A' || character > 'F')) { Event.current.character = '\0'; } if (EditorGUI.EndChangeCheck()) { serializedProperty3.intValue = TMP_TextUtilities.StringToInt(s); property.serializedObject.ApplyModifiedProperties(); TMP_SpriteAsset tMP_SpriteAsset = property.serializedObject.targetObject as TMP_SpriteAsset; tMP_SpriteAsset.UpdateLookupTables(); } } EditorGUIUtility.labelWidth = 45f; EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(new Rect(rect.x + 185f, rect.y, rect.width - 260f, 18f), serializedProperty, new GUIContent("Name: " + serializedProperty.stringValue)); if (EditorGUI.EndChangeCheck()) { Sprite sprite = serializedProperty8.objectReferenceValue as Sprite; if (sprite != null) { sprite.name = serializedProperty.stringValue; } serializedProperty2.intValue = TMP_TextUtilities.GetSimpleHashCode(serializedProperty.stringValue); property.serializedObject.ApplyModifiedProperties(); } EditorGUIUtility.labelWidth = 30f; EditorGUIUtility.fieldWidth = 10f; float num = (rect.width - 75f) / 4f; EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 0f, rect.y + 22f, num - 5f, 18f), serializedProperty4, new GUIContent("X:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 1f, rect.y + 22f, num - 5f, 18f), serializedProperty5, new GUIContent("Y:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 2f, rect.y + 22f, num - 5f, 18f), serializedProperty6, new GUIContent("W:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 3f, rect.y + 22f, num - 5f, 18f), serializedProperty7, new GUIContent("H:")); EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 0f, rect.y + 44f, num - 5f, 18f), property3, new GUIContent("OX:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 1f, rect.y + 44f, num - 5f, 18f), property4, new GUIContent("OY:")); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 2f, rect.y + 44f, num - 5f, 18f), property5, new GUIContent("Adv.")); EditorGUI.PropertyField(new Rect(rect.x + 5f + num * 3f, rect.y + 44f, num - 5f, 18f), property6, new GUIContent("SF.")); if (!EditorGUI.EndChangeCheck()) { } }
internal static void UpdateSpriteAsset(TMP_SpriteAsset spriteAsset) { // Get a list of all the sprites contained in the texture referenced by the sprite asset. // This only works if the texture is set to sprite mode. string filePath = AssetDatabase.GetAssetPath(spriteAsset.spriteSheet); if (string.IsNullOrEmpty(filePath)) { return; } // Get all the Sprites sorted Left to Right / Top to Bottom 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(); List <TMP_SpriteGlyph> spriteGlyphTable = spriteAsset.spriteGlyphTable; // Finding available glyph indexes to insert new glyphs into. var tempGlyphTable = spriteGlyphTable.OrderBy(glyph => glyph.index).ToList(); List <uint> availableGlyphIndexes = new List <uint>(); int elementIndex = 0; for (uint i = 0; i < tempGlyphTable[tempGlyphTable.Count - 1].index; i++) { uint currentElementIndex = tempGlyphTable[elementIndex].index; if (i == currentElementIndex) { elementIndex += 1; } else { availableGlyphIndexes.Add(i); } } // Iterate over each of the sprites in the texture to try to match them to existing sprites in the sprite asset. for (int i = 0; i < sprites.Length; i++) { int id = sprites[i].GetInstanceID(); int glyphIndex = spriteGlyphTable.FindIndex(item => item.sprite.GetInstanceID() == id); if (glyphIndex == -1) { // Add new Sprite Glyph to the table Sprite sprite = sprites[i]; TMP_SpriteGlyph spriteGlyph = new TMP_SpriteGlyph(); // Get available glyph index if (availableGlyphIndexes.Count > 0) { spriteGlyph.index = availableGlyphIndexes[0]; availableGlyphIndexes.RemoveAt(0); } else { spriteGlyph.index = (uint)spriteGlyphTable.Count; } spriteGlyph.metrics = new GlyphMetrics(sprite.rect.width, sprite.rect.height, -sprite.pivot.x, sprite.rect.height - sprite.pivot.y, sprite.rect.width); spriteGlyph.glyphRect = new GlyphRect(sprite.rect); spriteGlyph.scale = 1.0f; spriteGlyph.sprite = sprite; spriteGlyphTable.Add(spriteGlyph); TMP_SpriteCharacter spriteCharacter = new TMP_SpriteCharacter(0, spriteGlyph); spriteCharacter.name = sprite.name; spriteCharacter.scale = 1.0f; spriteAsset.spriteCharacterTable.Add(spriteCharacter); } else { // Look for changes in existing Sprite Glyph Sprite sprite = sprites[i]; TMP_SpriteGlyph spriteGlyph = spriteGlyphTable[glyphIndex]; // We only update changes to the sprite position / glyph rect. 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) { spriteGlyph.glyphRect = new GlyphRect(sprite.rect); } } } // Sort glyph table by glyph index spriteAsset.SortGlyphTable(); spriteAsset.UpdateLookupTables(); TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, spriteAsset); }
public static void CreateSpriteAsset() { Object target = Selection.activeObject; // Make sure the selection is a texture. if (target == null || target.GetType() != typeof(Texture2D)) { Debug.LogWarning("A texture which contains sprites must first be selected in order to create a TextMesh Pro Sprite Asset."); return; } Texture2D sourceTex = target as Texture2D; // Get the path to the selected texture. string filePathWithName = AssetDatabase.GetAssetPath(sourceTex); string fileNameWithExtension = Path.GetFileName(filePathWithName); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName); string filePath = filePathWithName.Replace(fileNameWithExtension, ""); // Check if Sprite Asset already exists TMP_SpriteAsset spriteAsset = AssetDatabase.LoadAssetAtPath(filePath + fileNameWithoutExtension + ".asset", typeof(TMP_SpriteAsset)) as TMP_SpriteAsset; bool isNewAsset = spriteAsset == null ? true : false; if (isNewAsset) { // Create new Sprite Asset using this texture spriteAsset = ScriptableObject.CreateInstance <TMP_SpriteAsset>(); AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset"); spriteAsset.version = "1.1.0"; // Compute the hash code for the sprite asset. spriteAsset.hashCode = TMP_TextUtilities.GetSimpleHashCode(spriteAsset.name); // Assign new Sprite Sheet texture to the Sprite Asset. spriteAsset.spriteSheet = sourceTex; List <TMP_SpriteGlyph> spriteGlyphTable = new List <TMP_SpriteGlyph>(); List <TMP_SpriteCharacter> spriteCharacterTable = new List <TMP_SpriteCharacter>(); PopulateSpriteTables(sourceTex, ref spriteCharacterTable, ref spriteGlyphTable); spriteAsset.spriteCharacterTable = spriteCharacterTable; spriteAsset.spriteGlyphTable = spriteGlyphTable; // Add new default material for sprite asset. AddDefaultMaterial(spriteAsset); } //else //{ // spriteAsset.spriteInfoList = UpdateSpriteInfo(spriteAsset); // // Make sure the sprite asset already contains a default material // if (spriteAsset.material == null) // { // // Add new default material for sprite asset. // AddDefaultMaterial(spriteAsset); // } //} // Update Lookup tables. spriteAsset.UpdateLookupTables(); // Get the Sprites contained in the Sprite Sheet EditorUtility.SetDirty(spriteAsset); //spriteAsset.sprites = sprites; // Set source texture back to Not Readable. //texImporter.isReadable = false; AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(spriteAsset)); // Re-import font asset to get the new updated version. //AssetDatabase.Refresh(); }