Search Unity

Input Text

Discussion in 'Editor & General Support' started by Spoxx, Mar 21, 2019.

  1. Spoxx

    Spoxx

    Joined:
    Feb 19, 2019
    Posts:
    8
    hello does anyone know how to make a scene with 4 input fields in it,the fields should have the default text like this:
    firstbox:A
    secondbox:B
    thirdbox:C
    fourthbox:D
    this should be the default but i want the user to change it if he wants, and when the user clicks "start" the names should be saved so that i can use them later.I already wrote the code in C# for the userinput and it works perfectly but i made it with console application i don't know how to do this in Unity though.Thank you.
     
  2. SrTartarus

    SrTartarus

    Joined:
    Feb 27, 2017
    Posts:
    9
    Hello Spoxx.

    There is two children in the Input Field are called "Placeholder" and "Text", you can manipulate them.

    I created one script, so this is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. [ExecuteInEditMode]
    5. [RequireComponent(typeof(InputField))]
    6. public class InputValues : MonoBehaviour
    7. {
    8.     public string text;
    9.     public string placeHolder;
    10.  
    11.     // Update is called once you edited your scene
    12.     void Update()
    13.     {
    14.         // Placeholder
    15.         transform.GetChild(0).GetComponent<Text>().text = placeHolder;
    16.         // Text
    17.         transform.GetChild(1).GetComponent<Text>().text = text;
    18.     }
    19. }
    This code has "[ExecuteInEditMode]" that only will reproduce this code in Edit, it will not reproduce in Play Mode, so any change that you make in this component will change in the editor, in this case, I change this two children. I putted two public variables that you can use it to take the value with other scripts.

    I hope this resolved your problem.
     
    Last edited: Mar 21, 2019
  3. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501