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 · Dec 24, 2014 at 11:29 PM · c#uiinputfieldgraphic

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.

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
Best Answer

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 Texts 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.

Comment
Add comment · Show 5 · 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 · Dec 25, 2014 at 12:07 AM 0
Share

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.

avatar image unityplease · Dec 25, 2014 at 12:17 AM 0
Share

I tried adding inputfield.placeholder = inputfield; but this gives the cannot implicitly covert inputfield to graphic error.

avatar image RudyTheDev · Dec 25, 2014 at 02:56 AM 0
Share

$$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.

avatar image unityplease · Dec 25, 2014 at 03:01 AM 0
Share

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.

avatar image RudyTheDev · Dec 25, 2014 at 01:33 PM 0
Share

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

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

3 People are following this question.

avatar image avatar image avatar image

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


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