what can I use to write a text inside a box?
Hi,
I am trying to write a text inside a box.I have tried it using square of sprites but square and text are not merging they are creating a huge distance between them.what else can I use? So, that I can fit text inside a box which might give the look of labels.Please help me out.
Answer by Dibbie · Nov 17, 2016 at 06:42 AM
You can use the UI system for "labels".
Either a Panel or a Image, you can then parent that to a Text UI element.
When you do that (putting the Text game object, as a child to your Panel or Image gameObject, all while inside the Canvas parent), you will be restricted to sizing the ratio of the text's bounds, to the parents bounds -- so in this case the Panel or Image.
There is a handy tool on the top left of the RectTransform you can use to make sure the scale/ratio is right and automatically adjusts to the parent. Click that small box and several others will appear -- the bottom right box, holding down SHIFT first and click it, then ALT and clicking it again, will center it to the parent, then size it to fix the parent perfectly.
After this, on your Text game object, you can check "Best Fit" if youd like to make sure you can continue typing and have the size of the text automatically size down or up depending on how much text overlaps, or underlaps the parent. Theres a lot of weird things with working around this, youll have to tweak for what you want though.
And without any scripting what-so-ever, you just created a "label". (iv included some visuals to help illustrate my "steps").
For ease of your life later, if you plan to have these "labels" pop up at run time, you can create a prefab of them, though if they are only ever going to be on that canvas, you can just duplicate that one label you already made. The even nicer thing about the Canvas system, is that you can mount it any camera in your scene, or even freely in Unity space, so you can align individual Canvas's to objects in your scene rather then being "restricted" to only being about to align them to cameras.
Answer by SyedaAfreen · Nov 17, 2016 at 10:59 AM
Hi @Dibbie,
By following your steps I got panel and text.But when I am running my application the panel is somewhere and text is at some other position.How to fix them together and when I want to attach text within box whose position should I give text position or panel position? Please help me
Steps 3 and 4 kind of go over that -- make sure its all parented properly, then your text component will take the parents (panel in your case) Transform, so 0 0 0, becomes the center of the parent, rather then the center of the world.
When parenting the Text, if the scale changes to very large or small numbers from default 1 1 1, thats fine, leave them - its again, inheriting the parents scale accordingly.
After you parent the text, if it is off, just simply manually fix both, and they shouldnt change at run time.
By code, you shouldnt ever be moving the text at all, if you want to move the label around, by code or manually, you should ONLY move the parent (panel), because the text is linked to the parent, it will follow accordingly, same thing for rotating and scaling.
Hope that helps.
Your answer
Follow this Question
Related Questions
Click on different objects to move the camera 2 Answers
jumpy dinamic font 0 Answers
OnGUI() problems in unity 5.6, worked with Unity 4. 0 Answers
Label doesn't affect GUI 0 Answers
Label and line renderer not showing 0 Answers