Author Topic: Best practices for Managers  (Read 4786 times)

johnessy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Best practices for Managers
« on: October 05, 2012, 09:12:43 AM »
Hi all, So my question is, When you go down the route of having a single menu manager that handles everything for NGUI what would be the best way to do this. Currently i am doing something like this but want to keep my code really neat and tidy.
  1. public void Start()
  2.     {
  3.         barSizes[0] = accuracyBar.foreground.localScale.x;
  4.         barSizes[1] = energyUseBar.foreground.localScale.x;
  5.         barSizes[2] = mobilityBar.foreground.localScale.x;
  6.         barSizes[3] = powerBar.foreground.localScale.x;
  7.         barSizes[4] = rangeBar.foreground.localScale.x;
  8.         barSizes[5] = speedBar.foreground.localScale.x;
  9.     }
  10.  
  11.     void Update()
  12.     {
  13.        
  14.     }
  15.  
  16.     public void Test()
  17.     {
  18.         Debug.Log("We are just testing");
  19.     }
  20.  
  21.     public void AccuracyBarIncrease()
  22.     {
  23.         Debug.Log("We have it ");
  24.         if (accuracyBar.foreground.localScale.x <= 247)
  25.         {
  26.             accuracyBar.foreground.localScale = new Vector3(barSizes[0] += barIncreaseStep, accuracyBar.foreground.localScale.y, accuracyBar.foreground.localScale.z);
  27.         }
  28.         else
  29.         {
  30.             return;
  31.         }
  32.     }

Now this will go on and on as we will have 50-70 increase buttons and decrease buttons. I know there wil be a more simpler way of doing this.

Thanks

johnessy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Best practices for Managers
« Reply #1 on: October 06, 2012, 01:41:53 PM »
Aren Mook where are you?

AndyGFX

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 24
    • View Profile
    • AndyGFX
Re: Best practices for Managers
« Reply #2 on: October 06, 2012, 03:13:00 PM »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best practices for Managers
« Reply #3 on: October 06, 2012, 07:06:43 PM »
I'm not exactly sure what the code you posted is supposed to do. What are you trying to accomplish and how is it related to a menu system? If you want to just have a bunch of buttons in a row, UIGrid or UITable would allow you to position them easily.

Pip Robbins

  • Guest
Re: Best practices for Managers
« Reply #4 on: October 07, 2012, 06:18:34 PM »
I think his question is how is the most professional way to set up a GUI Manager to handle all his GUI elements.

johnessy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Best practices for Managers
« Reply #5 on: October 13, 2012, 05:31:07 AM »
Exactly what Pip said :)

N3uRo

  • Guest
Re: Best practices for Managers
« Reply #6 on: October 14, 2012, 02:31:54 AM »
My approach is:

1. Create a class that extends from Monobehaviour and make it a Singleton.

2. Create public references for all my interface "windows", etc... So I can drag them.

3. Create wrappers from them so it's easier to use them.

I like this approach because it's really easy to use and because for the main logic I like to mix it with PlayMaker SendMessage actions (it's easier to mantain when you take a project that it's not yours or you haven't look at it for several months). Example:

Intro --> MainMenu --> OptionMenu --> Return to MainMenu --> Play... (An so on...)

With a PlayMaker FSM it's really visual and easy to add later for example, an intermediate window.

Quick example (I'm writing here so it can have compilation errors):

  1. public class InterfaceManager : MonoBehaviour {
  2.    private static InterfaceManager instance;
  3.  
  4.    public static InterfaceManager Instance
  5.    {
  6.       get
  7.       {
  8.          if (instance == null)
  9.          {
  10.             Debug.LogError("You must attach this script to a Monobehaviour. Also, don't call it from Awake");
  11.          }
  12.          return instance;
  13.       }
  14.    }
  15.  
  16.    public MyWindow window;
  17.  
  18.    void Awake() {
  19.       instance = this;
  20.    }
  21.  
  22.    public void ShowMyWindow() {
  23.       window.Show();
  24.    }
  25.  
  26.    public void HideMyWindow() {
  27.       window.Hide();
  28.    }
  29. }
  30.  

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Best practices for Managers
« Reply #7 on: March 12, 2014, 04:03:43 AM »
My approach is:

1. Create a class that extends from Monobehaviour and make it a Singleton.

2. Create public references for all my interface "windows", etc... So I can drag them.

3. Create wrappers from them so it's easier to use them.

I like this approach because it's really easy to use and because for the main logic I like to mix it with PlayMaker SendMessage actions (it's easier to mantain when you take a project that it's not yours or you haven't look at it for several months). Example:

Intro --> MainMenu --> OptionMenu --> Return to MainMenu --> Play... (An so on...)

With a PlayMaker FSM it's really visual and easy to add later for example, an intermediate window.

Quick example (I'm writing here so it can have compilation errors):

  1. public class InterfaceManager : MonoBehaviour {
  2.    private static InterfaceManager instance;
  3.  
  4.    public static InterfaceManager Instance
  5.    {
  6.       get
  7.       {
  8.          if (instance == null)
  9.          {
  10.             Debug.LogError("You must attach this script to a Monobehaviour. Also, don't call it from Awake");
  11.          }
  12.          return instance;
  13.       }
  14.    }
  15.  
  16.    public MyWindow window;
  17.  
  18.    void Awake() {
  19.       instance = this;
  20.    }
  21.  
  22.    public void ShowMyWindow() {
  23.       window.Show();
  24.    }
  25.  
  26.    public void HideMyWindow() {
  27.       window.Hide();
  28.    }
  29. }
  30.  

Hey, can you be more specific on how to use this approach? I'm having trouble understanding it. For example should I create a public variable for every window(game object) and attach a script to each window?
  1. public FirstWindow window1;
  2. public SecondWindow window2;
  3. ....
  4.  

And where should the Hide() and Show() functions be? Thank you!

N3uRo

  • Guest
Re: Best practices for Managers
« Reply #8 on: March 12, 2014, 04:46:43 PM »
Hey, can you be more specific on how to use this approach? I'm having trouble understanding it. For example should I create a public variable for every window(game object) and attach a script to each window?
  1. public FirstWindow window1;
  2. public SecondWindow window2;
  3. ....
  4.  

And where should the Hide() and Show() functions be? Thank you!

You should create a public variable for each window you want to use. The scripts depends on your needs.

Show and Hide functions are an example you can call what you want and it should be in your referenced windows.