facebook twitter hatena line email

「Unity/EditorWindow/TextMeshProUGUIのMaterialPresetをサイズ別に変更」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行17: 行17:
 
     private Material mediumMaterial;
 
     private Material mediumMaterial;
 
     private Material largeMaterial;
 
     private Material largeMaterial;
     private Material extraLargeMaterial; // 追加
+
     private Material extraLargeMaterial;
  
 
     [MenuItem("Tools/Apply TMP Material by Size")]
 
     [MenuItem("Tools/Apply TMP Material by Size")]

2025年3月17日 (月) 04:09時点における版

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using System.IO;

public class ApplyTMPMaterialBySize : EditorWindow
{
    private string directoryPathScenes = "Assets/Scenes";
    private string directoryPathPrefabs = "Assets/Prefabs";
    private bool processScenes = true;
    private bool processPrefabs = true;

    private Material smallMaterial;
    private Material mediumMaterial;
    private Material largeMaterial;
    private Material extraLargeMaterial;

    [MenuItem("Tools/Apply TMP Material by Size")]
    public static void ShowWindow()
    {
        GetWindow<ApplyTMPMaterialBySize>("Apply TMP Material by Size");
    }

    private void OnGUI()
    {
        GUILayout.Label("Material Settings by Font Size", EditorStyles.boldLabel);

        smallMaterial = (Material)EditorGUILayout.ObjectField("Small (< 20)", smallMaterial, typeof(Material), false);
        mediumMaterial = (Material)EditorGUILayout.ObjectField("Medium (20-39)", mediumMaterial, typeof(Material), false);
        largeMaterial = (Material)EditorGUILayout.ObjectField("Large (40-59)", largeMaterial, typeof(Material), false);
        extraLargeMaterial = (Material)EditorGUILayout.ObjectField("Extra Large (>= 60)", extraLargeMaterial, typeof(Material), false); // 追加

        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("Apply Material Preset by Font Size"))
        {
            ApplyMaterialPreset();
        }
    }

    private void ApplyMaterialPreset()
    {
        if (smallMaterial == null || mediumMaterial == null || largeMaterial == null || extraLargeMaterial == null)
        {
            Debug.LogError("Please assign all Material Presets.");
            return;
        }

        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 applying Material Presets based on font size.");
    }

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

        foreach (string scenePath in sceneFiles)
        {
            Scene scene = EditorSceneManager.OpenScene(scenePath);
            if (!scene.IsValid()) continue;

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

            foreach (var textMesh in textMeshObjects)
            {
                Material selectedMaterial = GetMaterialForFontSize(textMesh.fontSize);
                if (textMesh.fontMaterial != selectedMaterial)
                {
                    textMesh.fontMaterial = selectedMaterial;
                    sceneModified = true;
                    Debug.Log($"Applied {selectedMaterial.name} to {textMesh.gameObject.name} (Size: {textMesh.fontSize}) in {scenePath}");
                }
            }

            if (sceneModified)
            {
                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) continue;

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

            foreach (var textMesh in textMeshObjects)
            {
                Material selectedMaterial = GetMaterialForFontSize(textMesh.fontSize);
                if (textMesh.fontMaterial != selectedMaterial)
                {
                    textMesh.fontMaterial = selectedMaterial;
                    prefabModified = true;
                    Debug.Log($"Applied {selectedMaterial.name} to {textMesh.gameObject.name} (Size: {textMesh.fontSize}) in {prefabPath}");
                }
            }

            if (prefabModified)
            {
                PrefabUtility.SavePrefabAsset(prefab);
            }
        }
    }

    private Material GetMaterialForFontSize(float fontSize)
    {
        if (fontSize < 20) return smallMaterial;
        if (fontSize < 40) return mediumMaterial;
        if (fontSize < 60) return largeMaterial;
        return extraLargeMaterial; // 60以上なら特大マテリアル
    }
}