Author Topic: Making Menus: Has to be a better way!  (Read 3567 times)

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Making Menus: Has to be a better way!
« on: March 09, 2013, 08:36:37 PM »
Hi! So my current GUI is getting scrapped, and I'm sure there's a better way to build one.

Might just skip to CLARIFICATION on the bottom to get an idea of the key problem.

the current system has a set 3 windows (menu, selection, detail) and I want to allow infinite windows, opening as needed.

Currently, the awkward system is:
there are prefab buttons for the three different windows. they each reference a guimanager, menuType (for if its menu,selection,or detail), and id (for listing the buttons purpose, or specific object id it refers to, etc).
OnClick(), it calls gui.manager.Pick(menuType, id)

for example, we click a button with menuButton,gamehelp. it sends that to guimanager, who then sends "gamehelp" to the menu case statement, which then determines gamehelp should run the connected script function, this.GetComponent<GameHelp>().PickMenu();

that then, according to the gamehelp script, populates the middle "Selection" window with buttons.

say we click "quickstart" button, it sends SelectionButton,quickstart to GUImanager, which then jumps to the selection button case statement (so any click on a button on the middle window comes here), then in that case statement it says this.GetComponent<GameHelp>().PickSelection(selectionName);

pick selection then, in yet another case statement, takes the selection (quickstart) and draws that info in the details window.

it's the same process for the detail clicks.

so yeah, this seems all sorts of loopy. Ideally, I can just create menu scripts and when i make a new window, attach the relevant script, then when any buttons in that window are clicked, it sends a notice and id's which button/thing was clicked to the target script specified, and if it cant find it it makes it.

For example, i click gamehelp button in the base window. it looks for a gamehelp script, cant find one, so it makes a new gamehelp window w/ script, and fills with buttons like quickstart and tutorialOnThing. Clicking on one of those buttons looks for helpDetail window, it doesnt exist so it makes one and populates it. If i were to click a different gamehelp button, it'd find the existing gamedetail window and repolulate it. by the same token, a button could close it's own window and send the relevant data to another (still open) window.

Is this still backwards? am I on the right track? how do you do this sort of thing?

EDIT: Clarification
Gonna try to describe what I want in the most basic terms.

a mainmenu script starts. it creates a window and procedurally adds some buttons: help, quit, review, and a list of 1-8 names. when it adds the button, it passes on the information of what method in mainmenu.cs it should call. In the case of the name buttons, it also passes on a specific indicator as to what name is referred to. For example, AddButton("Guy 1", NameDetails, "n1") with NameDetails being the method, and n1 being the data it should send back when it's pressed.

the Guy 1 button is pressed, and the event listener picks it up, running mainmenu.NameDetails(n1).

I figure AddButton(string name, ??? method) would take that data, make the button, and do the whole UIEventListener.Get(button).onClick += method;
though I'm not sure how to transfer a method to AddButton, or how to make it set the "n1" data to that specific button so when method is called, it sends the "n1" parameter.
« Last Edit: March 09, 2013, 10:41:52 PM by sintua »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Making Menus: Has to be a better way!
« Reply #1 on: March 10, 2013, 05:04:52 AM »
Create a custom script attached to your button. Add some public parameters to this script, and set them in the Inspector. In OnClick, trigger your remote function, passing the parameters you need.

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Re: Making Menus: Has to be a better way!
« Reply #2 on: March 10, 2013, 05:30:09 AM »
EDIT: While I was able to convert the Action delegates to the uiEventListener.VoidDelegate type, is that the only kind of delegate I can assign to onClick ?

I was hoping to keep the code in the menu scripts and not on the buttons by adding the method to the onclick listener, but it seems to take its own kind of delegate... which is a bit problematic as I was hoping to use other delegates loaded with parameter info as well.

  1. public class MainMenu : MenuScript
  2. {      
  3.         // Use this for initialization
  4.         void Start ()
  5.         {
  6.                 AddTitle("Main Menu");
  7.                
  8.                 AddButton("Game Help", GameHelpMenu);//implicit. should also work.
  9.                 AddButton("Review", new Action(ReviewMenu));
  10.         }
  11.         public void ReviewMenu ()
  12.         {
  13.                 //stuff
  14.         }
  15. }
  16.  
  17. public delegate void OpDelegate(Operative op);
  18. public delegate void strDelegate(string str);
  19.  
  20. public class GUIManager : MonoBehaviour
  21. {
  22.         //stuff
  23.         public void AddButton (string label, Action methodToCall, GameObject contents)
  24.         {      
  25.                 //make newButton
  26.                 GameObject newItem;
  27.                 newItem = NGUITools.AddChild(contents, menuButton);
  28.                 newItem.GetComponentInChildren<UILabel>().text = label;//write Title label
  29.                 newItem.name = ChildCount(contents) + "Button";
  30.                
  31.                 //add method to call to button event
  32.                 UIEventListener.Get(newItem).onClick += methodToCall;//ERROR: Operator `+=' cannot be applied to operands of type `UIEventListener.VoidDelegate' and `System.Action'
  33.         }
  34.  
  35.         public void AddStringButton (string label, strDelegate methodToCall, GameObject contents)
  36.         {      
  37.                 //make newButton
  38.                 GameObject newItem;
  39.                 newItem = NGUITools.AddChild(contents, menuButton);
  40.                 newItem.GetComponentInChildren<UILabel>().text = label;//write Title label
  41.                 newItem.name = ChildCount(contents) + "Button";
  42.                
  43.                 //add method to call to button event
  44.                 UIEventListener.Get(newItem).onClick += methodToCall;
  45.         }
  46. }
  47.  
« Last Edit: March 10, 2013, 04:45:01 PM by sintua »