facebook twitter hatena line email

Unity/EditorWindow/TextMeshProUGUIのMaterialPresetをサイズ別に変更

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using TMPro;
using System.IO;

public class ReplaceTextAutoSize : EditorWindow
{
    private string directoryPathScenes = "Assets/Scenes";  // シーンのデフォルトパス
    private string directoryPathPrefabs = "Assets/Prefabs"; // プレハブのデフォルトパス
    private bool processScenes = true;  // シーン処理のチェックボックス
    private bool processPrefabs = true;  // プレハブ処理のチェックボックス

    [SerializeField] private Material smallMaterial;
    [SerializeField] private Material mediumMaterial;
    [SerializeField] private Material largeMaterial;
    [SerializeField] private Material extraLargeMaterial;

    [MenuItem("Tools/AutoSize TextMeshProUGUI")]
    public static void ShowWindow()
    {
        GetWindow<ReplaceTextAutoSize>("AutoSize TextMeshProUGUI");
    }

    private void OnGUI()
    {
        GUILayout.Label("Directory Settings", EditorStyles.boldLabel);

        processScenes = EditorGUILayout.Toggle("Process Scenes", processScenes);
        if (processScenes)
        {
            directoryPathScenes = EditorGUILayout.TextField("Scenes Directory Path", directoryPathScenes);
        }

        processPrefabs = EditorGUILayout.Toggle("Process Prefabs", processPrefabs);
        if (processPrefabs)
        {
            directoryPathPrefabs = EditorGUILayout.TextField("Prefabs Directory Path", directoryPathPrefabs);
        }

        if (GUILayout.Button("Enable AutoSize in Selected Items"))
        {
            EnableAutoSizeInScenesAndPrefabs();
        }
    }

    private void EnableAutoSizeInScenesAndPrefabs()
    {
        Scene currentScene = EditorSceneManager.GetActiveScene();
        string currentScenePath = currentScene.path;

        if (currentScene.isDirty)
        {
            EditorSceneManager.SaveScene(currentScene);
        }

        if (processScenes)
        {
            ProcessScenes();
        }

        if (processPrefabs)
        {
            ProcessPrefabs();
        }

        if (!string.IsNullOrEmpty(currentScenePath))
        {
            EditorSceneManager.OpenScene(currentScenePath);
        }

        Debug.Log("Finished enabling AutoSize for TextMeshProUGUI in all selected items.");
    }

    private void ProcessScenes()
    {
        string[] sceneFiles = Directory.GetFiles(directoryPathScenes, "*.unity", SearchOption.AllDirectories);

        foreach (string scenePath in sceneFiles)
        {
            Scene scene = EditorSceneManager.OpenScene(scenePath);
            if (!scene.IsValid())
            {
                Debug.LogError($"Invalid scene path: {scenePath}");
                continue;
            }

            TextMeshProUGUI[] textMeshObjects = GameObject.FindObjectsOfType<TextMeshProUGUI>();

            foreach (var textMesh in textMeshObjects)
            {
                if (!textMesh.enableAutoSizing)
                {
                    textMesh.enableAutoSizing = true;
                    textMesh.fontSizeMax = textMesh.fontSize;
                    textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
                }
                textMesh.fontMaterial = GetMaterialForFontSize(textMesh.fontSize);
            }

            EditorSceneManager.MarkSceneDirty(scene);
            EditorSceneManager.SaveScene(scene);
        }
    }

    private void ProcessPrefabs()
    {
        string[] prefabFiles = Directory.GetFiles(directoryPathPrefabs, "*.prefab", SearchOption.AllDirectories);

        foreach (string prefabPath in prefabFiles)
        {
            GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
            if (prefab == null)
            {
                Debug.LogError($"Invalid prefab path: {prefabPath}");
                continue;
            }

            TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);

            foreach (var textMesh in textMeshObjects)
            {
                if (!textMesh.enableAutoSizing)
                {
                    textMesh.enableAutoSizing = true;
                    textMesh.fontSizeMax = textMesh.fontSize;
                    textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
                }
                textMesh.fontMaterial = GetMaterialForFontSize(textMesh.fontSize);
            }

            PrefabUtility.SavePrefabAsset(prefab);
        }
    }

    private Material GetMaterialForFontSize(float fontSize)
    {
        if (fontSize < 25) return smallMaterial;
        if (fontSize < 50) return mediumMaterial;
        if (fontSize < 75) return largeMaterial;
        return extraLargeMaterial;
    }
}