Skip to content

Instantly share code, notes, and snippets.

@lycoris102
Created September 5, 2016 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lycoris102/9439fd0ad54fe9b96ff419d4b566b8f8 to your computer and use it in GitHub Desktop.
Save lycoris102/9439fd0ad54fe9b96ff419d4b566b8f8 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections.Generic;
using System.Linq;
public class FontReplacer : EditorWindow
{
private Font font;
[MenuItem("Custom/FontReplacer")]
public static void OpenWindow()
{
EditorWindow.GetWindow(typeof(FontReplacer)).Show();
}
void OnGUI()
{
font = EditorGUILayout.ObjectField("Font", font, typeof(Font), true) as Font;
if (font != null)
{
if (GUILayout.Button("Replace font in all scenes"))
{
ReplaceFont(font);
Debug.Log("Replaced font in all scenes.");
}
}
}
static void ReplaceFont(Font font)
{
// 現在開いているシーン群
var sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
var scenePath2ActiveMap = new Dictionary<string, bool>();
for (int i = 0; i < sceneCount; i++)
{
var scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
scenePath2ActiveMap[scene.path] = true;
}
// 全てのシーンを取得し開く
var allScenePaths = AssetDatabase.FindAssets("t:Scene").Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToList();
foreach (var path in allScenePaths)
{
UnityEditor.SceneManagement.EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
}
// Scene上に存在するTextComponentのフォントを置換する
var textComponents = Resources.FindObjectsOfTypeAll(typeof(Text)) as Text[];
foreach (var textComponent in textComponents)
{
textComponent.font = font;
}
foreach (var path in allScenePaths)
{
// シーンを保存
var scene = UnityEngine.SceneManagement.SceneManager.GetSceneByPath(path);
UnityEditor.SceneManagement.EditorSceneManager.SaveScene(scene);
// 予め開いていたシーン群以外を閉じる
if (!scenePath2ActiveMap.ContainsKey(path))
{
UnityEditor.SceneManagement.EditorSceneManager.CloseScene(scene, true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment