Skip to content

Instantly share code, notes, and snippets.

@jesusnoseq
Created January 13, 2018 17:25
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 jesusnoseq/e26d7bc04ce74e1397b4d917d5b4bbc9 to your computer and use it in GitHub Desktop.
Save jesusnoseq/e26d7bc04ce74e1397b4d917d5b4bbc9 to your computer and use it in GitHub Desktop.
Creates a context menu item to create a button with a text mesh pro text instead unity's text
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class TMPButton {
[MenuItem("GameObject/UI/TextMeshPro - Button", false, 0)]
static void CreateTMPButton()
{
GameObject obj = new GameObject("TMPButton");
RectTransform parentRect = obj.AddComponent<RectTransform>();
obj.AddComponent<Image>();
obj.AddComponent<Button>();
obj.transform.SetParent(Selection.activeTransform, false);
GameObject child = new GameObject("ButtonText");
RectTransform rect= child.AddComponent<RectTransform>();
TextMeshProUGUI text = child.AddComponent<TextMeshProUGUI>();
text.SetText("Button");
text.color = Color.black;
text.fontSize = 30;
text.alignment = TextAlignmentOptions.Center;
child.transform.SetParent(obj.transform,false);
rect.anchorMin = new Vector2(0, 0);
rect.anchorMax = new Vector2(1, 1);
rect.offsetMax = new Vector2(0, 0);
rect.offsetMin = new Vector2(0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment