Thank you @Senshi very much for the rapid reply, and my apologies for a delayed response.
If I can describe this setup you will likely be able to offer advice for how to proceed - obviously if it suits your fancy
EventRelay.cs
usingUnityEngine;
usingSystem.Collections;
publicclassEventRelay : BaseMonoBehaviour {
publicdelegatestringEventAction(EventMessageTypetype, Objectsender);
publicstaticeventEventActionOnEventAction;
publicenumEventMessageType {
GuiElementPicked,
ObjectCollected,
DefenseChosen,
DefenseActivated,
LoadGamePicked,
SaveGamePicked,
MainMenuScreenPicked,
UpgradeShopPicked,
SocialMenuPicked,
WeaponChosen,
WeaponFired,
InventoryChecked,
ItemAddedToInventory,
ItemRemovedFromInventory,
QuestionAnswered,
QuestionPicked,
DebugUIOnPicked,
DebugUIOffPicked,
PlayButtonPicked,
EnemyAffected
}
publicstaticstringRelayEvent(EventMessageTypemessageType, Objectsender) {
returnOnEventAction(messageType, sender);
}
}
EventSender.cs (wired up using the Editor > Inspector)
usingUnityEngine;
usingSystem.Collections;
publicclassEventSender : BaseMonoBehaviour {
publicboolmouseIsOverThis = false;
publicObjectsender;
//Updateiscalledonceperframe
voidUpdate () {
if(mouseIsOverThis) {
if(Input.GetMouseButtonDown((int)MouseUtils.Button.Left)) {
stringvalue = EventRelay.RelayEvent(
EventRelay.EventMessageType.GuiElementPicked, sender);
Debug.Log("GuiElementPicked " + value);
}
}
}
publicvoidOnMouseEnter(ObjecteventSource) {
mouseIsOverThis = true;
sender = eventSource;
}
publicvoidOnMouseClick(ObjecteventSource) {
mouseIsOverThis = true;
sender = eventSource;
}
publicvoidOnMouseExit(ObjecteventSource) {
mouseIsOverThis = false;
sender = eventSource;
}
}
EventListener.cs - added to an item that needs to handle an event
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
publicclassEventListener : BaseMonoBehaviour {
publicList<EventRelay.EventMessageType> eventsHandled =
newList<EventRelay.EventMessageType>();
voidOnEnable() {
EventRelay.OnEventAction += HandleEvent;
}
voidOnDisable() {
EventRelay.OnEventAction -= HandleEvent;
}
stringHandleEvent(EventRelay.EventMessageTypemessageType, Objectsender) {
if(eventsHandled.Contains(messageType)) {
Debug.Log("Handled event: " + messageType + " from sender: " + sender.ToString());
returnthis.ToString();
} else {
//ignoreevent
returnthis.ToString();
}
}
}
The plan is to pass the generic GuiElementPicked event to the relay from the UI button (any UI button), along with the button (or other UI element) itself. So that my listener can evaluate which buttons were raising the events and the listener (on the GameManager or other object) could then invoke methods in the GameManager (TBD).
Another complication that I haven’t gotten to evaluate is that these UI elements, buttons and such - need to be in prefabs, and instantiated at runtime. Firstly via the action of a bootstrapping function in the GameManager (a persistent GameObject on the main scene). GuiLayouts (collections of controls, images, text fields, labels, etc) would be instantiated from a prefab library at run time. What problems do you see arising from this approach? I was planning on loading levels this way [possibly], and also NPCs, the Player, etc. Is this realistic in your opinion? Many thanks for the help. Feel free to contact me out-of-band if you prefer, or whatever suits. Thanks!