facebook twitter hatena line email

「Unity/EditorWindow/TextMeshProUGUIのAutoSizeをonに変更」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(TextMeshProUGUIのAutoSizeをonに変更するツール)
(TextMeshProUGUIのAutoSizeをonに変更するツール)
 
行1: 行1:
 
==TextMeshProUGUIのAutoSizeをonに変更するツール==
 
==TextMeshProUGUIのAutoSizeをonに変更するツール==
全てのシーンのTextMeshProUGUIのAutoSizeをonに変更するツールで、
+
全てのシーンとPrefabについて、TextMeshProUGUIのAutoSizeをonに変更するツールで、
 
現在のFontSizeをMaxサイズにし、Maxの1/10をMinサイズとする。Minは整数化する。
 
現在のFontSizeをMaxサイズにし、Maxの1/10をMinサイズとする。Minは整数化する。
  
行13: 行13:
 
public class ReplaceTextAutoSize : EditorWindow
 
public class ReplaceTextAutoSize : EditorWindow
 
{
 
{
     private string directoryPath = "Assets/Scenes";  // シーンのデフォルトパス
+
     private string directoryPathScenes = "Assets/Scenes";  // シーンのデフォルトパス
 +
    private string directoryPathPrefabs = "Assets/Prefabs"; // プレハブのデフォルトパス
 +
    private bool processScenes = true;  // シーン処理のチェックボックス
 +
    private bool processPrefabs = true;  // プレハブ処理のチェックボックス
  
 
     [MenuItem("Tools/AutoSize TextMeshProUGUI")]
 
     [MenuItem("Tools/AutoSize TextMeshProUGUI")]
行24: 行27:
 
     {
 
     {
 
         GUILayout.Label("Directory Settings", EditorStyles.boldLabel);
 
         GUILayout.Label("Directory Settings", EditorStyles.boldLabel);
        directoryPath = EditorGUILayout.TextField("Directory Path", directoryPath);
 
  
         if (GUILayout.Button("Enable AutoSize in Scenes"))
+
         // シーンのパス入力とチェックボックス
 +
        processScenes = EditorGUILayout.Toggle("Process Scenes", processScenes);
 +
        if (processScenes)
 
         {
 
         {
             EnableAutoSizeInScenes();
+
             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 EnableAutoSizeInScenes()
+
     private void EnableAutoSizeInScenesAndPrefabs()
 
     {
 
     {
         if (string.IsNullOrEmpty(directoryPath))
+
         if (processScenes && string.IsNullOrEmpty(directoryPathScenes))
 
         {
 
         {
             Debug.LogError("Please specify the directory path.");
+
             Debug.LogError("Please specify the scenes directory path.");
 +
            return;
 +
        }
 +
 
 +
        if (processPrefabs && string.IsNullOrEmpty(directoryPathPrefabs))
 +
        {
 +
            Debug.LogError("Please specify the prefabs directory path.");
 
             return;
 
             return;
 
         }
 
         }
行49: 行71:
 
         }
 
         }
  
 +
        // シーンの処理
 +
        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(directoryPath, "*.unity", SearchOption.AllDirectories);
+
         string[] sceneFiles = Directory.GetFiles(directoryPathScenes, "*.unity", SearchOption.AllDirectories);
  
 
         foreach (string scenePath in sceneFiles)
 
         foreach (string scenePath in sceneFiles)
行67: 行112:
 
             foreach (var textMesh in textMeshObjects)
 
             foreach (var textMesh in textMeshObjects)
 
             {
 
             {
                 // AutoSize を有効にし、最大サイズを設定
+
                 // AutoSize が無効のときのみ処理
                 textMesh.enableAutoSizing = true;
+
                 if (!textMesh.enableAutoSizing)
                textMesh.fontSizeMax = textMesh.fontSize;  // 現在のフォントサイズを最大値に設定
+
                {
                // 最小サイズを最大サイズの1/10に設定(整数に丸める)
+
                    textMesh.enableAutoSizing = true;
                textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
+
                    textMesh.fontSizeMax = textMesh.fontSize;  // 現在のフォントサイズを最大値に設定
                Debug.Log($"Enabled AutoSize on: {textMesh.gameObject.name} in scene: {scenePath} with max size: {textMesh.fontSizeMax} and min size: {textMesh.fontSizeMin}");
+
                    // 最小サイズを最大サイズの1/10に設定(整数に丸める)
 +
                    textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
 +
                    Debug.Log($"Enabled AutoSize on: {textMesh.gameObject.name} in scene: {scenePath} with max size: {textMesh.fontSizeMax} and min size: {textMesh.fontSizeMin}");
 +
                }
 
             }
 
             }
  
行79: 行127:
 
             EditorSceneManager.SaveScene(scene);
 
             EditorSceneManager.SaveScene(scene);
 
         }
 
         }
 +
    }
  
         // 最後に元のシーンを再度開く
+
    private void ProcessPrefabs()
         if (!string.IsNullOrEmpty(currentScenePath))
+
    {
 +
         // ディレクトリ内のすべてのプレハブファイルを取得
 +
         string[] prefabFiles = Directory.GetFiles(directoryPathPrefabs, "*.prefab", SearchOption.AllDirectories);
 +
 
 +
        foreach (string prefabPath in prefabFiles)
 
         {
 
         {
             EditorSceneManager.OpenScene(currentScenePath);
+
             GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
        }
+
            if (prefab == null)
 +
            {
 +
                Debug.LogError($"Invalid prefab path: {prefabPath}");
 +
                continue;
 +
            }
  
        Debug.Log("Finished enabling AutoSize for TextMeshProUGUI in all scenes.");
+
            // プレハブ内のすべての TextMeshProUGUI オブジェクトを探す
 +
            TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);
 +
 
 +
            foreach (var textMesh in textMeshObjects)
 +
            {
 +
                // AutoSize が無効のときのみ処理
 +
                if (!textMesh.enableAutoSizing)
 +
                {
 +
                    textMesh.enableAutoSizing = true;
 +
                    textMesh.fontSizeMax = textMesh.fontSize;  // 現在のフォントサイズを最大値に設定
 +
                    // 最小サイズを最大サイズの1/10に設定(整数に丸める)
 +
                    textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
 +
                    Debug.Log($"Enabled AutoSize on: {textMesh.gameObject.name} in prefab: {prefabPath} with max size: {textMesh.fontSizeMax} and min size: {textMesh.fontSizeMin}");
 +
                }
 +
            }
 +
 
 +
            // プレハブを保存する
 +
            PrefabUtility.SavePrefabAsset(prefab);
 +
        }
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>

2024年9月22日 (日) 09:24時点における最新版

TextMeshProUGUIのAutoSizeをonに変更するツール

全てのシーンとPrefabについて、TextMeshProUGUIのAutoSizeをonに変更するツールで、 現在のFontSizeをMaxサイズにし、Maxの1/10をMinサイズとする。Minは整数化する。

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;  // プレハブ処理のチェックボックス

    [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()
    {
        if (processScenes && string.IsNullOrEmpty(directoryPathScenes))
        {
            Debug.LogError("Please specify the scenes directory path.");
            return;
        }

        if (processPrefabs && string.IsNullOrEmpty(directoryPathPrefabs))
        {
            Debug.LogError("Please specify the prefabs directory path.");
            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 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 オブジェクトを探す
            TextMeshProUGUI[] textMeshObjects = GameObject.FindObjectsOfType<TextMeshProUGUI>();

            foreach (var textMesh in textMeshObjects)
            {
                // AutoSize が無効のときのみ処理
                if (!textMesh.enableAutoSizing)
                {
                    textMesh.enableAutoSizing = true;
                    textMesh.fontSizeMax = textMesh.fontSize;  // 現在のフォントサイズを最大値に設定
                    // 最小サイズを最大サイズの1/10に設定(整数に丸める)
                    textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
                    Debug.Log($"Enabled AutoSize on: {textMesh.gameObject.name} in scene: {scenePath} with max size: {textMesh.fontSizeMax} and min size: {textMesh.fontSizeMin}");
                }
            }

            // シーンをダーティーにして保存
            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 オブジェクトを探す
            TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);

            foreach (var textMesh in textMeshObjects)
            {
                // AutoSize が無効のときのみ処理
                if (!textMesh.enableAutoSizing)
                {
                    textMesh.enableAutoSizing = true;
                    textMesh.fontSizeMax = textMesh.fontSize;  // 現在のフォントサイズを最大値に設定
                    // 最小サイズを最大サイズの1/10に設定(整数に丸める)
                    textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10);
                    Debug.Log($"Enabled AutoSize on: {textMesh.gameObject.name} in prefab: {prefabPath} with max size: {textMesh.fontSizeMax} and min size: {textMesh.fontSizeMin}");
                }
            }

            // プレハブを保存する
            PrefabUtility.SavePrefabAsset(prefab);
        }
    }
}