Support => NGUI 3 Support => Topic started by: bigproblem01 on October 06, 2014, 04:34:59 AM
Title: NGUI UICenterOnChild, How to know what is centered?
Post by: bigproblem01 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.
Title: Re: NGUI UICenterOnChild, How to know what is centered?
Post by: ArenMook on October 06, 2014, 10:28:59 AM
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:
public GameObject[] childArray =new GameObject[2];
GameObject temp = UICenterOnChild.centeredObject;
switch(temp)
{
case childArray [0]:
//do something
break
case childArray [1]:
//do something
break
default:
break;
}
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
Title: Re: NGUI UICenterOnChild, How to know what is centered?
Post by: ArenMook on October 07, 2014, 09:54:55 AM
Replace that 'case' with an 'if' statement.
Title: Re: NGUI UICenterOnChild, How to know what is centered?
Post by: bigproblem01 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.