Tav*_*dos 5 c# unity-game-engine
当任何Button组件缺少方法时,是否有办法获取某种信息或警告?
我的意思是,当您为Button实现方法时,在场景中分配该方法,然后重命名该方法,重命名不会更新场景中的方法调用,因此它说“ <Missing ScriptName。 OldMethodName>”。
发生这种情况时,我希望得到通知-至少在按下play时,或者至少在部署应用程序时。
斯科特的答案非常接近你正在做的事情并导致了这个答案。虽然缺少了很多东西。您需要做更多的工作才能使该脚本正常运行。
1 .您需要使用 获取场景中的所有按钮(包括非活动/禁用的按钮)Resources.FindObjectsOfTypeAll
。
2.循环遍历按钮并检查保存该函数的类/脚本是否存在反射。您这样做是因为有时我们会重命名脚本。这可能会导致问题。显示脚本的消息不存在。
您可以通过简单地检查 if Type.GetType(className);
is来做到这一点null
。
如果是,null
则甚至不进行下面的测试,因为保存该函数Button
的组件onClick
已被重命名。显示一条错误消息,指出该脚本已被删除或重命名。
3 .如果该类存在,现在检查该函数是否存在并具有反射,因为class
该函数已注册到按钮的onClick
事件。
type.GetMethod(functionName);
这可以通过简单地检查 if is来完成null
。
如果该功能存在,则该按钮就可以。如果该函数存在,则不必显示消息。停在这里。
如果返回null
则继续#4。
4.检查该函数是否存在,但这一次,检查该函数是否使用private
访问修饰符声明。
这是人们常犯的错误。他们将函数声明为public
,通过编辑器分配它,然后错误地将其从 更改public
为private
。这应该可行,但可能会在将来引起问题。
type.GetMethod(functionName, BindingFlags.Instance | BindingFlags.NonPublic);
这可以通过简单地检查 if is来完成null
。
如果该函数存在,则显示一条消息,警告您该函数的访问修饰符已从 更改为public
且private
应更改回public
。
如果该函数不存在,则显示一条消息,警告您该函数不再存在或已被重命名。
下面是一个执行我上面提到的所有操作的脚本。将其附加到一个空的游戏对象,只要您在编辑器中运行游戏,它就会完成其工作。
using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
public class MissingOnClickDetector : MonoBehaviour
{
void Awake()
{
//Debug.Log("Class exist? " + classExist("ok.ButtonCallBackTest"));
searchForMissingOnClickFunctions();
}
void searchForMissingOnClickFunctions()
{
//Find all Buttons in the scene including hiding ones
Button[] allButtonScriptsInScene = Resources.FindObjectsOfTypeAll<Button>() as Button[];
for (int i = 0; i < allButtonScriptsInScene.Length; i++)
{
detectButtonError(allButtonScriptsInScene[i]);
}
}
//Searches each registered onClick function in each class
void detectButtonError(Button button)
{
for (int i = 0; i < button.onClick.GetPersistentEventCount(); i++)
{
//Get the target class name
UnityEngine.Object objectName = button.onClick.GetPersistentTarget(i);
//Get the function name
string methodName = button.onClick.GetPersistentMethodName(i); ;
//////////////////////////////////////////////////////CHECK CLASS/SCRIPT EXISTANCE/////////////////////////////////////////
//Check if the class that holds the function is null then exit if it is
if (objectName == null)
{
Debug.Log("<color=blue>Button \"" + button.gameObject.name +
"\" is missing the script that has the supposed button callback function. " +
"Please check if this script still exist or has been renamed</color>", button.gameObject);
continue; //Don't run code below
}
//Get full target class name(including namespace)
string objectFullNameWithNamespace = objectName.GetType().FullName;
//Check if the class that holds the function exist then exit if it does not
if (!classExist(objectFullNameWithNamespace))
{
Debug.Log("<color=blue>Button \"" + button.gameObject.name +
"\" is missing the script that has the supposed button callback function. " +
"Please check if this script still exist or has been renamed</color>", button.gameObject);
continue; //Don't run code below
}
//////////////////////////////////////////////////////CHECK FUNCTION EXISTANCE/////////////////////////////////////////
//Check if function Exist as public (the registered onClick function is ok if this returns true)
if (functionExistAsPublicInTarget(objectName, methodName))
{
//No Need to Log if function exist
//Debug.Log("<color=green>Function Exist</color>");
}
//Check if function Exist as private
else if (functionExistAsPrivateInTarget(objectName, methodName))
{
Debug.Log("<color=yellow>The registered Function \"" + methodName + "\" Exist as a private function. Please change \"" + methodName +
"\" function from the \"" + objectFullNameWithNamespace + "\" script to a public Access Modifier</color>", button.gameObject);
}
//Function does not even exist at-all
else
{
Debug.Log("<color=red>The \"" + methodName + "\" function Does NOT Exist in the \"" + objectFullNameWithNamespace + "\" script</color>", button.gameObject);
}
}
}
//Checks if class exit or has been renamed
bool classExist(string className)
{
Type myType = Type.GetType(className);
return myType != null;
}
//Checks if functions exist as public function
bool functionExistAsPublicInTarget(UnityEngine.Object target, string functionName)
{
Type type = target.GetType();
MethodInfo targetinfo = type.GetMethod(functionName);
return targetinfo != null;
}
//Checks if functions exist as private function
bool functionExistAsPrivateInTarget(UnityEngine.Object target, string functionName)
{
Type type = target.GetType();
MethodInfo targetinfo = type.GetMethod(functionName, BindingFlags.Instance | BindingFlags.NonPublic);
return targetinfo != null;
}
}
Run Code Online (Sandbox Code Playgroud)