Author Topic: Make Panel and Widgets invisible without deactivating them  (Read 3071 times)

Joss

  • Guest
Make Panel and Widgets invisible without deactivating them
« on: June 25, 2012, 08:50:26 PM »
Is this possible? Or should I just move them somewhere off screen?

dlewis

  • Guest
Re: Make Panel and Widgets invisible without deactivating them
« Reply #1 on: June 25, 2012, 09:00:57 PM »
You could try turning the alpha all the way to 1 on all the child objects of the panel (Although if you have any objects which have a variable alpha then this won't work well at all).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Make Panel and Widgets invisible without deactivating them
« Reply #2 on: June 25, 2012, 09:25:18 PM »
Moving them off-screen would be advisable.

Joss

  • Guest
Re: Make Panel and Widgets invisible without deactivating them
« Reply #3 on: June 26, 2012, 09:09:12 AM »
Thanks for that. I will do the movement option. In the meantime (prior to reading your response). I botched together an extension method for UIPanel (based on your UIPanel code). It seems to work for what I want at the moment (I have widgets being updated while the Panel is "deactivated") but I have only tested it briefly and with the single use case. I'm not aware of if there is any reason for not using Extension methods - so if you know of any; please let me know.

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public static class UIPanelExtensions
  6. {
  7.     /// <summary>
  8.     ///  toggles a panels visibility while till allowing it to do stuff
  9.     /// </summary>
  10.     /// <remarks>
  11.     /// <b>Usage</b>
  12.     /// <code>
  13.     /// myPanel.TogglePanelState();
  14.     /// </code>
  15.     /// </remarks>
  16.     public static void TogglePanelState(this UIPanel panel)
  17.     {
  18.         if (!panel.gameObject.active)
  19.         {
  20.             panel.gameObject.active = true;
  21.             SetActiveState(panel.transform, true);
  22.         }
  23.         else
  24.         {
  25.             SetActiveState(panel.transform, false);
  26.             panel.gameObject.active = false;
  27.         }
  28.     }
  29.  
  30.     /// <summary>
  31.     /// This I swiped from UIPanel.cs
  32.     /// </summary>
  33.     static void SetActiveState(Transform t, bool state)
  34.     {
  35.         for (int i = 0; i < t.childCount; ++i)
  36.         {
  37.             Transform child = t.GetChild(i);
  38.             //if (child.GetComponent<UIPanel>() != null) continue;
  39.  
  40.             if (state)
  41.             {
  42.                 child.gameObject.active = true;
  43.                 SetActiveState(child, true);
  44.             }
  45.             else
  46.             {
  47.                 SetActiveState(child, false);
  48.                 child.gameObject.active = false;
  49.             }
  50.         }
  51.     }
  52. }
  53.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Make Panel and Widgets invisible without deactivating them
« Reply #4 on: June 26, 2012, 04:40:25 PM »
Look into NGUITools.SetActive. It already does what you just coded.

Joss

  • Guest
Re: Make Panel and Widgets invisible without deactivating them
« Reply #5 on: June 26, 2012, 04:45:25 PM »
I will - i just like extension methods - they make things nice and neat - I think I took the code from there (or the UIPanel) - so i didn't really code anything. Many thanks