uint GetUnicodeCharacter(string source) { uint unicode; if (source.Length == 1) { unicode = source[0]; } else if (source.Length == 6) { unicode = (uint)TMP_TextUtilities.StringHexToInt(source.Replace("\\u", "")); } else { unicode = (uint)TMP_TextUtilities.StringHexToInt(source.Replace("\\U", "")); } return(unicode); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { SerializedProperty prop_SpriteName = property.FindPropertyRelative("m_Name"); SerializedProperty prop_SpriteNameHashCode = property.FindPropertyRelative("m_HashCode"); SerializedProperty prop_SpriteUnicode = property.FindPropertyRelative("m_Unicode"); SerializedProperty prop_SpriteGlyphIndex = property.FindPropertyRelative("m_GlyphIndex"); SerializedProperty prop_SpriteScale = property.FindPropertyRelative("m_Scale"); GUIStyle style = new GUIStyle(EditorStyles.label); style.richText = true; EditorGUIUtility.labelWidth = 40f; EditorGUIUtility.fieldWidth = 50; Rect rect = new Rect(position.x + 60, position.y, position.width, 49); // Display non-editable fields if (GUI.enabled == false) { // Sprite Character Index int spriteCharacterIndex; int.TryParse(property.displayName.Split(' ')[1], out spriteCharacterIndex); EditorGUI.LabelField(new Rect(rect.x, rect.y, 75f, 18), new GUIContent("Index: <color=#FFFF80>" + spriteCharacterIndex + "</color>"), style); EditorGUI.LabelField(new Rect(rect.x + 75f, rect.y, 120f, 18), new GUIContent("Unicode: <color=#FFFF80>0x" + prop_SpriteUnicode.intValue.ToString("X") + "</color>"), style); EditorGUI.LabelField(new Rect(rect.x + 195f, rect.y, rect.width - 255, 18), new GUIContent("Name: <color=#FFFF80>" + prop_SpriteName.stringValue + "</color>"), style); EditorGUI.LabelField(new Rect(rect.x, rect.y + 18, 120, 18), new GUIContent("Glyph ID: <color=#FFFF80>" + prop_SpriteGlyphIndex.intValue + "</color>"), style); // Draw Sprite Glyph (if exists) DrawSpriteGlyph(position, property); EditorGUI.LabelField(new Rect(rect.x, rect.y + 36, 80, 18), new GUIContent("Scale: <color=#FFFF80>" + prop_SpriteScale.floatValue + "</color>"), style); } else // Display editable fields { // Get a reference to the underlying Sprite Asset TMP_SpriteAsset spriteAsset = property.serializedObject.targetObject as TMP_SpriteAsset; // Sprite Character Index int spriteCharacterIndex; int.TryParse(property.displayName.Split(' ')[1], out spriteCharacterIndex); EditorGUI.LabelField(new Rect(rect.x, rect.y, 75f, 18), new GUIContent("Index: <color=#FFFF80>" + spriteCharacterIndex + "</color>"), style); EditorGUIUtility.labelWidth = 55f; GUI.SetNextControlName("Unicode Input"); EditorGUI.BeginChangeCheck(); string unicode = EditorGUI.DelayedTextField(new Rect(rect.x + 75f, rect.y, 120, 18), "Unicode:", prop_SpriteUnicode.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()) { // Update Unicode value prop_SpriteUnicode.intValue = TMP_TextUtilities.StringHexToInt(unicode); spriteAsset.m_IsSpriteAssetLookupTablesDirty = true; } EditorGUIUtility.labelWidth = 41f; EditorGUI.BeginChangeCheck(); EditorGUI.DelayedTextField(new Rect(rect.x + 195f, rect.y, rect.width - 255, 18), prop_SpriteName, new GUIContent("Name:")); if (EditorGUI.EndChangeCheck()) { // Recompute hashCode for new name prop_SpriteNameHashCode.intValue = TMP_TextUtilities.GetSimpleHashCode(prop_SpriteName.stringValue); spriteAsset.m_IsSpriteAssetLookupTablesDirty = true; } EditorGUIUtility.labelWidth = 59f; EditorGUI.BeginChangeCheck(); EditorGUI.DelayedIntField(new Rect(rect.x, rect.y + 18, 100, 18), prop_SpriteGlyphIndex, new GUIContent("Glyph ID:")); if (EditorGUI.EndChangeCheck()) { spriteAsset.m_IsSpriteAssetLookupTablesDirty = true; } // Draw Sprite Glyph (if exists) DrawSpriteGlyph(position, property); int glyphIndex = prop_SpriteGlyphIndex.intValue; // Reset glyph selection if new character has been selected. if (GUI.enabled && m_GlyphSelectedForEditing != glyphIndex) { m_GlyphSelectedForEditing = -1; } // Display button to edit the glyph data. if (GUI.Button(new Rect(rect.x + 120, rect.y + 18, 75, 18), new GUIContent("Edit Glyph"))) { if (m_GlyphSelectedForEditing == -1) { m_GlyphSelectedForEditing = glyphIndex; } else { m_GlyphSelectedForEditing = -1; } // Button clicks should not result in potential change. GUI.changed = false; } // Show the glyph property drawer if selected if (glyphIndex == m_GlyphSelectedForEditing && GUI.enabled) { if (spriteAsset != null) { // Lookup glyph and draw glyph (if available) int elementIndex = spriteAsset.spriteGlyphTable.FindIndex(item => item.index == glyphIndex); if (elementIndex != -1) { // Get a reference to the Sprite Glyph Table SerializedProperty prop_SpriteGlyphTable = property.serializedObject.FindProperty("m_SpriteGlyphTable"); SerializedProperty prop_SpriteGlyph = prop_SpriteGlyphTable.GetArrayElementAtIndex(elementIndex); SerializedProperty prop_GlyphMetrics = prop_SpriteGlyph.FindPropertyRelative("m_Metrics"); SerializedProperty prop_GlyphRect = prop_SpriteGlyph.FindPropertyRelative("m_GlyphRect"); Rect newRect = EditorGUILayout.GetControlRect(false, 115); EditorGUI.DrawRect(new Rect(newRect.x + 62, newRect.y - 20, newRect.width - 62, newRect.height - 5), new Color(0.1f, 0.1f, 0.1f, 0.45f)); EditorGUI.DrawRect(new Rect(newRect.x + 63, newRect.y - 19, newRect.width - 64, newRect.height - 7), new Color(0.3f, 0.3f, 0.3f, 0.8f)); // Display GlyphRect newRect.x += 65; newRect.y -= 18; newRect.width += 5; EditorGUI.PropertyField(newRect, prop_GlyphRect); // Display GlyphMetrics newRect.y += 45; EditorGUI.PropertyField(newRect, prop_GlyphMetrics); rect.y += 120; } } } EditorGUIUtility.labelWidth = 39f; EditorGUI.PropertyField(new Rect(rect.x, rect.y + 36, 80, 18), prop_SpriteScale, new GUIContent("Scale:")); } }
List <TMP_Sprite> CreateSpriteInfoList(TexturePacker.SpriteDataObject spriteDataObject) #endif { #if TMP_2_1_0_PREVIEW_1_OR_NEWER List <TexturePacker_JsonArray.Frame> importedSprites = spriteDataObject.frames; #else List <TexturePacker.SpriteData> importedSprites = spriteDataObject.frames; #endif List <TMP_Sprite> spriteInfoList = new List <TMP_Sprite>(); for (int i = 0; i < importedSprites.Count; i++) { TMP_Sprite sprite = new TMP_Sprite(); sprite.id = i; sprite.name = Path.GetFileNameWithoutExtension(importedSprites[i].filename) ?? ""; sprite.hashCode = TMP_TextUtilities.GetSimpleHashCode(sprite.name); // Attempt to extract Unicode value from name int unicode; int indexOfSeparator = sprite.name.IndexOf('-'); if (indexOfSeparator != -1) { string substring = sprite.name.Substring(0, indexOfSeparator); #if TMP_1_4_0_OR_NEWER unicode = TMP_TextUtilities.StringHexToInt(substring); #else unicode = TMP_TextUtilities.StringToInt(substring); #endif } else { #if TMP_1_4_0_OR_NEWER unicode = TMP_TextUtilities.StringHexToInt(sprite.name); #else unicode = TMP_TextUtilities.StringToInt(sprite.name); #endif } sprite.unicode = unicode; sprite.x = importedSprites[i].frame.x; sprite.y = m_SpriteAtlas.height - (importedSprites[i].frame.y + importedSprites[i].frame.h); sprite.width = importedSprites[i].frame.w; sprite.height = importedSprites[i].frame.h; //Calculate sprite pivot position sprite.pivot = importedSprites[i].pivot; //Extra Properties //var scaledOffset = (sprite.height * ((m_globalGlyphScale - 1) * 0.5f)) * sprite.pivot.y; sprite.xAdvance = sprite.width; #if TMP_1_4_0_OR_NEWER sprite.scale = 1.0f; #else sprite.scale = m_globalGlyphScale; #endif sprite.xOffset = 0 - (sprite.width * sprite.pivot.x); sprite.yOffset = sprite.height - (sprite.height * sprite.pivot.y); spriteInfoList.Add(sprite); } return(spriteInfoList); }
//TODO: Currently this doesn't work on a folder that has no textures in it. public void LoadCustomFolderSprite(ICustomFolderInfo folder, string path) { if (string.IsNullOrEmpty(path)) { return; } var folderIconPath = Path.Combine(path, "folder.png"); if (!File.Exists(folderIconPath)) { folderIconPath = Path.Combine(path, "folder.jpg"); } if (!File.Exists(folderIconPath)) { folderIconPath = Path.Combine(path, "folder.jpeg"); } if (!File.Exists(folderIconPath)) { return; } // Assign new Sprite Sheet texture to the Sprite Asset. var texture = LoadTexture(folderIconPath); List <TMP_Sprite> spriteInfoList = new List <TMP_Sprite>(); TMP_Sprite sprite = new TMP_Sprite(); sprite.id = 0; sprite.name = Path.GetFileNameWithoutExtension(folderIconPath) ?? ""; sprite.hashCode = TMP_TextUtilities.GetSimpleHashCode(sprite.name); //// Attempt to extract Unicode value from name int unicode; int indexOfSeperator = sprite.name.IndexOf('-'); if (indexOfSeperator != -1) { unicode = TMP_TextUtilities.StringHexToInt(sprite.name.Substring(indexOfSeperator + 1)); } else { unicode = TMP_TextUtilities.StringHexToInt(sprite.name); } sprite.unicode = unicode; sprite.x = 0; sprite.y = 0; sprite.width = texture.width; sprite.height = texture.height; sprite.pivot = new Vector2(0.5f, 0.5f); sprite.xAdvance = sprite.width; sprite.scale = 1.0f; sprite.xOffset = 0 - (sprite.width * sprite.pivot.x); sprite.yOffset = sprite.height; spriteInfoList.Add(sprite); // Create new Sprite Asset using this texture TMP_SpriteAsset spriteAsset = ScriptableObject.CreateInstance <TMP_SpriteAsset>(); // Compute the hash code for the sprite asset. spriteAsset.hashCode = TMP_TextUtilities.GetSimpleHashCode(spriteAsset.name); spriteAsset.spriteSheet = texture; spriteAsset.spriteInfoList = spriteInfoList; // Add new default material for sprite asset. AddDefaultMaterial(spriteAsset); folder.CustomSprite = spriteAsset; }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { SerializedProperty prop_Unicode = property.FindPropertyRelative("m_Unicode"); SerializedProperty prop_GlyphIndex = property.FindPropertyRelative("m_GlyphIndex"); SerializedProperty prop_Scale = property.FindPropertyRelative("m_Scale"); GUIStyle style = new GUIStyle(EditorStyles.label); style.richText = true; EditorGUIUtility.labelWidth = 40f; EditorGUIUtility.fieldWidth = 50; Rect rect = new Rect(position.x + 50, position.y, position.width, 49); // Display non-editable fields if (GUI.enabled == false) { int unicode = prop_Unicode.intValue; EditorGUI.LabelField(new Rect(rect.x, rect.y, 120f, 18), new GUIContent("Unicode: <color=#FFFF80>0x" + unicode.ToString("X") + "</color>"), style); EditorGUI.LabelField(new Rect(rect.x + 115, rect.y, 120f, 18), unicode <= 0xFFFF ? new GUIContent("UTF16: <color=#FFFF80>\\u" + unicode.ToString("X4") + "</color>") : new GUIContent("UTF32: <color=#FFFF80>\\U" + unicode.ToString("X8") + "</color>"), style); EditorGUI.LabelField(new Rect(rect.x, rect.y + 18, 120, 18), new GUIContent("Glyph ID: <color=#FFFF80>" + prop_GlyphIndex.intValue + "</color>"), style); EditorGUI.LabelField(new Rect(rect.x, rect.y + 36, 80, 18), new GUIContent("Scale: <color=#FFFF80>" + prop_Scale.floatValue + "</color>"), style); // Draw Glyph (if exists) DrawGlyph(position, property); } else // Display editable fields { EditorGUIUtility.labelWidth = 55f; GUI.SetNextControlName("Unicode Input"); EditorGUI.BeginChangeCheck(); string unicode = EditorGUI.TextField(new Rect(rect.x, rect.y, 120, 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()) { // Update Unicode value prop_Unicode.intValue = TMP_TextUtilities.StringHexToInt(unicode); } // Cache current glyph index in case it needs to be restored if the new glyph index is invalid. int currentGlyphIndex = prop_GlyphIndex.intValue; EditorGUIUtility.labelWidth = 59f; EditorGUI.BeginChangeCheck(); EditorGUI.DelayedIntField(new Rect(rect.x, rect.y + 18, 100, 18), prop_GlyphIndex, new GUIContent("Glyph ID:")); if (EditorGUI.EndChangeCheck()) { // Get a reference to the font asset TMP_FontAsset fontAsset = property.serializedObject.targetObject as TMP_FontAsset; // Make sure new glyph index is valid. int elementIndex = fontAsset.glyphTable.FindIndex(item => item.index == prop_GlyphIndex.intValue); if (elementIndex == -1) { prop_GlyphIndex.intValue = currentGlyphIndex; } else { fontAsset.m_IsFontAssetLookupTablesDirty = true; } } int glyphIndex = prop_GlyphIndex.intValue; // Reset glyph selection if new character has been selected. if (GUI.enabled && m_GlyphSelectedForEditing != glyphIndex) { m_GlyphSelectedForEditing = -1; } // Display button to edit the glyph data. if (GUI.Button(new Rect(rect.x + 120, rect.y + 18, 75, 18), new GUIContent("Edit Glyph"))) { if (m_GlyphSelectedForEditing == -1) { m_GlyphSelectedForEditing = glyphIndex; } else { m_GlyphSelectedForEditing = -1; } // Button clicks should not result in potential change. GUI.changed = false; } // Show the glyph property drawer if selected if (glyphIndex == m_GlyphSelectedForEditing && GUI.enabled) { // Get a reference to the font asset TMP_FontAsset fontAsset = property.serializedObject.targetObject as TMP_FontAsset; if (fontAsset != null) { // Get the index of the glyph in the font asset glyph table. int elementIndex = fontAsset.glyphTable.FindIndex(item => item.index == glyphIndex); if (elementIndex != -1) { SerializedProperty prop_GlyphTable = property.serializedObject.FindProperty("m_GlyphTable"); SerializedProperty prop_Glyph = prop_GlyphTable.GetArrayElementAtIndex(elementIndex); SerializedProperty prop_GlyphMetrics = prop_Glyph.FindPropertyRelative("m_Metrics"); SerializedProperty prop_GlyphRect = prop_Glyph.FindPropertyRelative("m_GlyphRect"); Rect newRect = EditorGUILayout.GetControlRect(false, 115); EditorGUI.DrawRect(new Rect(newRect.x + 52, newRect.y - 20, newRect.width - 52, newRect.height - 5), new Color(0.1f, 0.1f, 0.1f, 0.45f)); EditorGUI.DrawRect(new Rect(newRect.x + 53, newRect.y - 19, newRect.width - 54, newRect.height - 7), new Color(0.3f, 0.3f, 0.3f, 0.8f)); // Display GlyphRect newRect.x += 55; newRect.y -= 18; newRect.width += 5; EditorGUI.PropertyField(newRect, prop_GlyphRect); // Display GlyphMetrics newRect.y += 45; EditorGUI.PropertyField(newRect, prop_GlyphMetrics); rect.y += 120; } } } EditorGUIUtility.labelWidth = 39f; EditorGUI.PropertyField(new Rect(rect.x, rect.y + 36, 80, 18), prop_Scale, new GUIContent("Scale:")); // Draw Glyph (if exists) DrawGlyph(position, property); } }