3

How can I create a normal Button with text label in Unity 4.6, without using any prefab or cloning existing game objects? I will use the button for debug purposes so I don't want to clutter up the design hierarchy with this button.

Jonny
  • 15,955
  • 18
  • 111
  • 232
  • 1
    Related thread: forum.unity3d.com/threads/creating-a-gui-from-code.263563 <- from this discussion I'm thinking that even if there exists a way to create a normal button from code, it's so bloated and ugly that it's likely not worth the trouble. Sure messing with a few extra prefabs might not be 100% clean, but it looks like the lesser evil. – Jonny Feb 02 '15 at 02:31
  • I'm not sure how this question can be a duplicate as it was written back in 2014, and that other question was written 2016. – Jonny Mar 23 '17 at 03:02
  • Even if it's not an explicit duplicate, it doesn't show any research effort at all.. – arkon Oct 01 '19 at 17:17

3 Answers3

10

You can use this for UI 4.6

    public void CreateButton(Transform panel ,Vector3 position, Vector2 size, UnityEngine.Events.UnityAction method)
{
    GameObject button = new GameObject();
    button.transform.parent = panel;
    button.AddComponent<RectTransform>();
    button.AddComponent<Button>();
    button.transform.position = position;
    button.GetComponent<RectTransform>().SetSize(size);
    button.GetComponent<Button>().onClick.AddListener(method);
}
M Bak
  • 117
  • 4
0

The following applies only to the legacy "gui" system from Unity3 (before about 2012). It is now unavailable.

Add this function to any script and it will display 100 x 100 pixel sized button on the left upper corner. Then just change the Debug.Log to your debug code.

void OnGUI() // deprecated, use ordinary .UI now available in Unity
{
    if(GUI.Button(new Rect(0, 0, 100, 100), "Debug!")){
        Debug.Log("Do some debugging");
    }
}
Fattie
  • 27,874
  • 70
  • 431
  • 719
maZZZu
  • 3,585
  • 2
  • 17
  • 21
  • It works. But is there no way to use a 4.6 Button? – Jonny Nov 28 '14 at 08:37
  • 1
    Though I marked this correct, the question is still unanswered as for the UI Button. I find myself looking for the how-to almost every day. If someone can post a working example I will check that answer instead. – Jonny Dec 17 '14 at 05:51
  • The question is specifically about 4.6+. GUI-class is deprecated/legacy – Dmitry Konovalov Apr 08 '15 at 02:10
  • **YOU CAN NOT USE** the very old "gui" system from unity3 - it is now deprecated and unavailable. Use the incredibly simple UI.Button system now built-in to Unity – Fattie Feb 21 '16 at 13:35
0

you need to create a prefab of the element of UI you want to instantiate. And after you can manipublate your ui element like a gameobject and use GetComponet method for manipulate properties and methods. See the FAQ http://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

M Bak
  • 117
  • 4
  • 2
    The problem with this approach is that in this case where I don't want to clutter up the design with dev/debug elements, the dev prefab will sit there among production prefabs. I don't want to ship that stuff. That's why I asked "no prefabs please". – Jonny Dec 17 '14 at 05:43