Hello Guest

Author Topic: Need help thinking about GUI  (Read 4212 times)

huminaboz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Need help thinking about GUI
« on: July 06, 2014, 02:26:40 am »
I could use a little help thinking about the GUI.

First, are there any recommended best practice tips hiding behind a link somewhere?

Next, more specific questions:
1. Hypothetical situation: you're in a game screen and you hit the pause button. A menu appears with "keep playing" and "exit game" buttons. How should I create this menu? Right now I have it in an off-screen location and make it appear in view when the pause button is pressed. Is there a smarter way to go about this? I've tried enable/disable but every time I try that it seems to elude me because a disabled object doesn't seem to respond to enabling.

2. Buttons. These guys are driving me crazy. So far as I can tell, I have to create button prefabs, drag them into the scene, and control them via a script on a single object (like an empty 'GUI Manager' game object). Here's the script I use on the GUIManager:

Code: [Select]
public tk2dUIItem PauseBtn;

void OnEnable()
{
if (PauseBtn) {PauseBtn.OnClick += PauseMenu;}
}

I feel like this method requires a lot of duplication of work across several scenes.

This issue probably relates to Unity itself as much as TK2D in that I would like to know how to programatically create these buttons without dragging them to the scene. (I usually end up instantiating and object and not being able to control the clone).
 
Alternatively, I've tried putting the button control script on the buttons themselves, but this seems to make it harder to control functions across other scripts, which is what led me to the GUI Manager solution. What would you recommend?

3. What is void OnDisable() used for?

Thanks for insight on any of these questions!

Disclaimer: I have a little under 2 years programming experience, am self-taught, and this is the first time I've tried asking a question - please pardon my computer science ignorance :/

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Need help thinking about GUI
« Reply #1 on: July 06, 2014, 04:30:45 pm »
1. SetActive(true/false) on the root object should work fine. The buttons, etc should be children of this object.

2. Whatever works for you. If you don't like the event method, you can also use the "Send Message" method. Select the button in the scene, drag the object with the function you want triggered into the Send Message Target slot, and then choose the function to call from the dropdown that appears in OnClick.
To create them programatically, just instantiate, and bind the events whichever way you like - either using events to the button itself, or using the send message target method above.
Hint: Unity returns the object you have a reference to. Eg. if your prefab is referenced like so -
public tk2dUIItem buttonPrefab;
Instantiating will return the new uiItem.
tk2dUIItem newButton = Instantiate(buttonPrefab) as tk2dUIITem;
at which point you can hook up whatever you need there.

3. OnDisable is a Unity function. You don't call it, it gets called when your object is disabled. http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDisable.html

huminaboz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Need help thinking about GUI
« Reply #2 on: July 07, 2014, 09:30:02 am »
Amazing, you've solved two of my major reoccurring problems from the past few months, I should have asked sooner. Such a quick response too, thanks sir!

But (of course) now there's a new problem:
What if I want to control the button as a child of a parent GameObject? Say I instantiate the entire GUI interface at once with several buttons as child objects. Is there a way to control those individual buttons? Or do I need to instantiate each button separately?

I did thisGameGUIHolder = Instantiate(GameGUIHolder, Vector3.zero, Quaternion.identity) as GameObject;
Instead of thisPauseBtn = Instantiate(PauseBtn, Vector3.zero, Quaternion.identity) as tk2dUIItem;

I fumbled around for a couple of hours trying to find some way to make thisPauseBtn = GameGUIHolder/PauseBtn to no success.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Need help thinking about GUI
« Reply #3 on: July 07, 2014, 10:22:58 pm »
You can control it any way you like. What I normally do is create a controller for the gui interface which has direct links to the buttons it needs to control, etc. You'd do this at the correct levels of granularity - i.e. if there was a dynamically constructed list underneath that, you'd use group those together with a behaviour.

Sometimes you don't need that level of control, and in those cases you can use transform.Find("...") to find the correct components programatically. tk2dUIDemo5Controller, CustomizeListObject for an example of this.

huminaboz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Need help thinking about GUI
« Reply #4 on: July 08, 2014, 08:30:51 am »
Oh my... I have a long way to go.

Thanks for your help!