facebook twitter hatena line email

「Unity/TMPro/TextからTMProへ」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(使い方)
(Prefabの全Textを対応する)
 
(同じ利用者による、間の9版が非表示)
行1: 行1:
 
==使い方==
 
==使い方==
#ツールをDL https://github.com/jackisgames/TextMeshProReplacer
+
#ツールをDL&インストール https://github.com/jackisgames/TextMeshProReplacer
 
#CanvasUIEditor.csが、エラーになるので、このファイルのすべてを、コメントアウト対応
 
#CanvasUIEditor.csが、エラーになるので、このファイルのすべてを、コメントアウト対応
 
#TextReplacer.csの以下201行目辺りの行を修正し、フォント名を入れる
 
#TextReplacer.csの以下201行目辺りの行を修正し、フォント名を入れる
行6: 行6:
  
 
<pre>
 
<pre>
201 - if (fontAsset.fontInfo.Name.Equals(m_font.name, System.StringComparison.OrdinalIgnoreCase))
+
176 - if (fontAsset.fontInfo.Name.Equals(m_font.name, System.StringComparison.OrdinalIgnoreCase))
201 + (fontAsset.name.Equals("NotoSansJP-Regular SDF", System.StringComparison.OrdinalIgnoreCase))
+
176 + if (fontAsset.name.Equals("NotoSansJP-Regular SDF", System.StringComparison.OrdinalIgnoreCase))
 
</pre>
 
</pre>
  
 
参考:https://kan-kikuchi.hatenablog.com/entry/TextMeshPro_Migration
 
参考:https://kan-kikuchi.hatenablog.com/entry/TextMeshPro_Migration
 +
 +
==Sceneの全Textを対応する==
 +
TextMeshProReplacerのTextReplacer.ReplaceCurrentSceneメソッドを、以下の通り書き換える
 +
<pre>
 +
namespace TextMeshProReplacer
 +
{
 +
    internal class TextReplacer
 +
    {
 +
        [MenuItem("Text Mesh Replacer/Replace Current Scene")]
 +
        internal static void ReplaceCurrentScene()
 +
        {
 +
            GameObject[] rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
 +
            for (int i = 0; i < rootGameObjects.Length; i++)
 +
            {
 +
                GameObject root = rootGameObjects[i];
 +
                AllUnderObject(root);
 +
            } 
 +
        }
 +
        public static void AllUnderObject(GameObject obj)
 +
        {
 +
            Transform transforms = obj.GetComponentInChildren<Transform>();
 +
            if (transforms.childCount > 0)
 +
            {
 +
                foreach (Transform transform in transforms)
 +
                {
 +
                    AllUnderObject(transform.gameObject);
 +
                    Text text = transform.GetComponent<Text>();
 +
                    if (text)
 +
                    {
 +
                        Debug.Log("name=" + transform.gameObject.name);
 +
                        ReplaceUnityText(text);
 +
                    }
 +
                }
 +
            }
 +
        }
 +
</pre>
 +
==Prefabの全Textを対応する==
 +
以下をTextMeshProReplacerに追加して、Text Mesh Replacer/ReplacePrefabを実行する
 +
<pre>
 +
using System.IO;
 +
namespace TextMeshProReplacer
 +
{
 +
    internal class TextReplacer
 +
    {
 +
        [MenuItem("Text Mesh Replacer/ReplacePrefab")]
 +
        internal static void ReplacePrefabTextAll()
 +
        {
 +
            string[] files = ReadPrefabFiles();
 +
            for (int i = 0; i < files.Length; i++)
 +
            {
 +
                Debug.Log("files[i]=" + files[i]);
 +
                ReplacePrefabText(files[i]);
 +
            }
 +
        }
 +
        static string[] ReadPrefabFiles()
 +
        {
 +
            string path = Application.dataPath + "/App/Resources";
 +
            string[] files = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
 +
 +
            return files;
 +
        }
 +
        static void ReplacePrefabText(string prefabPath)
 +
        {
 +
            // string prefabPath = "Assets/App/Resources/AttPanel.prefab";
 +
            var contentsRoot = PrefabUtility.LoadPrefabContents(prefabPath);
 +
 +
            AllUnderObject(contentsRoot);
 +
 +
            PrefabUtility.SaveAsPrefabAsset(contentsRoot, prefabPath);
 +
            PrefabUtility.UnloadPrefabContents(contentsRoot);
 +
        }

2023年1月6日 (金) 21:55時点における最新版

使い方

  1. ツールをDL&インストール https://github.com/jackisgames/TextMeshProReplacer
  2. CanvasUIEditor.csが、エラーになるので、このファイルのすべてを、コメントアウト対応
  3. TextReplacer.csの以下201行目辺りの行を修正し、フォント名を入れる
  4. unityメニューのwindow/ReplaceCurrentSceneをクリックで、TextをTextMeshProUGUIに置換される
176 - if (fontAsset.fontInfo.Name.Equals(m_font.name, System.StringComparison.OrdinalIgnoreCase))
176 + if (fontAsset.name.Equals("NotoSansJP-Regular SDF", System.StringComparison.OrdinalIgnoreCase))

参考:https://kan-kikuchi.hatenablog.com/entry/TextMeshPro_Migration

Sceneの全Textを対応する

TextMeshProReplacerのTextReplacer.ReplaceCurrentSceneメソッドを、以下の通り書き換える

namespace TextMeshProReplacer
{
    internal class TextReplacer
    {
        [MenuItem("Text Mesh Replacer/Replace Current Scene")]
        internal static void ReplaceCurrentScene()
        {
            GameObject[] rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
            for (int i = 0; i < rootGameObjects.Length; i++)
            {
                GameObject root = rootGameObjects[i];
                AllUnderObject(root);
            }  
        }
        public static void AllUnderObject(GameObject obj)
        {
            Transform transforms = obj.GetComponentInChildren<Transform>();
            if (transforms.childCount > 0)
            {
                foreach (Transform transform in transforms)
                {
                    AllUnderObject(transform.gameObject);
                    Text text = transform.GetComponent<Text>();
                    if (text)
                    {
                        Debug.Log("name=" + transform.gameObject.name);
                        ReplaceUnityText(text);
                    }
                }
            }
        }

Prefabの全Textを対応する

以下をTextMeshProReplacerに追加して、Text Mesh Replacer/ReplacePrefabを実行する

using System.IO;
namespace TextMeshProReplacer
{
    internal class TextReplacer
    {
        [MenuItem("Text Mesh Replacer/ReplacePrefab")]
        internal static void ReplacePrefabTextAll()
        {
            string[] files = ReadPrefabFiles();
            for (int i = 0; i < files.Length; i++)
            {
                Debug.Log("files[i]=" + files[i]);
                ReplacePrefabText(files[i]);
            }
        }
        static string[] ReadPrefabFiles()
        {
            string path = Application.dataPath + "/App/Resources";
            string[] files = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);

            return files;
        }
        static void ReplacePrefabText(string prefabPath)
        { 
            // string prefabPath = "Assets/App/Resources/AttPanel.prefab";
            var contentsRoot = PrefabUtility.LoadPrefabContents(prefabPath);

            AllUnderObject(contentsRoot);

            PrefabUtility.SaveAsPrefabAsset(contentsRoot, prefabPath);
            PrefabUtility.UnloadPrefabContents(contentsRoot);
        }