Skip to content

Instantly share code, notes, and snippets.

@TakaakiIchijo
Created January 9, 2023 05:10
Show Gist options
  • Save TakaakiIchijo/eb8421746d4672f796ed5ca835c3c10f to your computer and use it in GitHub Desktop.
Save TakaakiIchijo/eb8421746d4672f796ed5ca835c3c10f to your computer and use it in GitHub Desktop.
Exclude TextMeshPro default font asset when build
#if UNITY_EDITOR
using System.Collections.Generic;
using System.Reflection;
using TMPro;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class ExcludeTMPDefaultFontAssetInBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public int callbackOrder => 0;
private FieldInfo defaultFontAssetFieldInfo =
typeof(TMP_Settings).GetField("m_defaultFontAsset", BindingFlags.Instance | BindingFlags.NonPublic);
private TMP_FontAsset defaultFontAsset;
private TMP_FontAsset defaultFallbackFontAsset;
public void OnPreprocessBuild(BuildReport report)
{
ExcludeDefaultFontAsset();
}
private void ExcludeDefaultFontAsset()
{
var settings = TMP_Settings.instance;
if (settings == null)
return;
defaultFontAsset = (TMP_FontAsset)defaultFontAssetFieldInfo.GetValue(settings);
if (defaultFontAsset == null)
return;
//fallbackフォントをはがす
//入れ子fallbackまではがさなくても大丈夫
defaultFallbackFontAsset = defaultFontAsset.fallbackFontAssetTable[0];
defaultFontAsset.fallbackFontAssetTable = null;
EditorUtility.SetDirty(settings);
}
public void OnPostprocessBuild(BuildReport report)
{
BackToEditorDefaultFontAsset();
}
private void BackToEditorDefaultFontAsset()
{
var settings = TMP_Settings.instance;
if (settings == null || defaultFontAsset == null)
return;
//fallbackフォントをはがす
defaultFontAsset.fallbackFontAssetTable = new List<TMP_FontAsset>() { defaultFallbackFontAsset };
defaultFontAsset = null;
defaultFallbackFontAsset = null;
EditorUtility.SetDirty(settings);
AssetDatabase.SaveAssets();
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment