C#/Unity3D: Bordered Text Box

This code snippet is just a public method for using your own bordered text in Unity3D.

I stick these sorts of commands into a ‘Helper.cs’ public static class.

You’ll end up with something like this:

The method is very simple and reflects the exact same method I used to achieve the effect in Dark Basic Pro.

public static void BorderTextBox(string text, float x, float y, float width, float height)
{

GUI.contentColor = Color.black;

GUI.Label(new Rect(x + 1, y, width, height), text);
GUI.Label(new Rect(x – 1, y, width, height), text);
GUI.Label(new Rect(x, y + 1, width, height), text);
GUI.Label(new Rect(x, y – 1, width, height), text);
GUI.Label(new Rect(x + 1, y + 1, width, height), text);
GUI.Label(new Rect(x – 1, y + 1, width, height), text);
GUI.Label(new Rect(x + 1, y – 1, width, height), text);
GUI.Label(new Rect(x – 1, y – 1, width, height), text);

GUI.contentColor = Color.white;
GUI.Label(new Rect(x, y, width, height), text);

}

As mine is stored in the helper.cs class, I have to refer to that first, but this is all I typed for the above screenshot.

Helper.BorderTextBox(“Attacking”, 90, 50, 100, 100);

Or

BorderTextBox( textBody, position x, position y, width, height)

I suppose if you wanted to modify it, you could add a parameter for changing the colour, as you might use this for the tutorial I posted up yesterday.

Add ‘Color Col’ to the argument in the method and then change this line:

GUI.contentColor = Color.white;

To

GUI.contentColor = col;

Then you might end up with this:

Helper.BorderTextBox(“Attacking”, 90, 50, 100, 100, Color.blue);

For a menu system you might have ‘Attack’ as Color.blue whilst selection = 1 and the rest as Color.grey and it would be effective.

 

Here is a sample of it being used with my battle menu system.

Advertisements

One comment


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s