コガネブログ

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

【Unity】文字を指定して TMP_FontAsset.characterTable から情報を取得する拡張メソッド

ソースコード

using System;
using System.Collections.Generic;
using System.Text;
using TMPro;

namespace Kogane
{
    public static class TMP_CharacterExtensionMethods
    {
        private static readonly Encoding ENCODING = Encoding.Unicode;

        public static TMP_Character FindByCharacter
        (
            this List<TMP_Character> self,
            string                   character
        )
        {
            var bytes   = ENCODING.GetBytes( character );
            var unicode = BitConverter.ToUInt16( bytes, 0 );

            return self.Find( x => x.unicode == unicode );
        }
    }
}

使用例

var tmpCharacter = tmpFontAsset.characterTable.FindByCharacter( "あ" );