Author Topic: Adding a Button to a child Game Object.  (Read 4365 times)

MaskedPixel

  • Guest
Adding a Button to a child Game Object.
« on: May 22, 2012, 01:45:08 PM »
I actually have two questions.

1.  When I add a button to a "Window" GameObject that is the child of a Panel, the button does not work.  But if I add it to the Panel first, then drag it to the window, it does work.  Is this expected behavior?

2.  If you have a menu system with multiple menus, should each menu be:
    A. an individual panel?
    B. a child GameObject of a single panel?

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding a Button to a child Game Object.
« Reply #1 on: May 22, 2012, 01:51:10 PM »
1. Define "does not work"?

2. Depends on how frequently they will be changing and how many you'll have open at the same time. It's really a question of what will work better in a certain situation.

MaskedPixel

  • Guest
Re: Adding a Button to a child Game Object.
« Reply #2 on: May 22, 2012, 02:06:27 PM »
Doesn't work means it doesn't stretch or change color or offset.  I haven't actually tested if an event triggers OnClick, but I am assuming it won't because the stretch/offset/color change effects work off the events anyway.

EDIT:  I am using version 2.0.5a
« Last Edit: May 22, 2012, 02:10:23 PM by MaskedPixel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding a Button to a child Game Object.
« Reply #3 on: May 22, 2012, 02:24:41 PM »
Make sure that the button's collider is actually in front of the window's collider, or the window will intercept all your events.

MaskedPixel

  • Guest
Re: Adding a Button to a child Game Object.
« Reply #4 on: May 22, 2012, 05:16:55 PM »
Ok, I changed the Z value of both a working and non-working button in the way described.  The non-working button did not start working, and the working button did not stop working.

This isn't an issue for me btw.  Adding to the panel and then moving the button to the window is a completely acceptable work around for me.

diademlee

  • Guest
Re: Adding a Button to a child Game Object.
« Reply #5 on: December 17, 2012, 08:17:12 PM »
Bump, anyone figure this out?  I just started working with NGUI and was trying to organize my buttons, this little issue cost me the past 2 hours trying to figure out why half my buttons dont work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding a Button to a child Game Object.
« Reply #6 on: December 18, 2012, 06:56:15 AM »
When you select a button, you can see its collider outlined in green in the Scene View. When you select your UI, you can see all of the colliders. Whenever you resize a background of a button, you also must adjust the collider as the two are not in sync and don't know anything about one another. Alt+Shift+C with the collider selected fixes its bounds (also NGUI menu -> Add Collider).

If you want to debug and figure out what is intercepting your events, set UICamera's genericEventHandler property to some game object with a script on it that will do something like Debug.Log(UICamera.lastHit.collider.name) in OnClick().

diademlee

  • Guest
Re: Adding a Button to a child Game Object.
« Reply #7 on: December 18, 2012, 02:59:10 PM »
Thanks for the tip ArenMonk.

I tried your suggestion for logging out the intercepting event, and I get a null reference when clicking on the button created as a child.  The null reference is on:

UICamera.lastHit.collider

Any further suggestions for digging deeper?  The other buttons log out fine, but anything created as a child has this issue.

For reference, here is the script that I used to detect this:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NGUI_GenericEventLogger : MonoBehaviour {
  5.  
  6. // Use this for initialization
  7. void Start () {
  8.      UICamera.genericEventHandler = this.gameObject;
  9. }
  10.  
  11. void OnClick () {
  12.      if(UICamera.lastHit.collider == null){
  13.           Debug.Log("UICamera.lastHit.collider is null");
  14.      }else if(UICamera.lastHit.collider.name == null){
  15.           Debug.Log("UICamera.lastHit.collider.name is null");
  16.      }
  17.  
  18.      Debug.Log("NGUI_GenericEventLogger OnClick:" + UICamera.lastHit.collider.name);
  19. }
  20. }

diademlee

  • Guest
Re: Adding a Button to a child Game Object.
« Reply #8 on: December 18, 2012, 05:19:59 PM »
Mystery solved. 

The game object I created and subsequently re-parented to the "Panel" object was created with the default layer selected, not the layer I created for NGUI.

Widgets created with the "Panel" object selected had the correct Layer.  Widgets created with the game object had the incorrect Layer specified (Default in this case).  Changing the Layer of the game object to the NGUI layer resolved the issue, widgets created now are created with the correct Layer setting.

Moral of the story, if you create game objects to act as containers for NGUI widgets, don't forget to set their layer properly!