Hello Guest

Author Topic: Basic Button  (Read 5220 times)

guidez

  • Newbie
  • *
  • Posts: 7
    • View Profile
Basic Button
« on: June 27, 2013, 08:31:00 pm »
For someone that is still pretty newbish with not only Unity, but with C# as well, I found it very hard to understand the concept of incorporating a Basic Button and having it do something OTHER then play an audioclip. The documentation on this was very lacking. After finally "getting" it today, I think the biggest factor to the confusion was the example of listening to events in "Understanding UI System and Building your Own Components" (http://www.unikronsoftware.com/2dtoolkit/docs/2.00/ui/system.html)

Code: [Select]
void OnEnable()
{
    uiItem.OnDown += ButtonDown;
    uiItem.OnClickUIItem += Clicked;
}

void ButtonDown()
{
    Debug.Log("Button Down");
}

void Clicked(tk2dUIItem clickedUIItem)
{
    Debug.Log("Clicked:" + clickedUIItem);
}

//Also remember if you are adding event listeners to events you need to also remove them:
void OnDisable()
{
    uiItem.OnDown -= ButtonDown;
    uiItem.OnClickUIItem -= Clicked;
}

If you copy/paste the code as-is, you get the following error: error CS0103: The name `uiItem' does not exist in the current context. The example is not clear that you need to have the class extend (if that's the right terminology) tk2dUIBaseItemControl, like:

Code: [Select]
public class MyCScript: tk2dUIBaseItemControl {


    public GameObject lureObject;

    void OnEnable()
    {
uiItem.OnClick += specialMove;
    }

    void OnDisable()
    {
uiItem.OnClick -= specialMove;
    }

    private void specialMove()
    {
        Debug.Log ("Yep");
    }

}

This might seem simple and easy to debug, but for someone just starting out it was extremely frustrating. Everything else so far with 2D toolkit has been great so far though and I feel it's one of the best investments I've made so far.
« Last Edit: June 27, 2013, 08:35:31 pm by guidez »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Basic Button
« Reply #1 on: June 27, 2013, 10:41:17 pm »
The ui system uses c# events extensively, and assumes you understand how they work. If you don't understand that, then any book/tutorial on c# will  help, as it'll introduce you to those concepts, etc.

What exactly are you trying to do? I think you might be going about this the wrong way. You won't need to inherit from tk2dUIBaseItemControl in most cases. If you just want to subscribe to an event that triggers when the button is clicked, look in the tk2dUIDemoController for an example of how to hook into a button event to "do something".