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.

1 Answer

1

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