Hello Guest

Author Topic: Button events  (Read 5664 times)

vambier

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 35
    • View Profile
Button events
« on: October 04, 2012, 10:34:48 pm »
I'm fairly new to unity and I'm trying to get the events for the tk2dbutton working but I can't seem to get the hang of it.

Could you post a really simple example in c# code(maybe also in java for others)?

It's been a long time since I programmed in c# and I thought I knew how the event system worked(I work with c++ normally) but apparently I don't:-) Been looking for examples but I can't get it to work.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Button events
« Reply #1 on: October 05, 2012, 11:54:20 am »
Do you mean the button delegate events?

They are really simple to wire up. Basic sample here:
Code: [Select]
public class ButtonTestScript : MonoBehaviour {

tk2dButton button;

// Use this for initialization
void Start () {
button = GetComponent<tk2dButton>();
button.ButtonPressedEvent += ButtonPressed;
button.ButtonAutoFireEvent += ButtonAutoFire;
button.ButtonDownEvent += ButtonDown;
button.ButtonUpEvent += ButtonUp;
}

void ButtonPressed(tk2dButton source)
{
Debug.Log("Pressed");
}

void ButtonAutoFire(tk2dButton source)
{
Debug.Log("Autofire");
}

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

void ButtonUp(tk2dButton source)
{
Debug.Log("Up");
}

// Update is called once per frame
void Update () {

}
}

Its pretty much the same in JS

Code: [Select]
#pragma strict

var button : tk2dButton;
button = GetComponent(tk2dButton);

function Start () {
button.ButtonPressedEvent += ButtonPressed;
}

function ButtonPressed()
{
Debug.Log("Hello");
}

vambier

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Button events
« Reply #2 on: October 05, 2012, 04:16:19 pm »
Thanks for the quick reply again!

Then I'm afraid I remembered correctly... This is how I wrote it but my event don't come trough.
Are there no settings in the inspector that can mess things up?

vambier

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Button events
« Reply #3 on: October 05, 2012, 06:14:29 pm »
I really don't understand why this doesn't work, the mouse clicks are detected when I use Input.GetMouseButtonDown(0)

I added a tk2dButton to my sprite and also my script called LButton with the following code :
Code: [Select]
using UnityEngine;
using System.Collections;

public class LButton : MonoBehaviour {

tk2dButton LeftButton;

void Start ()
{
LeftButton = GetComponent<tk2dButton>();
LeftButton.ButtonUpEvent += Down;
}

void Down( tk2dButton source )
{
print("Down");
}

void Update () {
}
}

So as far as I can see I didn't do anything strange here...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Button events
« Reply #4 on: October 05, 2012, 07:38:38 pm »
A few things which usually cause it to fail:

1. Make sure there is a collider on your button
2. Make sure the collider doesn't intersect with the camera. This is really important. Look from the side view to make sure the front of the collider is contained by the camera. Keeping it really thin might be a good option here.
3. Drop a breakpoint in the tk2dButton script, see if the update function passes... the whole button working depends on this.

vambier

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Button events
« Reply #5 on: October 05, 2012, 08:01:14 pm »
Yep, the collider was intersecting with the camera :-\ Thanks for the help!!