Created
February 1, 2015 18:29
-
-
Save N-Carter/cfc8e92b59c3fc24f57a to your computer and use it in GitHub Desktop.
Custom editor for Unity Font objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
[CustomEditor(typeof(Font))] | |
public class FontEditor : Editor | |
{ | |
Font m_Font; | |
static bool m_TilingControlsVisible; | |
static int m_FirstCharacterIndex = 32; | |
static int m_TilesU = 1; | |
static int m_TilesV = 1; | |
void OnEnable() | |
{ | |
m_TilingControlsVisible = EditorPrefs.GetBool("FontEditor.TilingControlsVisible", false); | |
m_FirstCharacterIndex = EditorPrefs.GetInt("FontEditor.FirstCharacterIndex", 32); | |
m_TilesU = EditorPrefs.GetInt("FontEditor.TilesU", 1); | |
m_TilesV = EditorPrefs.GetInt("FontEditor.TilesV", 1); | |
} | |
void OnDisable() | |
{ | |
EditorPrefs.SetBool("FontEditor.TilingControlsVisible", m_TilingControlsVisible); | |
EditorPrefs.SetInt("FontEditor.FirstCharacterIndex", m_FirstCharacterIndex); | |
EditorPrefs.SetInt("FontEditor.TilesU", m_TilesU); | |
EditorPrefs.SetInt("FontEditor.TilesV", m_TilesV); | |
} | |
public override void OnInspectorGUI() | |
{ | |
m_Font = (Font)target; | |
EditorGUILayout.BeginVertical("Box"); | |
m_TilingControlsVisible = EditorGUILayout.Foldout(m_TilingControlsVisible, "Tiling Controls"); | |
if(m_TilingControlsVisible) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
m_FirstCharacterIndex = Mathf.Max(0, EditorGUILayout.IntField("First Character Index", m_FirstCharacterIndex)); | |
string character = GUILayout.TextField(((char)m_FirstCharacterIndex).ToString(), GUILayout.Width(20)); | |
if(character.Length > 0) | |
m_FirstCharacterIndex = character[0]; | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
EditorGUILayout.PrefixLabel("Tiling"); | |
m_TilesU = Mathf.Max(0, EditorGUILayout.IntField(m_TilesU)); | |
m_TilesV = Mathf.Max(0, EditorGUILayout.IntField(m_TilesV)); | |
EditorGUILayout.EndHorizontal(); | |
if(GUILayout.Button("Configure Character Rects")) | |
{ | |
if(EditorUtility.DisplayDialog("Configure Character Rects", | |
"This will erase all character rects and set them up again from scratch. Are you sure?", | |
"OK", "Cancel")) | |
{ | |
ConfigureCharacterRects(); | |
EditorUtility.SetDirty(m_Font); | |
} | |
} | |
} | |
EditorGUILayout.EndVertical(); | |
EditorGUILayout.Separator(); | |
base.OnInspectorGUI(); | |
} | |
void ConfigureCharacterRects() | |
{ | |
CharacterInfo[] characters = new CharacterInfo[m_TilesU * m_TilesV]; | |
float sizeU = 1.0f / m_TilesU; | |
float sizeV = 1.0f / m_TilesV; | |
for(int v = 0; v < m_TilesV; ++v) | |
{ | |
for(int u = 0; u < m_TilesU; ++u) | |
{ | |
var character = new CharacterInfo(); | |
character.index = m_FirstCharacterIndex + u + v * m_TilesU; | |
character.uv = new Rect(u * sizeU, 1.0f - sizeV - v * sizeV, sizeU, sizeV); | |
character.vert = new Rect(0, 0, 1, -1); | |
character.size = 0; | |
character.width = 1.0f; | |
characters[u + v * m_TilesU] = character; | |
} | |
} | |
m_Font.characterInfo = characters; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment