Author Topic: UI Panel Management  (Read 6968 times)

weejohn

  • Guest
UI Panel Management
« on: April 28, 2013, 01:02:53 PM »
Hello again,
I have created several Panels containing UIButtons etc which will be used for interaction / HUD / Map etc. Each panel has an animation to enter/exit the screen.
There should only ever be one panel visible at any one time. In the past whilst using OnGUI I created a sort of matrix with switches to allow only one window on screen at a time. Does NGUI have a built in UIPanel manager to basically manage this or am I going to have to create something myself?

Many thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UI Panel Management
« Reply #1 on: April 28, 2013, 03:35:04 PM »
Here's what I use in Starlink:
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. /// <summary>
  5. /// This class facilitates an easy way of switching between different windows.
  6. /// </summary>
  7.  
  8. public class UIWindow : MonoBehaviour
  9. {
  10.         static UIWindow mInst;
  11.         static List<UIPanel> mHistory = new List<UIPanel>();
  12.         static List<UIPanel> mFading = new List<UIPanel>();
  13.         static UIPanel mActive;
  14.  
  15.         /// <summary>
  16.         /// Currently visible window.
  17.         /// </summary>
  18.  
  19.         static public UIPanel current { get { return mActive; } }
  20.  
  21.         /// <summary>
  22.         /// Ensure we have an instance to work with.
  23.         /// </summary>
  24.  
  25.         static void CreateInstance ()
  26.         {
  27.                 if (mInst == null)
  28.                 {
  29.                         GameObject go = new GameObject("_UIWindow");
  30.                         mInst = go.AddComponent<UIWindow>();
  31.                         DontDestroyOnLoad(go);
  32.                 }
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Ensure that the specified window has been added to the list.
  37.         /// </summary>
  38.  
  39.         static public void Add (UIPanel window)
  40.         {
  41.                 if (mActive == window) return;
  42.  
  43.                 CreateInstance();
  44.  
  45.                 if (mActive == null)
  46.                         mActive = window;
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Show the specified window.
  51.         /// </summary>
  52.  
  53.         static public void Show (UIPanel window)
  54.         {
  55.                 if (mActive == window) return;
  56.  
  57.                 CreateInstance();
  58.  
  59.                 if (mActive != null)
  60.                 {
  61.                         mFading.Add(mActive);
  62.                         mHistory.Add(mActive);
  63.                 }
  64.  
  65.                 if (mHistory.Remove(window))
  66.                 {
  67.                         mFading.Remove(window);
  68.                 }
  69.                 else if (window != null)
  70.                 {
  71.                         window.SetAlphaRecursive(0f, false);
  72.                 }
  73.  
  74.                 mActive = window;
  75.                 if (mActive != null)
  76.                         mActive.gameObject.SetActive(true);
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Return to the previous window.
  81.         /// </summary>
  82.  
  83.         static public void GoBack ()
  84.         {
  85.                 CreateInstance();
  86.  
  87.                 if (mHistory.size > 0)
  88.                 {
  89.                         if (mActive != null)
  90.                         {
  91.                                 mFading.Add(mActive);
  92.                                 mActive = null;
  93.                         }
  94.  
  95.                         while (mActive == null)
  96.                         {
  97.                                 mActive = mHistory.Pop();
  98.  
  99.                                 if (mActive != null)
  100.                                 {
  101.                                         mActive.SetAlphaRecursive(0f, false);
  102.                                         mActive.gameObject.SetActive(true);
  103.                                         mFading.Remove(mActive);
  104.                                         break;
  105.                                 }
  106.                         }
  107.                 }
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Hide the current window and clear the history.
  112.         /// </summary>
  113.  
  114.         static public void Close ()
  115.         {
  116.                 if (mActive != null)
  117.                 {
  118.                         CreateInstance();
  119.                         mFading.Add(mActive);
  120.                         mHistory.Add(mActive);
  121.                         mActive = null;
  122.                 }
  123.                 mHistory.Clear();
  124.         }
  125.  
  126.         /// <summary>
  127.         /// Do the actual fading of panels.
  128.         /// </summary>
  129.  
  130.         void Update ()
  131.         {
  132.                 // Fade out the previous window
  133.                 for (int i = mFading.size; i > 0; )
  134.                 {
  135.                         UIPanel p = mFading[--i];
  136.  
  137.                         if (p != null)
  138.                         {
  139.                                 p.SetAlphaRecursive(Mathf.Clamp01(p.alpha - GameTime.realDelta * 6f), false);
  140.                                 p.transform.localScale = Vector3.Lerp(Vector3.one * 0.9f, Vector3.one, p.alpha);
  141.                                 if (p.alpha > 0f) continue;
  142.                         }
  143.                         mFading.RemoveAt(i);
  144.                         p.gameObject.SetActive(false);
  145.                 }
  146.  
  147.                 // Only fade in the new window after the previous has faded out
  148.                 if (mFading.size == 0 && mActive != null && mActive.alpha < 1f)
  149.                 {
  150.                         mActive.SetAlphaRecursive(Mathf.Clamp01(mActive.alpha + GameTime.realDelta * 6f), false);
  151.                         Transform t = mActive.transform;
  152.                         t.localScale = Vector3.Lerp(Vector3.one * 0.9f, Vector3.one, mActive.alpha);
  153.  
  154.                         // 3D layer
  155.                         if (mActive.gameObject.layer == 10)
  156.                                 t.localRotation = Quaternion.Euler(0f, -18f + mActive.alpha * 24f, 0f);
  157.                 }
  158.         }
  159. }

weejohn

  • Guest
Re: UI Panel Management
« Reply #2 on: April 28, 2013, 03:49:57 PM »
You are a star....once again top answer!!  ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UI Panel Management
« Reply #3 on: April 28, 2013, 04:21:34 PM »
You can replace GameTime.realDelta with Time.deltaTime to get it working quickly on your end. It's another handy script I wrote that tracks realTime's delta every frame.

zentuit

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UI Panel Management
« Reply #4 on: June 08, 2013, 03:07:55 PM »
Never mind, apparently I had updated but not imported the latest version

I can't seem to use this UIWindow class.

  • UIPanel doesn't have a definition for SetAlphaRecursive
  • UIPanel doesn't have a definition for alpha

Also it seems like its using the Generic List class but getting missing method errors; it looks like BetterList class is used, correct?

is there an extension class for UIPanel?
« Last Edit: June 08, 2013, 03:19:17 PM by zentuit »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UI Panel Management
« Reply #5 on: June 08, 2013, 05:47:35 PM »
This script, along with other related ones, is a part of the UI Starter Kit.

mimminito

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: UI Panel Management
« Reply #6 on: November 25, 2013, 12:46:52 PM »
Am I right in assuming that this does not work with the latest NGUI? SetAlpaRecursive has now gone... so I am using the alpha property instead but this is resulting in blacked out Textures during transitions...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UI Panel Management
« Reply #7 on: November 25, 2013, 06:37:25 PM »
Alpha property is now cumulative, so it should work. When you say blacked out, do you mean they fade to black, or...?

mimminito

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: UI Panel Management
« Reply #8 on: November 26, 2013, 04:38:43 AM »
Here is what happens on my Nexus 10 (it is fine in the editor, sorry forgot to mention that):

I have a background panel, which is always active. On the game starting, I then show the Main Menu panel. If I then click on a button, to transition to another panel, the background panel is the turned to black. Every time I perform a transition from now on the background panel flickers in, then back to black (in say 0.2 seconds). This happens for every transition from now on, and the background never returns unless the scene is reloaded.

mimminito

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: UI Panel Management
« Reply #9 on: November 26, 2013, 04:43:45 AM »
Ok, I have just updated NGUI to 3.0.6f2, and its now all working again! I noticed in another thread you mentioned something about Alpha issues, and I saw there was an update (I only updated to f1 yesterday) and it fixed it. Do you know what was changed?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UI Panel Management
« Reply #10 on: November 26, 2013, 05:34:44 AM »
Probably related to the other flickering issue I fixed.