コガネブログ

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

【Unity】TextMesh Pro の Glyph の BY をスクリプトから編集する例

概要

using System;
using System.Text;
using TMPro;
using UnityEditor;

internal static class Example
{
    [MenuItem( "hoge/hoge" )]
    private static void Hoge()
    {
        const string path = "hoge.asset";
        const string text = "あ";

        var bytes     = Encoding.Unicode.GetBytes( text );
        var unicode   = BitConverter.ToUInt16( bytes, 0 );
        var fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>( path );
        var character = fontAsset.characterTable.Find( x => x.unicode == unicode );
        var glyph     = character.glyph;
        var metrics   = glyph.metrics;

        metrics.horizontalBearingY = 32;

        glyph.metrics = metrics;

        EditorUtility.SetDirty( fontAsset );
        AssetDatabase.SaveAssetIfDirty( fontAsset );
    }
}