Unity使用odin,在某一脚本中,利用反射执行测试代码

一般的
思想1:想打开某一页面_使用快捷键(F7),特定的代码方法体里,EditorMothodF7()中做完A功能的测试代码就删除里面的方法体,过一两个月后又得再次测,又回到原本的EditorMothodF7()中补充回去,原本的内容又得删除…反反复复,就很烦
思想2:在GM里,if-elseif 里追加代码…也只能在Game视图中,点击UI元素咯,有些还不要在Game使用的,另GM代码里,就很杂乱了
思想3…

总结
应用场景:写了一段代码,测试完会还想着之后还要删除,就很烦.(有时还要测时,又得再写一遍,烦) 现只需在EditorForDebugScript.cs追加代码则可,然后在下拉框选择方法名
在这里插入图片描述
代码 EditorForDebugWindow.cs

using System;
using System.Collections.Generic;
using System.Reflection;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
using Sirenix.Utilities;
using UnityEngine.Serialization;
[InfoBox("这是一个调用方法的窗口\r\n应用场景:写了一段代码,测试完会还想着之后还要删除,就很烦.(有时还要测时,又得再写一遍,烦)\r\n              现只需在EditorForDebugScript.cs追加代码则可,然后在下拉框选择方法名", InfoMessageType.Info, null)]
public class EditorForDebugWindow : OdinEditorWindow
{    
    [MenuItem("Tools/辅助工具/方便_调用代码 &#%D", priority = 200)]
    public static void ShowNetTool()
    {
        var win = GetWindow<EditorForDebugWindow>("方便_调用代码");
        win.position = GUIHelper.GetEditorWindowRect().AlignCenter(520, 260);
    }
    /// <summary> 构造函数 </summary>
    private EditorForDebugWindow()
    {
        GetAllMethodsForScripts();
    }
    private Dictionary<string, MethodInfo> mDebugMethod = new Dictionary<string, MethodInfo>();
    [ValueDropdown("mMethodList", IsUniqueList = true)] [LabelText("方法名"), OnValueChanged("ChangeMethodDropdown")]
    public string mMethodName="OpenTestNoParam";
    void ChangeMethodDropdown()
    {
        if (mDebugMethod.TryGetValue(mMethodName, out var method))
        {
            ParameterInfo[] parameterInfos = method.GetParameters();
            var strValue = ""; //若无参数  下面的for 也不会进入 的
            var strName = "";
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                var str = GetDefaultValue(parameterInfos[i].ParameterType);
                strValue += $" {str} ::";
                strName += $"{parameterInfos[i].Name}::";
            }
            mNameParamTxt = $"{mMethodName}::{strValue}".TrimEnd("::".ToCharArray());
            paramNames =$"方法名::参数1::参数2::参数....   使用冒号分隔  {mMethodName}::{strName}".TrimEnd(':');
        }
        else
        {
            mNameParamTxt = mMethodName;
            paramNames =$"方法名::参数1::参数2::参数....    使用冒号分隔 ";
        }
    }
    private List<string> mMethodList = new List<string>();
    [FormerlySerializedAs("mMethodNameTxt")] [LabelText("@ paramNames"), Space(15), TextArea(5, 5)]
    public string mNameParamTxt = "OpenTestNoParam";
    private string paramNames = "方法名::参数1::参数2::参数....            使用冒号分隔  ";//文本提示框
    [Button("调用方法", ButtonSizes.Medium, ButtonHeight = 30)]
    public void UsingMethod()
    {
        if (string.IsNullOrEmpty(mNameParamTxt))
        {
            Debug.LogError("上面的方法名与参数 未填入哦");
            return;
        }
        var strList = mNameParamTxt.Split("::");
        if (mDebugMethod.TryGetValue(strList[0], out var method))
        {
            Type type = typeof(QQTest.EditorForDebugScript);
            var instance = (QQTest.EditorForDebugScript)Activator.CreateInstance(type);
            var sum = strList.Length;

            ParameterInfo[] parameterInfos = method.GetParameters();
            var parameters = new object[parameterInfos.Length]; //若无参数  下面的for 也不会进入 的
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                if (i + 1 < sum)
                {
                    parameters[i] = Convert.ChangeType(strList[i + 1], parameterInfos[i].ParameterType); // 如果提供了参数值,则使用提供的参数值
                }
                else
                {
                    parameters[i] = GetDefaultValue(parameterInfos[i].ParameterType); // 如果没有提供参数值,则使用默认值
                }
            }
            method.Invoke(instance, parameters);
        }
    }

    private object GetDefaultValue(Type type)
    {
        if (type == typeof(string))
        {
            return "str_null";
        }
        else if (type == typeof(float))
        {
            return "0.0";
        }
        else if (type.IsValueType)
        {
            return Activator.CreateInstance(type);
        }
        return null;
    }

    public void GetAllMethodsForScripts()
    {
        Type type = typeof(QQTest.EditorForDebugScript);
        MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

        foreach (MethodInfo method in methods)
        {
            if (method.Name == "Equals" || method.Name == "Finalize" || method.Name == "GetHashCode" ||
                method.Name == "GetType" || method.Name == "MemberwiseClone" || method.Name == "ToString")
            {
                //非 手写的 方法
            }
            else
            {
                mDebugMethod[method.Name] = method;
                mMethodList.Add(method.Name);
            }
        }
    }
}

代码 EditorForDebugScript.cs

using System.Collections.Generic;
using Hotfix.Modules.GM;
using Pb;
using QQu;
using QQu.UI;
using UnityEngine;
namespace QQTest
{
    public class EditorForDebugScript
    {
        /// <summary>测试  无参数   </summary>
        public void OpenTestNoParam()
        {
            Debug.LogError($"测试 调用了 OpenTestNoParam");
        }
        /// <summary> 测试 传入参数   通用的参数都使用了</summary>
        public void OpenTestToParam(uint pId, string name, bool pBool, int cfgId = 1, float pFloat = 1.1f)
        {
            Debug.LogError($"测试 调用了 OpenTestToParam  传入的  {pId} {name} {pBool} {cfgId} {pFloat}");
        }
        public void SendGM(string str)
        {
            if (string.IsNullOrEmpty(str) || str.Contains("str_null")) str = "player_currency.IncCurrency(1, 10000, true)";
            GMManager.Ins.ExecuteGM(str);
        }
        /// <summary> 打开通用奖励页面   </summary>
        public void OpenRewardViewToParam(uint pId, int pNum, int pType = 1)
        {
            if (pId <= 0) pId = 1;
            if (pNum <= 0) pNum = 1;
            CommonUtils.Ins.OpenComRewardView(new List<ItemInfo>()
            {
                new ItemInfo() { Id = pId, Num = (uint)pNum },
            }, pType); //打开通用的奖励页面
        }
        /// <summary> 测试打开页面   功能解锁页面</summary>
        public void OpenUnlockView(int key, int val)
        {
            if (key <= 0) key = 2;
            if (val <= 0) val = 1;
            PropIntInfo info = new PropIntInfo() { Key = 2, Val = 2 };
            UIMgr.Ins.Show<FunctionUnlockView>(IUIParam.Create(info)).Task();
        }
        //------------------------供 继续写------------
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值