- Home /
NGUI UICenterOnChild, find out what is centered
Hi guys,
I'm having trouble figuring out how to find out programatically 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.
Answer by Bigproblem01 · Oct 08, 2014 at 01:15 PM
I resolved the issue myself. For others having the same question, here's how to do this:
Create an UICenterOnChild variable (let's say its name is myCenteringGrid), Plug in your desired UICenterOnChild in there, Then make myCenteringGrid.onCenter equal to your desired function which you want to fire when the UICenterOnChild starts centering on a new object in the grid. onCenter is an instance of a OnCenterCallback delegate. Your desired function must have the same return value and input parameters as the original delegate (that is OnCenterCallback). In this case the return type is VOID and the parameter is a GameObject (which is a gameobject being centered).
@Bigproblem01 - I am having the exact same problem, and I am not sure how you have set this up? Can you please give an example :-/
create a variable:
public UICenterOnChild myGrid;
this will give you an empty slot in the unity's editor on the gameobject which has the script attached to it in which you are declaring this variable.
So if you have a "Game $$anonymous$$anager" game object which has a "ControlPanel" script attached to it, when you declare "myGrid" variable in "ControlPanel" script, it will show up on "Game $$anonymous$$anager" game object.
Under your NGUI's "UI Root", create a new widget and attach a "Grid" and "UICenterOnChild" components to it. Now plug in this new game object we created (with Widget, Grid and UICenterOnChild components on it) in your empty myGrid slot.
Let's say your grid has 3 items in it. Create an array of GameObjects:
public GameObject[] gridItems = new GameObject[3];
then plug in the grid items into corresponding slots just like you plugged into myGrid slot.
Now create a custom function which will do whatever you want once the desired item in your grid is selected. Let's call this function "knowWhatIsCentered" and write something like:
public void knowWhatIsCentered(GameObject _centeredItem)
{
if(_centeredItem == gridItems[0])
{
print("item 1 selected");
}
else if(_centeredItem == gridItems[1])
{
print("item 2 selected");
}
else
{
print("item 3 selected");
}
}
now in your Start() function, write :
void Start()
{
myGrid.onCenter = knowWhatIsCentered;
}
if you mouseover the "onCenter" in your monoDevelop it will show you the description of the delegate. The Summary reads "Callback triggered whenever the script begins centering on a new child object". What you need is to know what parameters this delegate takes to pass on the correct parameter yourself to your "knowWhatIsCentered" function (which you just equated to the onCenter delegate). In this case, onCenter takes in a GameObject and you have to pass in the one which is centered by your NGUI grid which we did above.
Now knowWhatIsCentered will be called everytime you swipe the grid and you can also call it manually by passing in the desired gridItem.
Hope this helps 8)