- Home /
GUI Layout. Can I end and begin a horizontal group automatically?
Hey! I'm writing the script for my inventory.
What I'm trying to do is something like this:
function OnGUI()
{
GUILayout.BeginArea (Rect (0,0,400,));
GUILayout.BeginHorizontal();
if (hatchet > 0){
GUILayout.Button ("Hatchet");
}
if (coins > 0){
GUILayout.Button ("Coins");
}
if (logs > 0){
GUILayout.Button ("Logs");
}
if (kindling > 0){
GUILayout.Button ("Kindling");
}
if (fishingPole > 0){
GUILayout.Button ("Fishing Pole");
}
if (rawSalmon > 0){
GUILayout.Button ("Raw Salmon");
}
if (rawTrout > 0){
GUILayout.Button ("Raw Trout");
}
if (fishingBait > 0){
GUILayout.Button ("Fishing Bait");
}
if (cookedTrout > 0){
GUILayout.Button ("Cooked Trout");
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
This isn't my actual code. The buttons are more complicated and have functionality. But this is the basics of my inventory script. What I want is to have a 5 x 8 inventory where the buttons all align to the top and to the left. I don't want them overlapping. I think this could be achieved by writing script that automatically places:
GUILayout.EndHorizontal();
GUILayout.StartHorizontal();
after every 5 items that exist in the inventory (greater than 0). How could I do this?
Thanks very much.
Answer by Eric5h5 · Aug 07, 2010 at 05:23 AM
I think it would be a lot easier with arrays:
import UnityEngine.GUILayout;
var inventoryItems : int[]; var inventoryNames : String[];
function OnGUI() { var count = 0; BeginArea (Rect (0,0,400,700)); BeginVertical(); for (v = 0; v < 8; v++) { BeginHorizontal(); for (h = 0; h < 5; h++) { while (count < inventoryItems.Length && inventoryItems[count] == 0) count++; if (count < inventoryItems.Length && Button (inventoryNames[count])) { // do whatever } count++; } EndHorizontal(); } EndVertical(); EndArea(); }
I haven't used arrays yet. I suppose now would be a good time to learn.
But before I start, will this make it possible for the buttons to align as I want? I looked at the script you wrote and it looks like your for/while is what takes care of that. Thanks
@$$anonymous$$ythStrott, as far as I know, yes...you could always test it and see. :)
Your answer
Follow this Question
Related Questions
Button in GUI BeginScrollView problem 0 Answers
proper inventory system issue.. 1 Answer
Unity android- GUILayout button not showing text? 1 Answer