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:

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);