Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by T27M · Sep 26, 2012 at 10:35 PM · c#guiclassshop

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 27, 2012 at 04:04 AM 1
Share

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.

avatar image Fattie · Sep 27, 2012 at 08:17 AM 0
Share

also it's better if it rhymes

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image T27M · Sep 27, 2012 at 08:42 AM 0
Share

When your calling your other scripts from your camera, do you put them on a gameobject and use GetComponent?

avatar image MarkFinn · Sep 27, 2012 at 09:43 AM 0
Share

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.

avatar image T27M · Sep 27, 2012 at 11:22 AM 0
Share

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.

avatar image MarkFinn · Sep 27, 2012 at 12:33 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Trying to change a subclasses' variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges