Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: sk1989 on July 24, 2012, 12:08:30 PM

Title: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 12:08:30 PM
Below is the snippet of my code. I want to set "buyButton" to active when "button" is selected and set the "button" to false at the same time . Do this vice versa. But what i get is either both the buttons disappear off the screen or both the buttons showing on top of each other. Can you help me with this and how this can be achieved in NGUI.

If not, is there a possibility where I can change the depth value of the buttons so that I can change which button is on top of the other button.




if(IsSelected)
      {
          NGUITools.SetActive(buyButton.gameObject,true);
          Position = buyButton.transform.position;
          buyButton.transform.position = Position;
       
          NGUITools.SetActive(button.gameObject,false);
         
      }
      else if(IsSelected == false)
      {
         NGUITools.SetActive(button.gameObject,true);//this part does not work, it does not go back to true.why?
         NGUITools.SetActive(buyButton.gameObject,false);
         
      }


thanks in advance for anyone with the help
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 01:32:03 PM
I am attaching a pic for better understandment
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: PhilipC on July 24, 2012, 01:38:35 PM
NGUITools.SetActive is acctually recursive so any children will also get disabled (Unity's setactiverecursivly doesnt enable and disable in the correct order). My guess is that your buy button is a child of the object you are trying to disable.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: JRoch on July 24, 2012, 01:41:32 PM
Either that or he's disabling the child that has the button image and the collider, but not the other pieces of the button.

sk1989:  give us a screencap of these buttons in your heierarchy view, with all the children expanded.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 01:49:57 PM
here is the screen shot.
That's what i thought too, so i put those buttons in an empty game object.

Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: JRoch on July 24, 2012, 02:46:43 PM
Are all of those "Child (clone)" items buttons?  If so, why aren't they parented to some NGUI panel that's parented to an anchor that's parented to a UICamera?  (Meaning, they certainly aren't going to behave right in your UI if some of your items are in NGUI's UICamera 2D space, and some are in your world main camera space.)
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 02:48:45 PM
i don't know why they appear that way because, the way i have it setup is, im instantiate each item from a prefab with another script that is attached to the grid.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: JRoch on July 24, 2012, 02:51:14 PM
You have to grab the top-most gameobject of your prefab and then set its transform.parent to your grid's gameobject.transform.  If the script is attached to the grid it will be something like:

  1. Transform newButtonItem = ((GameObject)Instantiate((GameObject)Resources.Load("Prefabs/Whatever/MyPrefab"))).transform;
  2. newButtonItem.parent = this.transform;
  3. newButtonItem.name = "InventoryButton_" + newButtonItem.GetInstanceID.ToString(); // make the name unique, just for laughs
  4.  
  5. // now call your code that re-calcs the grid layout...
  6.  
  7.  
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: ArenMook on July 24, 2012, 05:16:33 PM
Don't use Instantiate. Use NGUITools.AddChild.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 05:32:32 PM
I have the instantiate stuff on a different script attached to the grid, where it instantiates the buttons.

the prefab that I have for to instantiate is basically has both the button and buyButton in it as child. Also i added another script to each buttons to detect
the onSelect() and broadcast the message to the script that is on the parent of these buttons . But now if i select any of the buttons in the list, they all get selected.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: ArenMook on July 24, 2012, 05:37:43 PM
Component.Broadcast sends the message recursively, including all child objects -- just like NGUITools.SetActive. You should use SendMessage if you want it to go only to the one object you're targeting. Consult Unity's documentation for more information.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 06:02:11 PM
I did the send message instead of broadcast message. so now it does select the specific button. but now it does not set the button back to active . That works perfectly fine with the BuyButton. Here is the snippet of my code.

private void Display(bool isSelected)
   {
      _isSelected=isSelected;
      if(IsSelected)
      {   
         Debug.Log("here");
         Vector3 newpos = _panel.transform.worldToLocalMatrix.MultiplyPoint3x4(button.transform.position);
          SpringPanel.Begin(_panel.gameObject,-newpos,50.0f);
         
         NGUITools.SetActive(buyButton.gameObject,true);
         buyButton.transform.position = Position;
         NGUITools.SetActive(button.gameObject,false);
      
         _lbl.color = Color.red;

         
      }      
      else if (IsSelected == false)
      {   
         NGUITools.SetActive(buyButton.gameObject,false);
         NGUITools.SetActive(button.gameObject,true);// this part does not work for some reason.
         _lbl.color = Color.yellow;

         
            
      }
      
   }
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 07:17:58 PM
I know what the problem is, once set the button to false(not active) then after that it does not detect OnSelect function anymore. so how do I go around this.

Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: ArenMook on July 24, 2012, 07:20:27 PM
You can't. Disabled game objects can't receive events.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 24, 2012, 09:38:14 PM
Sorry but, How else can I do this behavior for my situation. Does NGUI have anything that fix my problem? I was thinking of moving the button off screen when its selected instead of deactivating it. But it wont let me do that either because the position is being override by the grid position from the script that is attached to the grid.

IF anyone have a solution for this problem, thanks in advance.
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: Rafe on July 25, 2012, 12:15:12 AM
You can't. Disabled game objects can't receive events.

You can call methods on disabled components/objects though (I think). Delegates might work, or calling your own method directly from another script. I am pretty sure it works because at the end of the day these are just classes instanced in memory. Their state in Unity is a sort of meta behaviour. Worth a try anyway
Title: Re: NGUI.TOOls set active not working as intended ?????
Post by: sk1989 on July 26, 2012, 03:14:55 PM
no actually they don't work, but I got around it by just having to move the positions of the off the screen when selected instead of disabling it, I thought that was the best solution for this problem and it works for me.