Can you have an event trigger mercurial delegates?

I have a class. I want to perform a function in that class, when OnPreRender is called on other objects with cameras. So I’ve got this, on those game objects:

public delegate void EventHandler ();
public event EventHandler PreRender;

void OnPreRender () {PreRender();}

Now, in the other class, I want to run a delegate, when that event happens.

cameraThing.PreRender += theDelegate;

But that doesn’t work. So, I can do:

cameraThing.PreRender += new CameraThing.EventHandler(theDelegate);

But, then, when I change what theDelegate does, the event handling doesn’t reflect that, and continues to do what theDelegate did when the event was registered. I’d like to avoid having to unregister and reregister, when theDelegate changes. Possible?

Not sure, but can you make theDelegate itself an Event handler chain rather than a method? You’d still have to register and unregister somewhere, maybe just not there.

BTW, why do you not want to register/unregister? I often find it a pain registering events on a subclass whose parent contains the actual event, but other than that it is usually possib;le to find a good place to do it.

I’d love to see an example of what you mean.

I’m always going to want to do something when OnPreRender happens for each of those cameras. So altering the function sounds like a better idea than unregistering an old function, and registering a new one. I don’t understand the inner workings of all this to know if that makes sense to the compiler, but it makes sense to me, and it would be less code.

I don;t know enough about the underlying compiler mechanisms to know if exactly what you want is possible. I’m curious though. If I get a moment, I’ll do some experimenting. Meanwhile, I often use a specific method to handle to changes with delegates. It might be useful to you.

	GUIManager.DrawGUIHandler currentDrawGUI;
	void Awake() {
		GUIStateChangeTo(new GUIManager.DrawGUIHandler(DrawGUIA));
	}
	
	
[COLOR="red"]	void GUIStateChangeTo(GUIManager.DrawGUIHandler changeTo) {
		if (currentDrawGUI != null) {
			GUIManager.OnDrawGUI -= currentDrawGUI;
		}
		currentDrawGUI = changeTo;
		GUIManager.OnDrawGUI += currentDrawGUI;		
	}[/COLOR]
	
	void DrawGUIA() {
		if (GUI.Button (new Rect ( 100, 110, 100, 20), "Switch To B")) {
			GUIStateChangeTo(new GUIManager.DrawGUIHandler(DrawGUIB) );
                }
	}
	
	void DrawGUIB() {
		if (GUI.Button (new Rect ( 200, 110, 100, 20), "Switch To A")) {
			GUIStateChangeTo(new GUIManager.DrawGUIHandler(DrawGUIA) );
                }
	}