- Home /
How to subscribe method to OnDisable in a gameObject.
Hi, I've just started using events and delegates, I know how to subscribe to some events in UI buttons/elements. But how do you subscribe or un-subscribe a method to the OnEnable/OnDisable events in a GameObejct? (if that's possible?).
I'm basically making a little prompt system with different possible types of prompts (which is using functionality from a third party asset-not explicitly made for that).
Anyway, I need to do add the method from an external script because I'm not always going to be doing this to the same menu and I'm not always going to be passing the same method. Also, at times the menus are going to be shared - one object will have to wait until the menu is free to be used (a limitation in the third party asset... the asset doesn't have a method to instantiate it's own menus, because the core asset doesn't need it...).
Sadly, I'm still too green to replicate the features provided by the asset, and if possible I'd like to not have to attach anything extra to the menus to make setup always easy. Hence I want to use events... And I need to trigger some random method when one of the prompts closes (I'm currently using a coroutine with wait time to detect it)...
Answer by edwingohbh88 · Aug 26, 2016 at 04:47 AM
You can take a look in this link, can use delegates to subscribe and unsubscribe in OnEnable(), OnDisable() methods respectively.
http://www.theappguruz.com/blog/using-delegates-and-events-in-unity
Alternatively, if you want to have a custom delegate to cater for all sorts of event, you can create a custom class to hold the delegate
// class to hold the delegates
namespace MyPackage
{
public delegate void CustomAction(object[] param);
}
// class to run it
public class TestClass : MonoBehaviour
{
public static CustomAction OnActionClicked;
void OnEnable()
{
TestClass.OnActionClicked += HandleOnClick;
}
void OnDisable()
{
TestClass.OnActionClicked -= HandleOnClick;
}
}
For full source, you can take a look at my GitHub, i've written some code to it. https://github.com/gohbiscuit/UnityCustomCoroutine/tree/master/UnityCoroutine
Hope it helps.
Thanks, I'll accept the answer, but I haven tried it yet (will see later today). But if you don't $$anonymous$$d, an example of subscribing directly to OnEnable and Ondisabled would've been great... I had tried to get the GameObject from the menus and see if I could add a function/delegate to it but when I do I don't see the option appear like it does for OnClick. And If I try to just write OnDisable anyway I get a red warning. Is that somewhere else and not in the game object? Do I have to address monobehaviour or something? Or Are this events not native in the object like the onclick is in buttons? Do I have to be forced put a script on the menu with blank OnDisable function?
Hi Alverik, I am not quite sure what u mean but if you mean something like triggered an event when the button is enabled or disabled you can do something like.
// class to hold the delegates
namespace $$anonymous$$yPackage
{
public delegate void CustomAction(object[] param);
}
// another class
public class $$anonymous$$enuClass
{
// any na$$anonymous$$g you like, because is a custom delegate u defined in your namespace
public static CustomAction OnButtonEnable;
public Button testButton;
void EnableButtonTest()
{
testButton.setActive(true);
// I want to send a message to tell every class listening to the event that I am active
// always check for not null when invoke the event
if(OnButtonEnable != null)
// since it takes in an object[] param, you can pass parameters to it
OnButtonEnable(true);
}
void DisableButtonTest()
{
testButton.setActive(false);
// I want to send a message to tell every class listening to the event that I am inactive
if(OnButtonEnable != null)
OnButtonEnable(false);
}
}
// Class Listening to the event
public class AnyClass : $$anonymous$$onoBehaviour
{
void OnEnable()
{
// subscribing for "ButtonClass" OnButtonEnable event,
// when class is active, because it is static you do not need to create a instance of that class.
ButtonClass.OnButtonEnable += HandleButtonEnable;
}
void OnDisable()
{
// subscribe when class is inactive
ButtonClass.OnButtonEnable -= HandleButtonEnable;
}
void HandleButtonEnable(object[] param)
{
//button enable
if((bool) param[0] == true)
{...}
// button disabled
else
{...}
}
}
This might be a little complicated but i hope it helps, u can look into my github, there are more examples there you can use.
Thanks for the example, I'll take my time to read it. Anyways, sorry I wasn't been clear, but I meant to subscribe to the onDisable and onEnable on the UI Canvas. What I was wondering, was if you had to have a script with a declared OnEnable and OnDisable on the canvas to be able to access it remotely (from another script). Or if it was natively accessible? (sorry I'm new to unity and program$$anonymous$$g so I'm not sure how things work yet. $$anonymous$$y knowledge is like swiss cheese right now).