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