Last active
August 1, 2016 16:50
-
-
Save lonewolfwilliams/f4bd304e7c9d974cb04774ffba14685c to your computer and use it in GitHub Desktop.
track event delegate registration, to spot unbalanced addition and removal...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//sometimes, when it's not your codebase and you have a lot of events flying round, | |
//it's useful to spot where an event may have been registered twice on the same component, | |
//or added and not removed (reference retained outside lifecylce of GameObject) | |
//this expansion of the event property may help... | |
//note: for whatever reason target may be null... | |
//pro tip: use the console to backtrack to the source of the registration | |
// | |
//idea adapted from juval Lowy: Programming .Net Components (O' Reilly) | |
private event EventHandler m_onClick; | |
public event EventHandler onClick | |
{ | |
add | |
{ | |
m_onClick += value; | |
Debug.LogFormat("adding delegate {0} to GO {1} now has {2} delegates", (value as Delegate).Target, GetInstanceID(), m_onClick.GetInvocationList().Length); | |
} | |
remove | |
{ | |
Debug.LogFormat("removing delegate {0} to GO {1} now has {2} delegates", (value as Delegate).Target, GetInstanceID(), m_onClick.GetInvocationList().Length); | |
m_onClick -= value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment