- Home /
How to create a 4.6 UI button NOT from prefab NOT in unity editor only from C#
I've read a lot of questions and answers and watched endless youtube videos but I can't find the answer to this anywhere.
Frequently people suggest instatiating a prefab button or worse ignore the question conditions and just say "drag and drop the button and function".
I really would like to create an entire 4.6 UI button ONLY using C#. complete with mouse over and on click functions.
I have found that the following doesn't work:
MyButton.onClick.AddListener(() => { MyFunction();})
I create the following example c# code that shows I cannot reach the handleButton method and it does not print "pressed" when I click the invisible button.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CreateCanvasButton : MonoBehaviour {
void Start () {
// create event system
GameObject eventsystem = new GameObject ();
eventsystem.AddComponent<EventSystem> ();
eventsystem.AddComponent<StandaloneInputModule> ();
eventsystem.AddComponent<TouchInputModule> ();
string name = "Canvas";
GameObject newCanvasGO = new GameObject ();
newCanvasGO.name = name;
newCanvasGO.transform.SetParent (this.transform);
newCanvasGO.AddComponent <Canvas>();
newCanvasGO.AddComponent<CanvasScaler> ();
newCanvasGO.AddComponent<GraphicRaycaster> ();
RectTransform theCanvasRectTransform = newCanvasGO.GetComponent<RectTransform> ();
theCanvasRectTransform.anchoredPosition3D = new Vector3 (0f, 0f, 0f);
theCanvasRectTransform.sizeDelta = new Vector2 (2400f,3800f);
theCanvasRectTransform.anchorMin = new Vector2 (0f,0f);
theCanvasRectTransform.anchorMax = new Vector2 (1f,1f);
theCanvasRectTransform.localScale = new Vector3 (0.0005f,0.0005f,1f);
theCanvasRectTransform.localPosition = new Vector3 (0f, 2.9f, 0f);
Canvas theCanvas = newCanvasGO.GetComponent<Canvas> ();
theCanvas.worldCamera = Camera.main;
newCanvasGO.SetActive (true);
newCanvasGO.AddComponent<Image> ();
Image canvasImage = newCanvasGO.GetComponent<Image> ();
Sprite mySprite = Resources.Load<Sprite> ("Background");
canvasImage.sprite = mySprite;
canvasImage.type = Image.Type.Sliced;
// create name title text
GameObject titleTextGO = new GameObject ();
titleTextGO.name = "Name_text";
titleTextGO.transform.parent = newCanvasGO.transform;
Text titleText = titleTextGO.AddComponent<Text> ();
RectTransform titleTextRT = titleTextGO.GetComponent<RectTransform> ();
titleTextRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
titleTextRT.sizeDelta = new Vector2 (877,199);
titleTextRT.localPosition = new Vector3 (-22f,1722f,0f);
titleTextRT.localScale = new Vector3 (1f, 1f, 1f);
titleText.text = "Title";
Font theFont = Resources.GetBuiltinResource<Font> ("Arial.ttf");
titleText.font = theFont;
titleText.fontSize = 140;
titleText.color = Color.black;
titleText.alignment = TextAnchor.MiddleCenter;
titleText.resizeTextForBestFit = true;
titleText.resizeTextMinSize = 10;
titleText.resizeTextMaxSize = 160;
// create nickname title text
GameObject TextGO = new GameObject ();
TextGO.name = "InputField";
TextGO.transform.parent = newCanvasGO.transform;
TextGO.AddComponent<RectTransform> ();
RectTransform TextRT = TextGO.GetComponent<RectTransform> ();
TextGO.AddComponent<InputField> ();
InputField Input = TextGO.GetComponent<InputField> ();
TextRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
TextRT.sizeDelta = new Vector2 (1669,3370);
TextRT.localPosition = new Vector3 (-35f,-188f,0f);
TextRT.localScale = new Vector3 (1f, 1f, 1f);
Input.transition = Selectable.Transition.None;
Input.text = "SomeText that never appears";
GameObject textForInputGO = new GameObject ();
textForInputGO.name = "Text_ForInputField";
textForInputGO.transform.SetParent(TextGO.transform);
RectTransform textRT = textForInputGO.AddComponent<RectTransform> ();
Text textscript = textForInputGO.AddComponent<Text>();
textRT.sizeDelta = new Vector2 (1356f,300f);
textRT.localPosition = new Vector3 (0f,-0f,0f);
textRT.localRotation = new Quaternion (0f, 0f, 0f, 0f);
textRT.localScale = new Vector3 (1f,1f,1f);
Input.textComponent = textscript;
textscript.supportRichText = false;
textscript.resizeTextForBestFit = true;
textscript.resizeTextMaxSize = 300;
textscript.resizeTextMinSize = 10;
textscript.font = theFont;
textscript.text = "This is the main body text.";
textscript.color = Color.black;
textscript.alignment = TextAnchor.MiddleCenter;
// create button
GameObject invisibleButtonGO = new GameObject ();
invisibleButtonGO.name = "invisibleButton";
invisibleButtonGO.transform.parent = newCanvasGO.transform;
invisibleButtonGO.AddComponent<RectTransform> ();
RectTransform invisibleButtonRT = invisibleButtonGO.GetComponent<RectTransform> ();
invisibleButtonRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
invisibleButtonRT.sizeDelta = new Vector2 (358,354);
invisibleButtonRT.localPosition = new Vector3 (1021f,-1723f,0f);
invisibleButtonRT.localScale = new Vector3 (1f, 1f, 1f);
invisibleButtonGO.AddComponent<Button> ();
Button invisibleButton = invisibleButtonGO.GetComponent<Button> ();
invisibleButton.transition = Selectable.Transition.None;
invisibleButton.onClick.AddListener (() => handleButton());
}
void handleButton (){
print ("pressed!");
}
}
Please help I've been trying this for days and have only found unanswered questions such as this one:
http://answers.unity3d.com/questions/855412/adding-a-function-to-a-ui-46-button-called-on-anot.html
well they are created using the new keyword so I assume they are still in memory. The game objects appear in the scene hierachy and the input field seems to work correctly, but the button does not.
@unityplease, i missed this line:
newCanvasGO.transform.SetParent (this.transform);
And thought there wasn't a reference applied to a GameObject in the scene, my bad.
Answer by jenci1990 · Jan 02, 2015 at 11:37 AM
Add image component to your button:
GameObject invisibleButtonGO = new GameObject();
invisibleButtonGO.name = "invisibleButton";
invisibleButtonGO.transform.parent = transform;
invisibleButtonGO.AddComponent<RectTransform>();
RectTransform invisibleButtonRT = invisibleButtonGO.GetComponent<RectTransform>();
invisibleButtonRT.anchoredPosition3D = new Vector3(0f, 0f, 0f);
invisibleButtonRT.sizeDelta = new Vector2(358, 354);
invisibleButtonRT.localPosition = new Vector3(1021f, -1723f, 0f);
invisibleButtonRT.localScale = new Vector3(1f, 1f, 1f);
invisibleButtonGO.AddComponent<Button>();
Button invisibleButton = invisibleButtonGO.GetComponent<Button>();
invisibleButton.transition = Selectable.Transition.None;
invisibleButton.onClick.AddListener(() => handleButton());
//change:
Image image = invisibleButtonGO.AddComponent<Image>();
image.color = new Color(0, 0, 0, 0);
Thanks this works, is this requirement for the event to be registered requiring an image component documented somewhere because it wasn't obvious?
Another question about my code if you can help me, how to I stop the input field from picking up key presses after I press return?
The canvas object has a component called 'GraphicsRaycaster' so for it to register, you must have a graphic component attached 'GraphicRaycaster : A BaseRaycaster to raycast against Graphic elements.'
It's not that well documented, but it makes sense that invisible buttons don't function (you can get round this by adding a transparent texture to the image component I believe)
Your answer
Follow this Question
Related Questions
UI 4.6 menu "Start" button script c# 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity UI 4.6 Canvas Enable/Disable make Accessible 2 Answers
Pause Menu? 3 Answers