- Home /
When your GUI is gluey, what to do-ey?
Hello
I've made a GUI scrip to display a variety of information in the game on the HUD, I have added some buttons that lead to some menus. The menus are going to be arranged in a grid with an image/texture button representing a game object that is purchasable in game. This will be my shop design.
I am going to generate the item buttons at run time depending on how many items are available in the shop. The problem I'm having is coming up with a way I can store the references to these items.
I was looking into creating an item class that has all the basic properties, then I could just create a new instance with the name,cost,texture and the game object itself for each item. Then I was thinking I could loop through each item and display it as a button which when clicked would instantiate the game object.
Is it possible to break up parts of the GUI into smaller scripts and then call them all in one scrip that displays everything? or Would this be best to do in one script and keep everything together? My GUI scrip is getting a bit long and messy.
How would I go about creating the base class for the item or is there a better workaround ?
Thanks
Please, find a better question title. "What is the best approach" is one of the worst title you could choose. It won't help anyone. This is a knowledge base. The question title should represent your actual question.
Answer by MarkFinn · Sep 27, 2012 at 03:41 AM
Personally I put "WindowDisplay()" methods in the classes that they most relate to and then use a single OnGUI() in a camera mounted script to call those that are active at that time.
On larger projects I have such "gui element classes" register themselves with the gui-controlling script at startup (or when they are generated) and build a table of enabled/disabled values the OnGUI can scan through.
When your calling your other scripts from your camera, do you put them on a gameobject and use GetComponent?
I used to, but these days I actually keep a table of the gui element monobehaviors, rather than the gameobjects, in the gui controller. It worked out to be a lot more efficient than repetitive GetComponent calls.
I've been considering rewriting it into a simpler more structured system that'll use delegates more intelligently.
How do you go about setting up your table?
I was trying setup an event when the button(s) were pressed but it never worked as I intended. I always had trouble accessing other scripts.
Inside the gui controller class (Generally a monobehavior attached to the main camera) I have a static method of the form
public static int RegisterGUIElement(GUI.WindowFunction guiElement, bool activeAtStart){}
which returns a count of the number of registered elements in the system, after adding the new element and its status to a pair of (private static) lists, guiElements and guiElementStates. (Using lists, rather than tables to nicely enforce unique keys, keys being position in the list and also the value of the returned integer).
RegisterGUIElement is called from a number of locations including the startup logic of the game and the Start() of multiple $$anonymous$$onobehaviours. These get back an int key they can use in calls to other public static methods in the gui controller, mainly
void EnableGUIElement(int)
and
void DisableGUIElement(int)
which work much as you would expect.
The OnGUI in the GUI controller then simply has to get the list of positions in the guiElementStates which have a [true] value and call the corresponding window methods from the guiElements list.
$$anonymous$$y current version adds a lot more complexity, including storing positions for movable windows, and using my own window methods rather than the Unity GUI windows, but that is the gist of it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081530im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make a custom class inherited from Unity's GUI? 0 Answers
c# - void method from external class. 2 Answers
Distribute terrain in zones 3 Answers