Skip to content

Instantly share code, notes, and snippets.

@Westerveld
Created February 13, 2020 11:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Westerveld/b0f0e6addc745b74b75c0f9017558efc to your computer and use it in GitHub Desktop.
Save Westerveld/b0f0e6addc745b74b75c0f9017558efc to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using TMPro;
using System.Text.RegularExpressions;
public class ButtonNameToText : EditorWindow
{
/// When right-clicking on a Button GameObject in Scene, you can use the Button Name To Text option
/// to set the Text components text field to the name of the button GameObject.
[MenuItem("GameObject/Button Name To Text", false, 0)]
public static void Convert()
{
GameObject[] objsToChange = Selection.gameObjects;
List<GameObject> buttons = new List<GameObject>();
foreach(GameObject obj in objsToChange)
{
if(obj.GetComponent<Button>() != null)
buttons.Add(obj);
}
string tmp;
foreach(GameObject obj in buttons)
{
tmp = Regex.Replace(obj.name, "([a-z])([A-Z])", "$1 $2");
if(obj.GetComponentInChildren<Text>() != null)
{
Undo.RecordObject(obj.GetComponentInChildren<Text>(), "Changed Text");
obj.GetComponentInChildren<Text>().text = tmp;
EditorUtility.SetDirty(obj.GetComponentInChildren<Text>());
}
else if(obj.GetComponentInChildren<TMP_Text>() != null)
{
Undo.RecordObject(obj.GetComponentInChildren<TMP_Text>(), "Changed Text");
obj.GetComponentInChildren<TMP_Text>().text = tmp;
EditorUtility.SetDirty(obj.GetComponentInChildren<TMP_Text>());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment