Unity封装一个像脚本语言print(...)一样,输出可变参数的Log方法

Unity封装一个像脚本语言print一样,输出可变参数的Log方法

【前言】

我们在使用脚本语言(lua或者python)之后会发现有个常用的输出log方法,他的原型大致为

--原型,其中...是传递的可变参数列表
--print(...)

--举个例子
print(1,"two",true) -->1	two	true

以上lua代码输出了1 two true,对于同时输出多个对象就可以方便的打log。
而Unity中的UnityEngine.Debug.Log和MonoBehaviour.print都只有一个参数的重载,所以我们用C#中实现像lua中print(…)一样可以传递多个对象参数,然后整条输出log。

【放出代码】

Debug.SuperLog接口封装

public class Debug
{
    
    
    public static void SuperLog(params object[] obj)
    {
    
    
        object deubgObject = null;
        for (int i = 0; i < obj.Length; i++)
        {
    
    
            deubgObject += obj[i].GetType().Name+":"+ obj[i] + "        ";
            //deubgObject += "参数" + (i + 1) as string + ":" + obj[i] + "        ";
        }
        UnityEngine.Debug.Log(deubgObject);
    }
}

简单调用测试

    private void Start()
    {
    
    
        string par1 = "string1";
        int par2 = 123;
        bool par3 = true;
        GameObject par4 = this.gameObject;
        Debug par5 = new Debug();
        
        Debug.SuperLog(par1,par2,par3,par4,par5);//调用入口    
    }

测试输出结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42541751/article/details/122396028