Last active
January 13, 2020 04:17
-
-
Save sukedon/661468699ba26d57902bcc490d3460aa to your computer and use it in GitHub Desktop.
特定のフォントを別のフォントに置き換えるやつ
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; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.UI; | |
public class FontReplaceWindow : EditorWindow | |
{ | |
private static Font _replaceBeforeFont; | |
private static Font _replaceAfterFont; | |
private const string BeforeFontFieldName = "置き換え対象のフォント"; | |
private const string AfterFontFieldName = "置き換え後のフォント"; | |
private const string ReplacePrefabButtonName = "Prefabのフォントを置き換える"; | |
private const string ReplaceSceneButtonName = "Sceneのフォントを置き換える"; | |
private const string PrefabProgressTitle = "Prefab内のフォント差し替え中"; | |
private const string SceneProgressTitle = "Scene内のフォント差し替え中"; | |
[MenuItem("Tools/Replace specific fonts")] | |
public static void ShowWindow() | |
{ | |
GetWindow(typeof(FontReplaceWindow), true, "Font差し替え"); | |
} | |
void OnGUI() | |
{ | |
if (EditorApplication.isCompiling) | |
{ | |
GUILayout.Label("コンパイル中..."); | |
return; | |
} | |
_replaceBeforeFont = EditorGUILayout.ObjectField(BeforeFontFieldName, _replaceBeforeFont, typeof(Font), true) as Font; | |
_replaceAfterFont = EditorGUILayout.ObjectField(AfterFontFieldName, _replaceAfterFont, typeof(Font), true) as Font; | |
if (GUILayout.Button(ReplacePrefabButtonName)) | |
{ | |
if (!ValidateFont()) | |
{ | |
return; | |
} | |
HandlePrefab<Text>(PrefabProgressTitle,ReplaceFont); | |
} | |
if (GUILayout.Button(ReplaceSceneButtonName)) | |
{ | |
if (!ValidateFont()) | |
{ | |
return; | |
} | |
HandleScene(SceneProgressTitle, ReplaceLabelsFontInScene); | |
} | |
} | |
private bool ValidateFont() | |
{ | |
return _replaceBeforeFont != null && _replaceAfterFont != null; | |
} | |
public static void HandlePrefab<T>(string progressTitle,Action<T> actionForComponent) | |
{ | |
var directory = new System.IO.DirectoryInfo(Application.dataPath); | |
var files = directory.GetFiles("*.prefab", System.IO.SearchOption.AllDirectories); | |
try | |
{ | |
for(int i = 0; i < files.Length; i++) | |
{ | |
var file = files[i]; | |
var filePath = file.ToString().Replace(Application.dataPath, ""); | |
var path = "Assets/" + filePath; | |
var go = PrefabUtility.LoadPrefabContents(path); | |
EditorUtility.DisplayProgressBar(progressTitle, $"{i}/{files.Length},{file.FullName}",(float)i/(float)files.Length); | |
foreach (var component in go.GetComponentsInChildren<T>(true)) | |
{ | |
actionForComponent.Invoke(component); | |
} | |
PrefabUtility.SaveAsPrefabAsset(go, path); | |
PrefabUtility.UnloadPrefabContents(go); | |
} | |
EditorUtility.ClearProgressBar(); | |
} | |
catch (Exception e) | |
{ | |
STDebug.LogError(e); | |
EditorUtility.ClearProgressBar(); | |
} | |
} | |
private static void HandleScene(string progressTitle, Func<bool> actionForScene) | |
{ | |
try | |
{ | |
string currentScene = EditorApplication.currentScene; | |
string[] sceneGuids = AssetDatabase.FindAssets("t:Scene"); | |
for (int i = 0; i < sceneGuids.Length; ++i) | |
{ | |
string guid = sceneGuids[i]; | |
string path = AssetDatabase.GUIDToAssetPath(guid); | |
EditorUtility.DisplayProgressBar(progressTitle, path, (float) i / (float) sceneGuids.Length); | |
EditorApplication.OpenScene(path); | |
if (actionForScene.Invoke()) | |
{ | |
EditorApplication.SaveScene(); | |
} | |
} | |
EditorUtility.ClearProgressBar(); | |
if (!string.IsNullOrEmpty(currentScene)) | |
{ | |
EditorApplication.OpenScene(currentScene); | |
} | |
} | |
catch (Exception e) | |
{ | |
STDebug.LogError(e); | |
EditorUtility.ClearProgressBar(); | |
} | |
} | |
private static bool ReplaceLabelsFontInScene() | |
{ | |
var texts = FindObjectsOfType(typeof(Text)) as Text[]; | |
if (texts != null && texts.Length == 0) | |
{ | |
return false; | |
} | |
foreach (Text text in texts) | |
{ | |
ReplaceFont(text); | |
} | |
return true; | |
} | |
private static void ReplaceFont(Text text) | |
{ | |
if (text == null) | |
{ | |
return; | |
} | |
if (text.font == _replaceBeforeFont) | |
{ | |
text.font = _replaceAfterFont; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment