Author Topic: NGUI UICenterOnChild, How to know what is centered?  (Read 6531 times)

bigproblem01

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
NGUI UICenterOnChild, How to know what is centered?
« on: October 06, 2014, 04:34:59 AM »
Hi guys,

I'm having trouble figuring out how to find out programmatically on which item does my NGUI grid center. I have a menu where items can be dragged horizontally and they get centered using UICenterOnChild. What I want is to find out from the script which item the menu is currently centered on.

I looked at the documentation of UICenterOnChild and there is a "delegate void UICenterOnChild.OnCenterCallback(GameObject centeredObject)". There's also a function "UICenterOnChild.onCenter" which triggers when a new object is being centered.

Probably the reason I don't quite understand the documentation is because I don't know how to use delegates yet. I thought I knew what they were and after looking at the unity's tutorial on delegates didn't find anything new but apparently I don't understand them enough to figure out how to achieve what I want in this case.

If anyone has any idea how to get that info I'll be happy to hear it.

Thanks in advance for you time.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI UICenterOnChild, How to know what is centered?
« Reply #1 on: October 06, 2014, 10:28:59 AM »
GetComponent<UICenterOnChild>().onCenter = YourCallback;

You really should just google "c# delegates".

bigproblem01

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: NGUI UICenterOnChild, How to know what is centered?
« Reply #2 on: October 06, 2014, 10:56:29 AM »
GetComponent<UICenterOnChild>().onCenter = YourCallback;

You really should just google "c# delegates".

I'll take another, deeper look at delegates but I asked here because I thought if I got the answer I'd understand the missing piece.
Doesn't onCenter trigger when the scripts starts centering on a new child object in the grid?
I also want to find out which specific child object it is.

EDIT:
Ok, I found the centeredObject but now NGUI changes the name of my objects to 0,1,2,3 and when you go in the other direction -1,-2,-3 etc. It would be ok if this was consistent, but the name changing happens at random, should it be like that?

On another note, when I try to compare centeredObject with my gameobjects it works if I use an IF statement but doesn't work when I use switch, like so:

Let's say I create an array of gameobjects to plug in these child objects so I can refer to them from the script. Then I'd like to write the Switch function like:
  1. public GameObject[] childArray = new GameObject[2];
  2. GameObject temp = UICenterOnChild.centeredObject;
  3.  
  4. switch(temp)
  5. {
  6. case childArray [0]:
  7. //do something
  8. break
  9. case childArray [1]:
  10. //do something
  11. break
  12. default:
  13. break;
  14. }
  15.  

Unity throws an error though saying "A switch expression of type 'Unity GameObject' cannot be converted to an integral type, bool, char, string, enum or nullable type"

I know this latter problem is not really NGUI related but does anyone happen to have an idea why switch statement can't process the gameObject?

Thanks
« Last Edit: October 06, 2014, 12:21:03 PM by bigproblem01 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI UICenterOnChild, How to know what is centered?
« Reply #3 on: October 07, 2014, 09:54:55 AM »
Replace that 'case' with an 'if' statement.

bigproblem01

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: NGUI UICenterOnChild, How to know what is centered?
« Reply #4 on: October 07, 2014, 11:27:45 AM »
yeah I know, with IF statement it works, just for some reason SWITCH refuses to compare game objects and I can't just replace "case"'s with "if"s inside the switch so I just stuck with if/else if.

Anyway, I originally had my code in update using centeredObject but since it was not optimal I made it work with onCenter delegate function.

Thank you for your support.
« Last Edit: October 07, 2014, 12:08:32 PM by bigproblem01 »