UiImGui の作法
StreamingAssets からロードする
コールバックでカスタマイズする
using System;
using System.Runtime.InteropServices;
using ImGuiNET;
class FontConfigWrap : IDisposable
{
ImFontConfig[] _config;
public delegate void EnterFunc(ref ImFontConfig config);
public void Enter(EnterFunc callback)
{
callback(ref _config[0]);
}
GCHandle _pinnedArray;
public IntPtr Ptr => _pinnedArray.AddrOfPinnedObject();
public FontConfigWrap()
{
// from im_draw.cpp: ImFontConfig::ImFontConfig
_config = new ImFontConfig[]
{
new ImFontConfig{
FontDataOwnedByAtlas = 1,
OversampleH = 3, // FIXME: 2 may be a better default?
OversampleV = 1,
GlyphMaxAdvanceX = float.MaxValue,
RasterizerMultiply = 1.0f,
EllipsisChar = ushort.MaxValue,
}
};
// 要素数1のArrayを利用して struct の IntPtr を得る
_pinnedArray = GCHandle.Alloc(_config, GCHandleType.Pinned);
}
public void Dispose()
{
if (_pinnedArray.IsAllocated)
{
_pinnedArray.Free();
}
}
}
システムの MSGothic.ttc を追加する例
TODO: FontAwesome を追加する
using UnityEngine;
using ImGuiNET;
public class MSGothic : MonoBehaviour
{
// Start is called before the first frame update
public void LoadMSGothic()
{
Debug.Log("LoadMSGothic");
var io = ImGui.GetIO();
#if false
io.Fonts.AddFontDefault();
io.Fonts.Build();
#else
io.Fonts.AddFontDefault();
using (var config = new FontConfigWrap())
{
config.Enter((ref ImFontConfig config) =>
{
config.MergeMode = 1;
});
io.Fonts.AddFontFromFileTTF("C:/Windows/Fonts/MSGothic.ttc", 24, config.Ptr, io.Fonts.GetGlyphRangesJapanese());
io.Fonts.Build();
}
#endif
}
private void OnEnable()
{
UImGui.UImGuiUtility.Layout += OnLayout;
}
private void OnDisable()
{
UImGui.UImGuiUtility.Layout -= OnLayout;
}
private void OnLayout(UImGui.UImGui uImGui)
{
if (ImGui.Begin("日本語"))
{
ImGui.TextUnformatted("テキスト");
}
ImGui.End();
}
}