GUI label and wordwrap

I’m making speech boxes in my game for NPCs but having a few issues.

I’m a unity rookie so dunno if this is the best way of doing it, but I create two GUI labels, one for the actual speech and one for the shadow underneath it (just black). I want the position of the speech box in the center of the NPC, which is why guiPosition is halved.

Here is my code:

//get size of text so we know shadow box size
Vector2 size = SpeechBox.CalcSize (new GUIContent (content));

//some code to set position of box above center of NPC
var guiPosition = Camera.main.WorldToScreenPoint (new Vector3 (transform.position.x, transform.position.y + 0.15f, 0));
guiPosition.y = Screen.height - guiPosition.y;
guiPosition.x = guiPosition.x - (size.x / 2f);
guiPosition.y = guiPosition.y - size.y;
         
//draw boxes
GUI.Label (new Rect (guiPosition.x + 3f, guiPosition.y + 3f, size.x, size.y), "", black);
GUI.Label (new Rect (guiPosition.x, guiPosition.y, size.x/2, size.y), content, SpeechBox);

The issue I’m having is when I try and get word wrap added to it. I don’t want the text to go over say… 300 pixels, and if it does it needs to move to the next line. I was thinking I could just add a min(size, 300) kind of thing to the code, but then the calcsize to get the shadow size wouldn’t work would it?

Just a little confused, am I in the right direction?

sorry for not really answering your question… but why do you use the legacy UI system of Unity?
It is not only hard to tame but also very imperformant.

I would highly recommend to use the new UI system instead. With the new UI system you can achieve such an effect (text shadows) with 3 clicks and without coding - and you have a live preview.

Someone in the chat pointed that out, and I re-coded it with new UI and sorted it, thanks