- Home /
Unity UI (4.6.1) placeholder graphic for input field c#
I am using the following code to dynamically create a canvas with an input field in it at runtime.
The issue is that the placeholder requires a graphic class. This can be created in the unity GUI by draging the inputfield into this slot and it auto converts it from an input field into a graphic to display the placeholder text "hello world!".
However if I try to assign the inputfield to the .placeholder the compiler complains that inputfield is not of type graphic.
I looked in the documentation and I couldn't even see placeholder mentioned as part the the inputfield class, nevermind how to deal with this.
Any ideas?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CreateCanvas : MonoBehaviour {
public GameObject theCanvasGO;
void Start() {
GameObject theCanvasGO = new GameObject ();
theCanvasGO.name = "TheCanvas";
Canvas theCanvas = theCanvasGO.AddComponent<Canvas>();
GraphicRaycaster theGraphicRaycaster = theCanvasGO.AddComponent<GraphicRaycaster> ();
RectTransform theCanvasRT = theCanvasGO.GetComponent<RectTransform> ();
theCanvas.renderMode = RenderMode.WorldSpace;
theCanvasRT.sizeDelta = new Vector2 (1000f,1000f);
theCanvasRT.localPosition = new Vector3 (0f,2.69f,0f);
theCanvasRT.localRotation = new Quaternion (0f, 180f, 0f, 0f);
theCanvasRT.localScale = new Vector3 (0.001f,0.001f,1f);
// add imput field
GameObject inputFieldGO = new GameObject ();
inputFieldGO.name = "inputfield";
inputFieldGO.transform.parent = theCanvasGO.transform;
InputField inputfield = inputFieldGO.AddComponent<InputField> ();
inputfield.text = "Hello World!";
Graphic myGraphic = new Graphic ();
inputfield.placeholder = myGraphic;
inputfield.transition = Selectable.Transition.None;
inputFieldGO.AddComponent<RectTransform> ();
RectTransform inputFieldRT = inputFieldGO.GetComponent<RectTransform> ();
inputFieldRT.sizeDelta = new Vector2 (871f,300f);
inputFieldRT.localPosition = new Vector3 (13f,-70f,0f);
inputFieldRT.localRotation = new Quaternion (0f, 180f, 0f, 0f);
inputFieldRT.localScale = new Vector3 (1f,1f,1f);
GameObject textGO = new GameObject ();
textGO.name = "textob";
textGO.transform.parent = inputFieldGO.transform;
RectTransform textRT = textGO.AddComponent<RectTransform> ();
Text textscript = textGO.AddComponent<Text>();
textRT.sizeDelta = new Vector2 (871f,300f);
textRT.localPosition = new Vector3 (13f,-70f,0f);
textRT.localRotation = new Quaternion (0f, 180f, 0f, 0f);
textRT.localScale = new Vector3 (1f,1f,1f);
inputfield.textComponent = textscript;
textscript.supportRichText = false;
textscript.resizeTextForBestFit = true;
textscript.resizeTextMaxSize = 300;
textscript.resizeTextMinSize = 10;
Font myFont = Resources.Load<Font> ("MyFont");
textscript.font = myFont;
textscript.text = "This was written second.";
}
}
ps: the font "MyFont" is a font I downloaded into my resources folder which is loaded at runtime, to get this to work for you substitute MyFont with a font of your choice placed into your resources folder.
Answer by RudyTheDev · Dec 24, 2014 at 11:43 PM
When you first set inputfield.text = "Hello World!";
, this won't actually show or update the text visually. This is because internally .text
also updates the .textComponent
's Text
. But you haven't created it yet, so there's nothing to draw. inputfield
simply stores/caches the value without drawing it.
Then when you do textscript.text = "This was written second.";
, that's what the inputfield will now show visually (because InputField
itself isn't a Graphic
, it's just a fancy wrapper and it needs an actual Text
that will be drawn).
But you have created a scenario (which sounds like a Unity bug) where the input field's text and corresponding text component's text are different. So when you first click the input field, it uses the original .text
value ("Hello World!") and changes the .textComponent
's .text
to it as well (because it assumes they should be the same).
If you had had created textscript
first and assigned it as input field's .textComponent
, and only then set inputfield.text
, it would have been overwritten textscript
's value immediately and you would have never seen whatever you manually assigned to textscript.text
.
As for placeholder, it needs to be a separate game object (so there are 2 Text
s children of inputfield
). Graphic
is Unity's base class for all UI elements. This simply means .placeholder
accepts any of them. Image
is a Graphic
, Text
is a Graphic
etc. So you really need to create whatever your placeholder will be: some Text
(which is what Unity creates by default), some Image
, or whatever makes sense.
If you want it to be some text, then you should create another child under inputFieldGO
and add Text
to it with the placeholder. Then link that text/image to your .placeholder
. In very short:
GameObject placeholderGO = new GameObject();
placeholderGO.transform.SetParent(inputFieldGO.transform);
Text myPlaceholderText = placeholderGO.AddComponent<Text>();
inputfield.placeholder = myPlaceholderText;
Edit: correction. Edit 2: expand.
The problem here is that I don't want to display an image as the placeholder but rather I want the placeholder text from my inputfield to be displayed.
I tried adding inputfield.placeholder = inputfield; but this gives the cannot implicitly covert inputfield to graphic error.
$$anonymous$$y bad about the InputField
, it's the Image
component of it that is Graphic
-based and what gets referenced when you drag&drop it into placeholder
field. Actual InputField
isn't Graphic
-derived.
If I understand you right, you want a Text
:
GameObject placeholderGO = new GameObject();
placeholderGO.transform.SetParent(inputFieldGO.transform);
Text myPlaceholderText = placeholderGO.AddComponent<Text>();
inputfield.placeholder = myPlaceholderText;
I'm not sure if this is any different to what would be created design time ($$anonymous$$us all the formatting).
So (assu$$anonymous$$g I'm missing somethign) what do you mean by "placeholder text from [the] inputfield". There is 1 Text
that is the InputField
's actual input and 1 Text
that is the unchanged placeholder.
yes the text that is held in the inputfield as default text isnt shown until I click on the field, this is undesirable behavior. If I use hello world as placeholder text defined in the inputfield text string it only shows "Hello" when I click on it. The initial text read from the text component is shown initially and then disappears. If you run the code you'll see what I'm taking about hopefully.
I think I understand what you mean now. Updated the answer with clarification on .text
and .textComponent
interaction. You need separate Text
's for placeholder and the behaviour you are seeing is due to the order in which you create and assign things.
Your answer

Follow this Question
Related Questions
Hover Over Input Field Before Inputting? 2 Answers
Force UI Input Field Character limit with C Sharp 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Masked InputField on Unity UI 4 Answers