Search Unity

onClick button, post preset text to InputField.

Discussion in 'Getting Started' started by ARUnityAccount, Dec 12, 2015.

  1. ARUnityAccount

    ARUnityAccount

    Joined:
    Dec 10, 2015
    Posts:
    8
    First thing: I'm new. So I'm trying to build a virtual keyboard in Unity. I'm starting with one key, and one InputField.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class KeyboardScript : MonoBehaviour {
    6. InputField iField;
    7.  
    8. public void onClick() {
    9. string key = "k";
    10. iField.text(key);
    11. }
    12. }
    Here's how I interpret the code I wrote:
    using UnityEngine.UI allows me to reference UI GameObjects.
    InputField is the name of my input field and I let the computer know it is an iField.
    This code is a component of my button which is called K, and so onClick() means when button K is clicked.
    Let string called key equal letter k.
    Let the text inside InputField equal k.

    My error says "The member 'UnityEngine.UI.InputField.Text' cannot be used as method or delegate.

    I appreciate any advice. I have looked at the Unity manuals and other things, I just need a human's help because I don't know what I'm looking at.
    Thanks.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yikes, you're attempting a very difficult task for a new developer.

    When a key is pressed, what should happen is that if there is an extended selection in the field, the selected text is replaced with the new text associated with that key. And if there isn't an extended selection, then the new text should be inserted at the caret position (which should then move over by the length of the text).

    So, you would need to look at InputField's selectionAnchorPosition and selectionFocusPosition and/or caretPosition, then replace the corresponding range of its text, stuff this back into iField.text, and update the selection anchor/focus and caret. (All this could be wrapped up in a simple "selectedText" getter/setter... somebody should submit that to Unity as a feature request, maybe with working code, as it's silly to make us work this hard for it!)

    Anyway, it's quite a daunting first task... would you consider tackling something different? Working with UI stuff means understanding Unity events; indeed, for many things (like simply setting text based on a button clicked) you don't need to write any code at all. So maybe you'd enjoy working through my fun video tutorial on Unity events?
     
    ARUnityAccount likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    ...Of course another way to tackle the custom onscreen keyboard problem would be to construct a fake key Event, and pass it to KeyPressed. Then everything should happen exactly as if a real key were pressed.
     
    ARUnityAccount likes this.
  4. ARUnityAccount

    ARUnityAccount

    Joined:
    Dec 10, 2015
    Posts:
    8
    Thank you for the reply! I will look at your tutorial videos and learn more!