Button缺少OnClick时Unity缺少警告 [英] Unity missing warning when Button has missing OnClick

查看:114
本文介绍了Button缺少OnClick时Unity缺少警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当任何Button组件缺少方法时,是否可以获取某种信息或警告?

is there a way to get some kind of info or warning, when any Button component has a missing method?

我的意思是,当您为Button实现一种方法时,请在场景中分配该方法,然后例如重命名该方法,重命名不会更新场景中的方法调用,因此它显示为<缺少ScriptName.OldMethodName>".

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 >".

发生这种情况时,我希望得到通知-至少在按下play时,或者至少在部署应用程序时.

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.

推荐答案

Scott的 answer 与您的情况非常接近这样做并导致了这个答案.虽然缺少很多东西.您需要做更多的工作才能使该脚本正常工作.

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 .您需要使用Resources.FindObjectsOfTypeAll来获取场景中的所有按钮(包括未激活/已禁用的按钮).

1.You need to get all the Buttons in the scene (including inactive/disabled ones) with Resources.FindObjectsOfTypeAll.

2 .循环浏览按钮,检查包含该功能的类/脚本是否存在反射.您这样做是因为有时我们会重命名脚本.这可能会引起问题.脚本的显示消息不存在.

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.

您可以通过简单地检查Type.GetType(className);是否为null来做到这一点.

You can do this by simply checking if Type.GetType(className); is null.

如果它是null,则不要同时进行下面的测试,因为保存了ButtononClick函数的组件已重命名.显示一条错误消息,指出该脚本已被删除或重命名.

If it is null then don't even both the test below because the component that holds that Button's onClick function has been re-named. Display an error message that says that this script has been deleted or re-named.

3 .如果该类存在,现在检查该函数是否在反射到事件注册的class中存在.

3.If the class exist, now check if the function exist with reflection in that class that is registered to the Button's onClick event.

这可以通过简单地检查type.GetMethod(functionName);是否为null来完成.

This can be done by simply checking if type.GetMethod(functionName); is null.

如果该函数存在,则该Button很好.如果该功能存在,则无需显示消息.到此为止.

If the function exist, that Button is fine. You don't have to show a message if the function exist. Stop here.

如果返回null,则继续执行#4 .

If that returns null then continue to #4.

4 .检查功能是否存在,但是这次,检查功能是否使用private 访问修饰符声明.

4.Check if the function exist, but this time, check if the function is declared with a private access modifier.

这是人们犯的典型错误.他们将函数声明为public,通过编辑器将其分配,然后错误地将其从public更改为private.这应该可行,但将来可能会引起问题.

This is a typical mistake people make. They declare function as public, assign it through the Editor, then mistakenly change it from public to private. This should work but can cause problem in the future.

这可以通过简单地检查type.GetMethod(functionName, BindingFlags.Instance | BindingFlags.NonPublic);是否为null来完成.

This can be done by simply checking if type.GetMethod(functionName, BindingFlags.Instance | BindingFlags.NonPublic); is null.

如果该函数存在,请显示一条消息,警告您该函数的访问修饰符已从public更改为private,应更改回public.

If the function exist, show a message that warns you that this function's access modifier has been changed from public to private and should be changed back to public.

如果该功能不存在,请显示一条消息,警告您该功能已不存在或已被重命名.

If the function does not exist, show a message that warns you that this function does no longer exist or has been re-named.

下面是执行我上面提到的所有操作的脚本.将它附加到一个空的 GameObject 上,只要您在编辑器中运行该游戏,它就可以完成工作.

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.

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;
    }
}

这篇关于Button缺少OnClick时Unity缺少警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆