Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unityplease · Jan 02, 2015 at 01:06 PM · c#uiinputfield

UI 4.6 input field how do I use the .onendedit?

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.
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jenci1990 · Jan 02, 2015 at 01:23 PM

Create void in your script like this:

 void StopInput(InputField _input) {
     if (_input.text.Length > 0) _input.interactable = false;
 }

and when you create the inputfield, add listener to onEndEdit:

 myInput.onEndEdit.AddListener(delegate{StopInput(myInput);});

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unityplease · Jan 02, 2015 at 01:49 PM 0
Share

This isn't the behavior I want unfortunately, this disables intractable. Where as I just want to call a method after the submission or clicking on something else, but I still want the input field to be able to be click on again.

avatar image unityplease · Jan 02, 2015 at 02:00 PM 0
Share

I'd guess I need something like:

void StopInput(InputField _input) { print (_input.text); _input.DeactivateInputField(); }

but a) this doesn't do stop it from picking up keypresses and b) it doesn't handle a submission if I click on another object rather than pressing enter.

avatar image jenci1990 · Jan 03, 2015 at 01:05 PM 0
Share

And this one?:

 void StopInput(InputField _input) {
     print(_input.text);
     myInput.DeactivateInputField();
     UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null,null);
 }
avatar image unityplease · Jan 03, 2015 at 02:43 PM 0
Share

Nope same behavior the inputfield still picks up keypresses after submit, I tried to clarify this question in these posts : http://answers.unity3d.com/questions/867836/46-inputfield-strange-behaviour-after-submit.html http://answers.unity3d.com/questions/867765/deactivate-input-field-not-set-interactable-to-fal.html

the above also causes the following error when clicking another object while entering text:

Attempting to select while already selecting an object UnityEngine.EventSystems.EventSystem:SetSelectedGameObject(GameObject, BaseEventData).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Masked InputField on Unity UI 4 Answers

Multiple Buttons To Use One Text Input Field 1 Answer

Request feedback for C# script (instantiating UI elements depending on Player Input) 0 Answers

Anyone know why my InputField is not being disabled? 0 Answers

InputField erratic behaviour 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges