- Home /
Multiple classes delegates combining methods?
I'm gonna be honest, i'm not sure if i understand delegates yet.
The thing, i thought that after registering a method belonging to a specific to a delegate of another class that ONLY the code from that method would run on the assigned gameobject. But that doesn't seems to be the case.
I have this setup with two different kinds of buttons and i have a handler that detects the input and calls the delegate. But every button runs the code of the TWO different kinds of buttons.
Why does this happens and how you do this correctly?
Can you show some code of how you are adding the delegates, and show the definition of what you are adding it to?
Yeah we need a code snippet to see what you're doing. Your declarations, how you're calling it etc.
Either he fix it, or he just gave up. Sometimes I wonder why we care so much while the one asking the question doesn't seem to care at all.
Sorry, i completely missed the first reply email. From my understanding delegates always run on every object that adds the same delegate method, so i ended up doing a check to see if the object calling it is the same of the running method... it works for now but is this really the intended way of using delegates?
I'll post a code snippet tomorrow at work.
this is my declaration on a touch handler:
public delegate void handleTouchBegan(RaycastHit rh, int fingerid);
public handleTouchBegan BeganTouch;
then i use it in mul$$anonymous$$ch loop like this
if (touch.phase == TouchPhase.Began){
if (Physics.Raycast (ray, out hitInfo, 1.0e8f)){
BeganTouch(hitInfo, touch.fingerId);
}
}
finally, on each button handler i have a code like this:
void Start () {
TouchHandler.instance.BeganTouch += handleTouchBegan;
}
void handleTouchBegan(RaycastHit hit, int id){
fid = id;
if(hit.collider.gameObject == this.gameObject){
//Do Something
}
}
Answer by dubbreak · Feb 19, 2013 at 12:15 AM
This might be of help? For my buttons (I use ngui) I have this script added on to them:
using UnityEngine;
using System.Collections;
using System;
public class EventButton : MonoBehaviour {
public event Action Clicked;
public bool buttonEnabled { get; set; }
void Start()
{
buttonEnabled = true;
}
void OnClick()
{
if (Clicked != null && buttonEnabled) Clicked();
}
}
OnClick gets called by ngui (since it's attached to an ngui button) by the normal unity messaging. The gist is OnClick gets called when it is clicked which then calls my own event. Since you're not using ngui you use your touch logic to determine it has been clicked. I'm using the built in Action delegate since I'm not sending info with it, could be a different custom delegate or
public delegate void ClickDelegate;
public event ClickDelegate Clicked;
Action is just easier and cleaner if you don't have any arguments.
I then reference my buttons from my main script:
public class GameManager : MonoBehaviour {
public EventButton playButton;
public EventButton leaveButton;
void Start()
{
playButton.Clicked += HandlePlay;
}
private void HandlePlay()
{
DoPlayStuff(true);
}
}
That's simplified a bit since I don't subscribe to the event until network stuff happens and some buttons get subscribed and unsubcribed depending on state.. but it gives you and idea of how I'm using events to deal with buttons.
In case anyone that's using ngui reads this, I think the latest ngui version has events you can subscribe to now (rather than just the unity messaging). So you shouldn't have to roll your own like this.
whats the advantage of using events?
and one clear difference from my approach is that you "subscribe" your buttons on the manager while i do it on the button itself, i did it like this because that way i could add any kind of button to the scene without having to change the manager
If it's within the button itself then why have an event? You can just run whatever method you need (using a coroutine if it's going to take a while).
If you don't have something external subscribing to the event I don't see the point of having an event.
Also you don't have to hardcode the references to the buttons like I did. Simple example. Ins$$anonymous$$d you can get all the objects with "eventbutton" components and subscribe to the click. Then you can deter$$anonymous$$e what button is being clicked in the handler. The point is so that you don't have to hardcode the button to references of other external things that should happen. It just says ,"Hey someone clicked me, if anyone cares here's the info and you can go do your own thing."
If you look how buttons are handled in winforms (for example) the form subscribes to the event in the form.designer.cs (adding whatever your handler method is). To do specific things you have to subscribe from somewhere. And to do something interesting it's external to the object raising the event.
Your answer

Follow this Question
Related Questions
Action delegate doesn't show in inspector. 1 Answer
Delegate bug, or intended behaviour? 0 Answers
Interaction script 2 Answers
Singleton class or delegate? 1 Answer