Last active
August 31, 2018 18:08
-
-
Save BeautyfullCastle/72cf2e691c9a657a8ea2028859d1be66 to your computer and use it in GitHub Desktop.
EditorWindow what can apply one font to all of components that uses font in the currently opened scene and assets.
This file contains 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 System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class FontApplier : EditorWindow | |
{ | |
private static FontApplier assetReferenceFinder = null; | |
private Vector2 scrollPosition; | |
private List<Object> references = new List<Object>(); | |
private Font fontField = null; | |
[MenuItem("Font Applier/Find and Apply Font", false, 0)] | |
private static void FindReferences() | |
{ | |
assetReferenceFinder = GetWindow<FontApplier>(); | |
} | |
private static void Find() | |
{ | |
var textComponents = UnityEngine.Resources.FindObjectsOfTypeAll<Text>(); | |
var textMeshComponents = UnityEngine.Resources.FindObjectsOfTypeAll<TextMesh>(); | |
assetReferenceFinder.references.Clear(); | |
if (textComponents != null) | |
{ | |
foreach (var text in textComponents) | |
{ | |
assetReferenceFinder.references.Add(text); | |
} | |
} | |
if (textMeshComponents != null) | |
{ | |
foreach (var textMesh in textMeshComponents) | |
{ | |
assetReferenceFinder.references.Add(textMesh); | |
} | |
} | |
} | |
private void ApplyFont() | |
{ | |
if (fontField == null) | |
{ | |
fontField = Selection.activeObject as Font ?? UnityEngine.Resources.GetBuiltinResource<Font>("Arial.ttf"); | |
} | |
if (references == null) | |
{ | |
return; | |
} | |
references.ForEach(obj => | |
{ | |
if (obj is Text) | |
{ | |
(obj as Text).font = fontField; | |
} | |
else if (obj is TextMesh) | |
{ | |
(obj as TextMesh).font = fontField; | |
} | |
}); | |
} | |
private void OnGUI() | |
{ | |
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); | |
fontField = EditorGUILayout.ObjectField(fontField, typeof(Font), false) as Font; | |
if (GUILayout.Button("Find Components")) | |
{ | |
Find(); | |
} | |
if (GUILayout.Button("Apply this font")) | |
{ | |
ApplyFont(); | |
} | |
foreach (var reference in references) | |
{ | |
EditorGUILayout.ObjectField(EditorUtility.IsPersistent(reference) ? "Asset: " : "Scene Object: ", reference, typeof(Object), true); | |
} | |
EditorGUILayout.EndScrollView(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment