「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 UnityEngine; | using UnityEngine; | ||
| + | using UnityEditor.SceneManagement; | ||
using UnityEngine.SceneManagement; | using UnityEngine.SceneManagement; | ||
using TMPro; | using TMPro; | ||
using System.IO; | using System.IO; | ||
| − | public class | + | 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/ | + | [MenuItem("Tools/AutoSize TextMeshProUGUI")] |
public static void ShowWindow() | public static void ShowWindow() | ||
{ | { | ||
| − | GetWindow< | + | GetWindow<ReplaceTextAutoSize>("AutoSize TextMeshProUGUI"); |
} | } | ||
private void OnGUI() | private void OnGUI() | ||
{ | { | ||
| − | GUILayout.Label(" | + | GUILayout.Label("Directory Settings", EditorStyles.boldLabel); |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
processScenes = EditorGUILayout.Toggle("Process Scenes", processScenes); | processScenes = EditorGUILayout.Toggle("Process Scenes", processScenes); | ||
| 行44: | 行41: | ||
} | } | ||
| − | if (GUILayout.Button(" | + | if (GUILayout.Button("Enable AutoSize in Selected Items")) |
{ | { | ||
| − | + | EnableAutoSizeInScenesAndPrefabs(); | |
} | } | ||
} | } | ||
| − | private void | + | private void EnableAutoSizeInScenesAndPrefabs() |
{ | { | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
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 | + | 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; | ||
| + | } | ||
| − | |||
TextMeshProUGUI[] textMeshObjects = GameObject.FindObjectsOfType<TextMeshProUGUI>(); | TextMeshProUGUI[] textMeshObjects = GameObject.FindObjectsOfType<TextMeshProUGUI>(); | ||
foreach (var textMesh in textMeshObjects) | foreach (var textMesh in textMeshObjects) | ||
{ | { | ||
| − | + | if (!textMesh.enableAutoSizing) | |
| − | if (textMesh. | + | |
{ | { | ||
| − | textMesh. | + | 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); | |
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
} | } | ||
| 行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; | ||
| + | } | ||
| − | |||
TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true); | TextMeshProUGUI[] textMeshObjects = prefab.GetComponentsInChildren<TextMeshProUGUI>(true); | ||
foreach (var textMesh in textMeshObjects) | foreach (var textMesh in textMeshObjects) | ||
{ | { | ||
| − | + | if (!textMesh.enableAutoSizing) | |
| − | if (textMesh. | + | |
{ | { | ||
| − | textMesh. | + | textMesh.enableAutoSizing = true; |
| − | + | textMesh.fontSizeMax = textMesh.fontSize; | |
| − | + | textMesh.fontSizeMin = Mathf.RoundToInt(textMesh.fontSizeMax / 10); | |
} | } | ||
| + | textMesh.fontMaterial = GetMaterialForFontSize(textMesh.fontSize); | ||
} | } | ||
| − | + | PrefabUtility.SavePrefabAsset(prefab); | |
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
} | } | ||
| 行140: | 行138: | ||
private Material GetMaterialForFontSize(float fontSize) | private Material GetMaterialForFontSize(float fontSize) | ||
{ | { | ||
| − | if (fontSize < | + | if (fontSize < 25) return smallMaterial; |
| − | if (fontSize < | + | 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;
}
}
