facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「<pre> using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; using TMPro; using System.IO; public class ApplyTMPMate...」)
 
 
(同じ利用者による、間の2版が非表示)
行1: 行1:
 
<pre>
 
<pre>
 
using UnityEditor;
 
using UnityEditor;
using UnityEditor.SceneManagement;
 
 
using UnityEngine;
 
using UnityEngine;
 +
using UnityEditor.SceneManagement;
 
using UnityEngine.SceneManagement;
 
using UnityEngine.SceneManagement;
 
using TMPro;
 
using TMPro;
 
using System.IO;
 
using System.IO;
  
public class ApplyTMPMaterialBySize : EditorWindow
+
public class ReplaceTextAutoSize : EditorWindow
 
{
 
{
     private string directoryPathScenes = "Assets/Scenes";
+
     private string directoryPathScenes = "Assets/Scenes"; // シーンのデフォルトパス
     private string directoryPathPrefabs = "Assets/Prefabs";
+
     private string directoryPathPrefabs = "Assets/Prefabs"; // プレハブのデフォルトパス
     private bool processScenes = true;
+
     private bool processScenes = true; // シーン処理のチェックボックス
     private bool processPrefabs = true;
+
     private bool processPrefabs = true; // プレハブ処理のチェックボックス
  
     private Material smallMaterial;
+
     [SerializeField] private Material smallMaterial;
     private Material mediumMaterial;
+
     [SerializeField] private Material mediumMaterial;
     private Material largeMaterial;
+
     [SerializeField] private Material largeMaterial;
 +
    [SerializeField] private Material extraLargeMaterial;
  
     [MenuItem("Tools/Apply TMP Material by Size")]
+
     [MenuItem("Tools/AutoSize TextMeshProUGUI")]
 
     public static void ShowWindow()
 
     public static void ShowWindow()
 
     {
 
     {
         GetWindow<ApplyTMPMaterialBySize>("Apply TMP Material by Size");
+
         GetWindow<ReplaceTextAutoSize>("AutoSize TextMeshProUGUI");
 
     }
 
     }
  
 
     private void OnGUI()
 
     private void OnGUI()
 
     {
 
     {
         GUILayout.Label("Material Settings by Font Size", EditorStyles.boldLabel);
+
         GUILayout.Label("Directory Settings", 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)", largeMaterial, typeof(Material), false);
+
  
 
         processScenes = EditorGUILayout.Toggle("Process Scenes", processScenes);
 
         processScenes = EditorGUILayout.Toggle("Process Scenes", processScenes);
行44: 行41:
 
         }
 
         }
  
         if (GUILayout.Button("Apply Material Preset by Font Size"))
+
         if (GUILayout.Button("Enable AutoSize in Selected Items"))
 
         {
 
         {
             ApplyMaterialPreset();
+
             EnableAutoSizeInScenesAndPrefabs();
 
         }
 
         }
 
     }
 
     }
  
     private void ApplyMaterialPreset()
+
     private void EnableAutoSizeInScenesAndPrefabs()
 
     {
 
     {
        if (smallMaterial == null || mediumMaterial == null || largeMaterial == null)
 
        {
 
            Debug.LogError("Please assign all Material Presets.");
 
            return;
 
        }
 
 
 
         Scene currentScene = EditorSceneManager.GetActiveScene();
 
         Scene currentScene = EditorSceneManager.GetActiveScene();
 
         string currentScenePath = currentScene.path;
 
         string currentScenePath = currentScene.path;
行66: 行57:
 
         }
 
         }
  
         if (processScenes) ProcessScenes();
+
         if (processScenes)
         if (processPrefabs) ProcessPrefabs();
+
        {
 +
            ProcessScenes();
 +
        }
 +
 
 +
         if (processPrefabs)
 +
        {
 +
            ProcessPrefabs();
 +
        }
  
 
         if (!string.IsNullOrEmpty(currentScenePath))
 
         if (!string.IsNullOrEmpty(currentScenePath))
行74: 行72:
 
         }
 
         }
  
         Debug.Log("Finished applying Material Presets based on font size.");
+
         Debug.Log("Finished enabling AutoSize for TextMeshProUGUI in all selected items.");
 
     }
 
     }
  
行84: 行82:
 
         {
 
         {
 
             Scene scene = EditorSceneManager.OpenScene(scenePath);
 
             Scene scene = EditorSceneManager.OpenScene(scenePath);
             if (!scene.IsValid()) continue;
+
             if (!scene.IsValid())
 +
            {
 +
                Debug.LogError($"Invalid scene path: {scenePath}");
 +
                continue;
 +
            }
  
            bool sceneModified = false;
 
 
             TextMeshProUGUI[] textMeshObjects = GameObject.FindObjectsOfType<TextMeshProUGUI>();
 
             TextMeshProUGUI[] textMeshObjects = GameObject.FindObjectsOfType<TextMeshProUGUI>();
  
 
             foreach (var textMesh in textMeshObjects)
 
             foreach (var textMesh in textMeshObjects)
 
             {
 
             {
                Material selectedMaterial = GetMaterialForFontSize(textMesh.fontSize);
+
                 if (!textMesh.enableAutoSizing)
                 if (textMesh.fontMaterial != selectedMaterial)
+
 
                 {
 
                 {
                     textMesh.fontMaterial = selectedMaterial;
+
                     textMesh.enableAutoSizing = true;
                     sceneModified = true;
+
                     textMesh.fontSizeMax = textMesh.fontSize;
                     Debug.Log($"Applied {selectedMaterial.name} to {textMesh.gameObject.name} (Size: {textMesh.fontSize}) in {scenePath}");
+
                     textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
 
                 }
 
                 }
 +
                textMesh.fontMaterial = GetMaterialForFontSize(textMesh.fontSize);
 
             }
 
             }
  
             if (sceneModified)
+
             EditorSceneManager.MarkSceneDirty(scene);
            {
+
            EditorSceneManager.SaveScene(scene);
                EditorSceneManager.MarkSceneDirty(scene);
+
                EditorSceneManager.SaveScene(scene);
+
            }
+
 
         }
 
         }
 
     }
 
     }
行115: 行113:
 
         {
 
         {
 
             GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
 
             GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
             if (prefab == null) continue;
+
             if (prefab == null)
 +
            {
 +
                Debug.LogError($"Invalid prefab path: {prefabPath}");
 +
                continue;
 +
            }
  
            bool prefabModified = false;
 
 
             TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);
 
             TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);
  
 
             foreach (var textMesh in textMeshObjects)
 
             foreach (var textMesh in textMeshObjects)
 
             {
 
             {
                Material selectedMaterial = GetMaterialForFontSize(textMesh.fontSize);
+
                 if (!textMesh.enableAutoSizing)
                 if (textMesh.fontMaterial != selectedMaterial)
+
 
                 {
 
                 {
                     textMesh.fontMaterial = selectedMaterial;
+
                     textMesh.enableAutoSizing = true;
                     prefabModified = true;
+
                     textMesh.fontSizeMax = textMesh.fontSize;
                     Debug.Log($"Applied {selectedMaterial.name} to {textMesh.gameObject.name} (Size: {textMesh.fontSize}) in {prefabPath}");
+
                     textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
 
                 }
 
                 }
 +
                textMesh.fontMaterial = GetMaterialForFontSize(textMesh.fontSize);
 
             }
 
             }
  
             if (prefabModified)
+
             PrefabUtility.SavePrefabAsset(prefab);
            {
+
                PrefabUtility.SavePrefabAsset(prefab);
+
            }
+
 
         }
 
         }
 
     }
 
     }
行140: 行138:
 
     private Material GetMaterialForFontSize(float fontSize)
 
     private Material GetMaterialForFontSize(float fontSize)
 
     {
 
     {
         if (fontSize < 20) return smallMaterial;
+
         if (fontSize < 25) return smallMaterial;
         if (fontSize < 40) return mediumMaterial;
+
         if (fontSize < 50) return mediumMaterial;
         return largeMaterial;
+
         if (fontSize < 75) return largeMaterial;
 +
        return extraLargeMaterial;
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>

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

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;
    }
}