is there a way to get some kind of info or warning, when any Button component has a missing method?
What I mean by that is that when you implement a method for a Button, assign that method in the scene and then e.g. rename the method, the rename won't update the method call in the scene and so it says "< Missing ScriptName.OldMethodName >".
When this happens I'd like to get notified about that - at least when pressing play, or at the very least when deploying the application.
you can extend the button class and check for event counts + method names, if the name contains missing, fire a error
Scott's answer is very close to what you are doing and led to this answer. Although it is missing so many things. You need to do more work to get that script working.
1.You need to get all the Buttons in the scene (including inactive/disabled ones) with
Resources.FindObjectsOfTypeAll
.2.Loop through the buttons and check if the class/script that holds that function exist with reflection. You do this because sometimes, we rename scripts. This could cause problems. Show message of the script does not exist.
You can do this by simply checking if
Type.GetType(className);
isnull
.If it is
null
then don't even both the test below because the component that holds thatButton
'sonClick
function has been re-named. Display an error message that says that this script has been deleted or re-named.3.If the class exist, now check if the function exist with reflection in that
class
that is registered to the Button'sonClick
event.This can be done by simply checking if
type.GetMethod(functionName);
isnull
.If the function exist, that Button is fine. You don't have to show a message if the function exist. Stop here.
If that returns
null
then continue to #4.4.Check if the function exist, but this time, check if the function is declared with a
private
access modifier.This is a typical mistake people make. They declare function as
public
, assign it through the Editor, then mistakenly change it frompublic
toprivate
. This should work but can cause problem in the future.This can be done by simply checking if
type.GetMethod(functionName, BindingFlags.Instance | BindingFlags.NonPublic);
isnull
.If the function exist, show a message that warns you that this function's access modifier has been changed from
public
toprivate
and should be changed back topublic
.If the function does not exist, show a message that warns you that this function does no longer exist or has been re-named.
Below is a script that performs everything I mentioned above. Attach it to an empty GameObject and it will do its job anytime you run the game in the Editor.