コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】正規表現を使わずに TextMesh Pro のタグを除外した文字列を取得する例

ソースコード

using TMPro;
using UnityEngine;

namespace Kogane
{
    public static class TextMeshProTagRemover
    {
        public static string Remove( string text )
        {
            var gameObject  = new GameObject();
            var textMeshPro = gameObject.AddComponent<TextMeshPro>();
            textMeshPro.text = text;
            textMeshPro.ForceMeshUpdate();
            var parsedText = textMeshPro.GetParsedText();
            Object.Destroy( gameObject );
            return parsedText;
        }
    }
}

使用例

using Kogane;
using UnityEngine;

public class Example : MonoBehaviour
{
    private void Start()
    {
        // Red
        Debug.Log( TextMeshProTagRemover.Remove( @"<b><i><color=""red"">Red</color></i></b>" ) );
    }
}

注意

FontAsset に登録されている文字のみ対応しています