Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: johnessy on October 05, 2012, 09:12:43 AM

Title: Best practices for Managers
Post by: johnessy 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
Title: Re: Best practices for Managers
Post by: johnessy on October 06, 2012, 01:41:53 PM
Aren Mook where are you?
Title: Re: Best practices for Managers
Post by: AndyGFX on October 06, 2012, 03:13:00 PM
Here: http://www.tasharen.com/forum/index.php?topic=1895.0
Title: Re: Best practices for Managers
Post by: ArenMook 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.
Title: Re: Best practices for Managers
Post by: Pip Robbins 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.
Title: Re: Best practices for Managers
Post by: johnessy on October 13, 2012, 05:31:07 AM
Exactly what Pip said :)
Title: Re: Best practices for Managers
Post by: N3uRo 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.  
Title: Re: Best practices for Managers
Post by: hexaust 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!
Title: Re: Best practices for Managers
Post by: N3uRo 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.