I try to mix strings and InputFields by script but I am unable to achieve it.
Does somebody know if there is a way to have a string for example My name is : then an InputField that allows the user to enter his/her name then another string and I am : then another InputField that allows the user to enter his/her nationality and so on…
The best I have for the moment is : my name is InputField(clone)(Unity.Engine.GameObject) and I am InputField(clone)(Unity.Engine.GameObject)
Is it possible to instantiate the inputfield in the string instead of having only the name of the GameObject.
Use Label(). I typically Cache as much of the data for editor scripting (and make them static readonly out of habit, even though that’s not needed due to how the editor Class would be reused).
so as an example, assuming you have a class like so:
using UnityEngine;
public class MyExampleProfile: Monobehaviour
{
public string playerName;
public string nationality;
}
Editor Script would be…
using UnityEditor;
[CustomEditor(typeof(MyExampleProfile))]
public class MyExampleProfileEditor:Editor
{
// I make these static since their data is identical between instances. and readonly cause they shouldn't change
private static readonly GUIContent c_playerNameLabel = new GUIContent("my name is ");
private static readonly GUIContent c_playerName= new GUIContent(string.Empty,"please enter your name here");
private static readonly GUIContent c_nationalityLabel = new GUIContent(" and I am ");
private static readonly GUIContent c_nationality = new GUIContent(string.Empty,"please enter your nationality here");
// cache the variable names of the fields we'll want to grab
private static readonly string k_playerName = "playerName";
private static readonly string k_nationality = "nationality";
public override void OnInspectorGUI ()
{
// if MyExampleProfile was changed by another source, then the SerializedObject reference will be outdated so
// we'll simply make sure the serialized object is always up to date before we use it
serializedObject.Update();
// lets access the fields as SerializedProperties cause they give you a lot of power out of the box
SerializedProperty p_playerName = serializedObject.FindProperty(k_playerName);
SerializedProperty p_nationality = serializedObject.FindProperty(k_nationality);
// we might change the values of these serialized properties so we tell the editor to track its state
// so that we can compare its new state when we ask again later
EditorGUI.BeginChangeCheck();
//we want all the labels and field to fit on one row so we wrap them in a Horizontal Group
EditorGUILayout.BeginHorizontal();
ShrinkLabel(c_playerNameLabel);
EditorGUILayout.PropertyField(p_playerName,c_playerName);
EditorGUILayout.EndHorizontal();
// and now for the nationality fields
EditorGUILayout.BeginHorizontal();
ShrinkLabel(c_nationalityLabel);
EditorGUILayout.PropertyField(p_nationality, c_nationality);
EditorGUILayout.EndHorizontal();
// now we ask the editor if the properties were changed in this GUI call
// if so push those changes (in the serializedProperties) back to MyExampleProfile
if(EditorGUI.EndChangeCheck())
{
// this works great cause now you can use things like ctrl+z to undo values you've entered
// its one of the things serializedObjects handles for you behind the scenes
serializedObject.ApplyModifiedProperties();
}
}
// here we specify a type of label that only takes up the amount of space it needs
private void ShrinkLabel(GUIContent content)
{
if(content==null || content == GUIContent.none)
return;
float min, max;
GUI.skin.label.CalcMinMaxWidth(content,out min,out max);
EditorGUILayout.LabelField(content,GUILayout.MinWidth(min),GUILayout.MaxWidth(max));
}
}
Woah, I think the OP was just asking about how to get the text from the inputfield.
using UnityEngine.UI;
[SerializedField]
Inputfield input1;
[SerializedField]
Inputfield input2;
// later on
string output = "User is " + input1.text + " and nationality is : " + input2.text
Thanks a lot for theses explainations. I need a bit time to understand how it works, I never used nor Editor Scripts neither serialization, but I really appreciate your detailed answers.
Actually I try to get not only the name of the input (or another component of it) I try to instantiate the whole input in a string, but I am not sure that this is possible…
Can you give a little more expanation on what this means “instantiate the whole input in a string”?
Does that mean “create a string using a few entered variables” or something else?
the part I wrote does put the strings (2 of them) together to form one sentence. You do not need the editor response for your situation (because I’m pretty sure you’re not using what that’s talking about).
The [SerializedField] attribute means that the variable is private, but can also be seen in the inspector window.
So, for the 2 variables I suggested, you would drag each one into its corresponding place in the inspector.
That way they are linked, and you can retrieve their text information from your script, if that makes sense.
My purpose is actually the following. I am working on a game that frequently submits to users a kind of form where they have to fill in blank boxes with correct answers.
To be more concrete, the user listens to an audio file and he has to complete a text with blanks: My name is (blank box where the user has to enter “Cesar”) and I am from (blank box where the user has to enter “France”)
I would like to create these forms with a loop which would make appear the form in the scene as a child of one of my gameOject (containing a canvas) when another gameObject is triggered. (this part is ok)
When a form appears on screen, I would like that the text diplays someting like string1 (in my example : "My name is ") + InputField1(empty so that the user can suggest an answer) + string2 (in my example : " and I am from ") + InputField2 etc.
This would probably be a mix of gameobjects and strings and I do not know if this is allowed.
I can concatanate string1 and string2 but I am unable to make the blank box (as an empty field) appear between the 2 strings.
The best I have had is : My name is InputField(UnityEngine.GameObject) and I am from InputField(UnityEngine.GameObject) i.e. a concatanated string where inputfields are mentionned as strings.
I hope I am more clear (I am sorry, I am a beginner). If I am, thank you for your answer.
It’s okay… I understand the problem is that you’re printing out the gameobject’s name and not the data from the inputfield.
You said to get a reference to the inputfield (similar to how I posted above.) and access its “.text” property.
Instead of using [SerializedField] (because it’s new to you… for now, just write public …
// at the top of the script where you see other "using .." statements, write:
using UnityEngine.UI;
public Inputfield input1;
public Inputfield input2;
// elsewhere, when you want to join them all together, do something like this:
string completeString = string1 + input1.text + string2 + input2.text
Attach this script to a gameobject in the scene and drag the two inputfields into the places you see in the inspector window. Maybe that will help you get started.