facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==TextMeshProUGUIのAutoSizeをonに変更するツール== <pre> using UnityEditor; using UnityEngine; using UnityEditor.SceneManagement; using UnityEngine.SceneManageme...」)
 
(TextMeshProUGUIのAutoSizeをonに変更するツール)
行1: 行1:
 
==TextMeshProUGUIのAutoSizeをonに変更するツール==
 
==TextMeshProUGUIのAutoSizeをonに変更するツール==
 +
全てのシーンのTextMeshProUGUIのAutoSizeをonに変更するツールで、
 +
現在のFontSizeをMaxサイズにし、Maxの1/10をMinサイズとする。Minの小数点以下は切り捨てる。
 +
 
<pre>
 
<pre>
 
using UnityEditor;
 
using UnityEditor;

2024年9月22日 (日) 08:02時点における版

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

全てのシーンの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 directoryPath = "Assets/Scenes";  // シーンのデフォルトパス

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

    private void OnGUI()
    {
        GUILayout.Label("Directory Settings", EditorStyles.boldLabel);
        directoryPath = EditorGUILayout.TextField("Directory Path", directoryPath);

        if (GUILayout.Button("Enable AutoSize in Scenes"))
        {
            EnableAutoSizeInScenes();
        }
    }

    private void EnableAutoSizeInScenes()
    {
        if (string.IsNullOrEmpty(directoryPath))
        {
            Debug.LogError("Please specify the directory path.");
            return;
        }

        // 現在のシーンを保存し、そのパスを記録
        Scene currentScene = EditorSceneManager.GetActiveScene();
        string currentScenePath = currentScene.path;

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

        // ディレクトリ内のすべてのシーンファイルを取得
        string[] sceneFiles = Directory.GetFiles(directoryPath, "*.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 を有効にし、最大サイズを設定
                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);
        }

        // 最後に元のシーンを再度開く
        if (!string.IsNullOrEmpty(currentScenePath))
        {
            EditorSceneManager.OpenScene(currentScenePath);
        }

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