I would like my input field to stop accepting further edits after the user has pressed return or mouse clicked away from the input field.
I assume I have to add a function to the .onendedit property but I am not sure how to do this?
Example code is as follows, with the input text in the middle.
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 myInput = 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);
myInput.transition = Selectable.Transition.None;
myInput.text = "SomeText that never appears";
//myInput.onEndEdit (myEndEdit()); // this line doens't work
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);
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> ();
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());
//add image for ray casting invisable button:
Image image = invisibleButtonGO.AddComponent<Image>();
image.color = new Color(0, 0, 0, 0);
}
void handleButton (){
print ("pressed!");
}
public void myEndEdit(string thestring){
print (thestring);
// I want to call this method on submission or clicking off the input field and stop the input field from picking up more
// key presses until it is clicked again.
}
}