• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Bigproblem01 · Oct 04, 2014 at 08:57 AM · gridnguidelegatecenterdelegates

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.

Comment
Andres-Fernandez

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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).

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmansa · Dec 23, 2014 at 12:59 PM 0
Share

@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 :-/

avatar image Bigproblem01 · Dec 26, 2014 at 10:09 AM 0
Share

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 Manager" game object which has a "ControlPanel" script attached to it, when you declare "myGrid" variable in "ControlPanel" script, it will show up on "Game Manager" 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)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Calling a method using delegate makes my object null 0 Answers

NGUI Eventdelegate Add passing a function with parameters 1 Answer

Delegates not working on iOS 0 Answers

How to make an Input system using delegates? 0 Answers

How to unsubscribe from InputSystem event properly 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges