Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: KyleB92 on June 08, 2013, 03:42:41 AM

Title: How do I use a game object to show/hide panels?
Post by: KyleB92 on June 08, 2013, 03:42:41 AM
I've been going off of this thread to try and figure it out so far:

http://www.tasharen.com/forum/index.php?topic=458.0

I've created what I understand is a "singleton" script that I want to attach to a game object in my scene so that I can turn panels on/off with SetActive, but at the moment I can't successfully attach it to an object because it doesn't derive from MonoBehaviour.

Here is my singleton script (i've been trying to stick with javascript since I've found it tricky to make c# and js talk to each other):

  1. #pragma strict
  2.  
  3. class UIManagerClass
  4. {
  5.  private static var Instance : UIManagerClass = null;
  6.  var panelState : boolean;
  7.  
  8.  public static function GetInstance() : UIManagerClass
  9.  {
  10.  if(Instance == null)
  11.  
  12.  Instance = new UIManagerClass();
  13.  
  14.  return Instance;
  15.  }
  16.  
  17.  private function UIManagerClass ()
  18.  {
  19.  Debug.Log(panelState);
  20.  if (!panelState)
  21.  Debug.Log("panel off");
  22.  }
  23. }

The private function UIManagerClass() is where I believe I'd add the SetActive(true/false) code, I think?

And this is my OnClick function called from a button:

  1. function OnClick()
  2.  
  3. {
  4.         var uiControl : UIManagerClass = UIManagerClass.GetInstance();
  5.         uiControl.panelState = false;
  6.         uiControl.GetInstance();
  7. }

This doesn't show any errors at the moment, but from my understanding I won't be able to reference the panel objects until I can attach this script to a game object in the scene, and I'm not sure how to do that.

I've tried adding "extends MonoBehaviour" and it lets me attach the script to an object, but tells me I can't use "new" and can only use AddComponent(). However, I'm not sure how to go about this (whenever I try something I seem to get varying errors, so I'm sure I'm not using the right code protocol or something).

Any assistance anyone can offer is very much appreciated, and I apologise if my code and/or methods are awfully ugly; I've been trying to teach myself by scouring the net and frankensteining pieces of code together.
Title: Re: How do I use a game object to show/hide panels?
Post by: ArenMook on June 08, 2013, 08:45:15 AM
Pseudocode, since that's what Javascript reads like to me:

if (no instance)
  create a new game object
  instance = addcomponent of your instance type (to this game object)

P.S. If you're looking for a class that would make it easy for you to switch between windows, the UI Starter Kit (http://www.tasharen.com/forum/index.php?topic=4506) has one.
 
Title: Re: How do I use a game object to show/hide panels?
Post by: KyleB92 on June 12, 2013, 01:50:22 AM
Ok, so I've now got:

  1. #pragma strict
  2.  
  3. class UIManagerClass extends MonoBehaviour
  4. {
  5.  private static var Instance : UIManagerClass = null;
  6.  var panelState : boolean;
  7.  
  8.  
  9.  public static function GetInstance() : UIManagerClass
  10.  {
  11.  if(Instance == null)
  12.        
  13.         var UIManager : GameObject;
  14.         UIManager = new GameObject("uimanager");
  15.         Instance = UIManager.AddComponent(UIManagerClass);
  16.  
  17.  return Instance;
  18.  }
  19.  
  20.  private function UIManagerClass ()
  21.  {
  22.  Debug.Log(panelState);
  23.  if (!panelState)
  24.  Debug.Log("panel off");
  25.  }
  26. }

This seems to work. I can successfully call the GetInstance function by clicking a button. When this happens, a game object called uimanager is created with the script attached. However, I'm still unsure as to how I would now reference the panel game objects and toggle them between active/inactive from this script?

Thanks very much for your help, btw.
Title: Re: How do I use a game object to show/hide panels?
Post by: Nicki on June 12, 2013, 02:53:35 AM
You start off by using C#.

Then you either load individual gameobjects from resources and keep references to them in your manager script. From there you can call setactive on them all you want.

You can't just magically know or find the panels here and there - you should explicitly make a reference.
Btw.:  Remember the brackets:
  1. if(Instance == null)
  2.         {
  3.         var UIManager : GameObject;
  4.         UIManager = new GameObject("uimanager");
  5.         Instance = UIManager.AddComponent(UIManagerClass);
  6.         }
  7.  

Otherwise it's only the first line that's covered by the if.
Title: Re: How do I use a game object to show/hide panels?
Post by: KyleB92 on June 13, 2013, 02:54:13 AM
I see. I think I have made some progress loading these objects from resources, however I have hit another road block.

I have created a panel that appears when a button is pressed (onclick), using this bit of script:

  1. uiControl.tutePrompt = Resources.Load("UIAssets/GameObjects/tutePanel");
  2.         NGUITools.AddChild(GameObject.Find("Anchor1"), uiControl.tutePrompt);

I'm trying to then make this panel disappear when I press a button, and I'm using this bit of script for it:

  1. var uiControl : UIManagerClass = UIManagerClass.GetInstance();
  2.         uiControl.tutePrompt.SetActive(false);

This disables the object, but only outside of the game in the project view. The little active checkbox in the inspector becomes unchecked, but in the actual game it doesn't disappear. It remains disabled even after I stop playing, and so then is disabled from the start when I hit play again.

I have also tried the reverse, having it inactive from the start and trying to activate it, but I get the same result; active state only changes in the inspector, not dynamically.

Hoping it's something simple that's gone over my head from staring at code all day. Thanks again for the help.
Title: Re: How do I use a game object to show/hide panels?
Post by: KyleB92 on June 14, 2013, 08:27:47 PM
Ah, I may have figured it out. Before, I was using SetActive() on the prefab instance, however, the instance I was creating was a (Clone) of that instance. So, after I created the object, but before I called SetActive, I set my object to:

  1. uiControl.tutePrompt = GameObject.Find("tutePanel(Clone)");
  2. uiControl.tutePrompt.SetActive(false);

Preliminary testing suggests that it's working, but I'll post if I run into any issues. Also, I'm still totally new to all this coding business, so if anyone can see that I'm going about things in an entirely messy or wrong way, please let me know.