Hello Guest

Author Topic: How to get delegate added to OnClickUIItem to work  (Read 6090 times)

ahobbit

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to get delegate added to OnClickUIItem to work
« on: December 04, 2013, 08:46:49 pm »
I've got an array of tk2dSprites with a tk2dUIItem script attached to them. The array items are created at runtime using Unity's Instantiate() command on a prefab with the tk2dSprite/tk2dUIItem setup. I've made a method, Click(tk2dUIItem clickedUIItem) to handle the OnClickUIItem() event. I also have another array that contains references to the tk2dUIItem for each tk2dSprite, for performance.

I've tried adding the Click() method to the sprite_tk2dUIItem[].OnClickUIItem list of delegates it will execute, as follows:

Code: [Select]
sprite_tk2dUIItem[I].OnClickUIItem += Click;
I've tried doing this at first in the Start() method of the script which is responsible for managing the sprites and their click events (a sprite manager script). When that didn't work, I tried instead to do this initialization in the OnEnable() event of the sprite manager script, and then removing the delegate assignments in the OnDisable() routine of the sprite manager, as is shown in all the example text:

Code: [Select]
sprite_tkUIItem[I].OnClickUIItem -= Click;
I assume that in the latest version of the 2D Toolkit, the tk2dUIManager is implicitly defined (I don't even see it listed in the list of 2D Toolkit UI scripts that can be added via the Add Component button in the Unity Editor).

Any ideas what is going wrong here?

Is it necessary to add to the OnClickUIItem delegate list only in the OnEnable() method? Can't this be done in the Start() event?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get delegate added to OnClickUIItem to work
« Reply #1 on: December 04, 2013, 10:20:46 pm »
can you be a bit more specific about what doesn't work?

You should register with OnEnable, remove in OnDisable otherwise a reference will be retained to it. If you only have one scene, then its probably fine to have it in Start and not unregister. Its not a lot more work to unregister properly in OnDisable though.

ahobbit

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to get delegate added to OnClickUIItem to work
« Reply #2 on: December 05, 2013, 01:07:59 am »
What doesn't work is that the tk2dSprites with the tk2dUIItem scripts attached to them aren't recognizing clicks, i.e. sprite_tk2dUIItem.OnClickUIItem  isn't calling my Click() method.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get delegate added to OnClickUIItem to work
« Reply #3 on: December 05, 2013, 06:11:00 pm »
Do you have a (3D) collider on that sprite?

ahobbit

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to get delegate added to OnClickUIItem to work
« Reply #4 on: December 05, 2013, 07:06:03 pm »
Ok, I think I know what went wrong.

1. I did not have a tk2dUICamera attached to the tk2dCamera I was using to draw the scene (I mistakenly thought it wasn't necessary to do that in the latest version of 2D Toolkit).

2. Although I did have a 3D box collider on the sprites in Unity 4.2, when I upgraded to Unity 4.3.1 (Unity 4.3.0 was too unstable for me to use) I tried using 2D box colliders for the sprites (which are square shaped). So thanks for pointing that out. Will 2D Toolkit allow using Unity's new 2D colliders in a future release?

Thanks so much again.  :D


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get delegate added to OnClickUIItem to work
« Reply #5 on: December 05, 2013, 07:30:37 pm »
We are leaning towards using our own colliders instead of unity 2d ones, but we'll probably support those too at some point.