In Megabite #11, I discussed how to do the basic layout features for both GUI and GUILayout. In this release, what I'll be discussing is a bit more in-depth (You will want to already have a basic understanding of the previous article.)

The first thing I want to talk about is GUILayout.Window, which is a very cool feature, especially if you are planning on having a draggable elements such as an inventory system or perhaps a status window. The command is simple enough, but the setup for a GUI window is a bit more complex than to just add your information into OnGUI. First, you will want to declare a Rect variable and give it a name:

var myWindow : Rect;

function Awake() { myWindow = Rect(0,0,300,100); //This will default our window to the upper-left corner, and set the minimum size as 300 wide and 200 tall.}

At this point, we have our OnGUI function gather information from 2 places - our variable and also a function we will now create. The "0" in the commad above symbolized an ID number for the window, which is also found in this new function. Because all of our regular GUI stuff will be actually located within this fuction, you may find this method to be a much cleaner way of laying out your regular GUI elements to begin with.

Running your game with all of these pieces of code within a .js file should give you a draggable window. The initial placement of the window can be manipulated in the section where you declare your Rect variable, and the window should resize based on the content as long as you are using GUILayout as opposed to simply GUI. Of course, both of these are options an much of how you use this type of setup will be largely based on your own choices.

Next, I wanted to discuss the addition of textures into your GUI. This could be useful for a lot of reasons, and you can use a standard picture or sprite-type image, or if you use Unity Pro you can actually add a render texture which is quite nice - basically the results of a camera can be sent into a texture format. If you want a "video" element within your GUI (like a spinning 3d item, etc.), that's the way to go.

Because access to Unity Basic is more universal, I'll cover that end of things. First, you'll want to declare a texture variable:

var myPicture : Texture;

When you do that, you'll end up with a drag/droppable section in the editor inspector. You can import any picture you like into Unity and simply assign the image to the inspector. Now you can use the GUI.Box or GUILayout.Box elements to display your texture as opposed to showing text. if using the same code from above, just do it like this:

Now you've got a fancy picture within your draggable window. If you managed a render texture, you've actually got a video feed, which is also awesome. :)

Now, we're going to talk about dynamic generation. With the clean, functional layout we have here, it's easy to use a FOR loop to actually make your window display information from an Array in a very dynamic way. In this example, we're going to replace our function after declaring an array. For now, we'll fill that array with a few strings for testing purposes.

What will happen here is that our draggable window should have 5 boxes - one for each element in our array. You may want to eventually add spacing and such depending on what you're going for, but this should give you an idea of what is possible.

Overall, there are a lot of options within the GUI command subset. GUI elements are difficult to master, but they are not only a handy thing to learn but also an important part of any game for navigation, inventory, statistics, status readouts, and thousands of other uses.

The above concludes the actual "lesson" portion, but as an added bonus, I'll paste a few pieces of GUI code functions below for some extra examples: (as of today, this is code included in "Stellar", and can be seen in-action here.)