2D Toolkit Forum
2D Toolkit => Support => Topic started by: vambier 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.
-
Do you mean the button delegate events?
They are really simple to wire up. Basic sample here:
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
#pragma strict
var button : tk2dButton;
button = GetComponent(tk2dButton);
function Start () {
button.ButtonPressedEvent += ButtonPressed;
}
function ButtonPressed()
{
Debug.Log("Hello");
}
-
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?
-
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 :
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...
-
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.
-
Yep, the collider was intersecting with the camera :-\ Thanks for the help!!