Skip to content

Instantly share code, notes, and snippets.

@WestHillApps
Created November 28, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WestHillApps/7ccc9fb6384cfba430f970bac2e2bbab to your computer and use it in GitHub Desktop.
Save WestHillApps/7ccc9fb6384cfba430f970bac2e2bbab to your computer and use it in GitHub Desktop.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using TMPro;
public static class CreateCustomTmproTextMenu
{
// TextMeshProのデフォルトフォントから差し替えたいResources外のフォントアセットパス
private const string FONT_PATH_TMPRO = "Assets/MyProject/NonResources/UseFont.asset";
[MenuItem("GameObject/UI/Custom TextMeshPro - Text", false, 0)]
private static void CreateCustomTmproText()
{
Transform parent = Selection.activeGameObject.transform;
GameObject go = new GameObject("TextMeshProText");
go.transform.SetParent(parent, false);
TextMeshProUGUI tmproText = go.AddComponent<TextMeshProUGUI>();
tmproText.fontSize = 24;
tmproText.color = Color.white;
tmproText.text = "New Text";
tmproText.raycastTarget = false;
tmproText.enableWordWrapping = true;
tmproText.overflowMode = TextOverflowModes.Truncate;
TMP_FontAsset font = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(FONT_PATH_TMPRO);
if (font != null)
{
tmproText.font = font;
tmproText.UpdateFontAsset();
tmproText.ForceMeshUpdate();
}
Selection.activeGameObject = go;
}
[MenuItem("GameObject/UI/Custom TextMeshPro - Text", true)]
private static bool CreateCustomTmproTextEnable()
{
return Selection.activeGameObject && Selection.activeGameObject.GetComponentInParent<Canvas>() != null;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment