That doesn’t appear to work please look at this code:
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.AddComponent ();
eventsystem.AddComponent ();
string name = “Canvas”;
GameObject newCanvasGO = new GameObject ();
newCanvasGO.name = name;
newCanvasGO.transform.SetParent (this.transform);
newCanvasGO.AddComponent ();
newCanvasGO.AddComponent ();
newCanvasGO.AddComponent ();
RectTransform theCanvasRectTransform = newCanvasGO.GetComponent ();
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 ();
theCanvas.worldCamera = Camera.main;
newCanvasGO.SetActive (true);
newCanvasGO.AddComponent ();
Image canvasImage = newCanvasGO.GetComponent ();
Sprite mySprite = Resources.Load (“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 ();
RectTransform titleTextRT = titleTextGO.GetComponent ();
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 (“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 TextRT = TextGO.GetComponent ();
TextGO.AddComponent ();
InputField myInput = TextGO.GetComponent ();
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);
myInput.transition = Selectable.Transition.None;
myInput.text = “SomeText that never appears”;
myInput.onEndEdit.AddListener(delegate{StopInput(myInput);});
GameObject textForInputGO = new GameObject ();
textForInputGO.name = “Text_ForInputField”;
textForInputGO.transform.SetParent(TextGO.transform);
RectTransform textRT = textForInputGO.AddComponent ();
Text textscript = textForInputGO.AddComponent();
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);
myInput.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 invisibleButtonRT = invisibleButtonGO.GetComponent ();
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 invisibleButton = invisibleButtonGO.GetComponent ();
invisibleButton.transition = Selectable.Transition.None;
invisibleButton.onClick.AddListener (() => handleButton());
//add image for ray casting invisable button:
Image image = invisibleButtonGO.AddComponent();
image.color = new Color(0, 0, 0, 0);
}
void handleButton (){
print (“pressed!”);
}
void StopInput(InputField _input) {
print (_input.text);
EventSystem.current.SetSelectedGameObject(null); // neither does this
}
}
- If I select another object rather than pressing return I get “Attempting to select while already selectring an object” error.
AND
- It requires me to press return twice before it stops accepting input.