facebook twitter hatena line email

Unity/EditorWindow/TextMeshProUGUIのMaterialPresetを変更

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using System.IO;

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

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

    private void OnGUI()
    {
        GUILayout.Label("Settings", EditorStyles.boldLabel);

        materialPreset = (Material)EditorGUILayout.ObjectField("Material Preset", materialPreset, 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"))
        {
            ApplyMaterialPreset();
        }
    }

    private void ApplyMaterialPreset()
    {
        if (materialPreset == null)
        {
            Debug.LogError("Please assign a Material Preset.");
            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 Preset to all selected TextMeshProUGUI objects.");
    }

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

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

            foreach (var textMesh in textMeshObjects)
            {
                if (textMesh.fontMaterial != materialPreset)
                {
                    textMesh.fontMaterial = materialPreset;
                    sceneModified = true;
                    Debug.Log($"Applied Material Preset to: {textMesh.gameObject.name} in scene: {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)
            {
                Debug.LogError($"Invalid prefab path: {prefabPath}");
                continue;
            }

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

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

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