吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5090|回复: 7
收起左侧

[其他原创] UnityDebug-打印出Unity游戏场景以及UI信息

  [复制链接]
CrazyNut 发表于 2019-9-13 23:45
本帖最后由 CrazyNut 于 2019-9-13 23:47 编辑

Unity游戏想要直接动态调试游戏内容好像很难,所以写了一个简单的脚本来实现打印场景信息,方便分析一点点。。。
这段代码适用与UGUI,Mono打包的Unity程序。
主要用于汉化(修改版权)
功能可以动态打印出当前按钮的点击事件,塞图片进游戏,寻找场景中的Text等等。。

这些方法写的很乱。。平时想要什么功能就加上什么功能。。。主要是方便自己的。
本想整理下再发出来,但这这9月大学刚刚开学,事情有一些多,所以一直没空整理
但马上动画大赛要结束了。。这里两天参加动画大赛要用到这个东西  就简单注释一下发出来了。。
对新人应该能有一些帮助。。


具体使用方法就是dnspy把Assembly-CSharp.dll拖进去后 把整个类添加进去, 然后在需要的地方调用对应的方法就行
[C#] 纯文本查看 复制代码
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace NutUnityDebug
{
    public delegate void gm_keydown_Action();


    public class Nut : MonoBehaviour
    {

        public Nut(MonoBehaviour theMonoBehaviour)
        {
            this.MyMonoBehaviour = theMonoBehaviour;
        }
         
                //打印出当前的场景名字以及Unity版本
        public static void NutDBG_showNowSencen()
        {
            UnityEngine.SceneManagement.Scene scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
            Debug.Log(scene.buildIndex + "==NutDBG_showNowSencen==" + scene.name);
            Debug.Log(Application.unityVersion + "  ==Application.unityVersion==Application.version==" + Application.version);


        }
  
        //获取参数GameObject在场景中的路径
        public static string NutDBG_getThePath(GameObject theGameObject)
        {
            string text = Nut.NutDBG_getThePath_temp(theGameObject, theGameObject.name);
            Text component = theGameObject.GetComponent<Text>();
            if (component != null)
            {
                text = text + "----Text->" + component.text;
            }
            return text;
        }
                
                
        private static string NutDBG_getThePath_temp(GameObject theGameObject, string returnPath)
        {
            Transform temp = theGameObject.transform.parent;

            if (temp != null)
            {
                returnPath = temp.name + "/" + returnPath;

                GameObject theTemp;

                returnPath = NutDBG_getThePath_temp(temp.gameObject, returnPath);

            }

            return returnPath;
        }

        //按下对应的按键执行对应的功能
        static float theConTime = 1; 
        public static void NutDBG_keydown(gm_keydown_Action temp)
        {
            theConTime -= Time.deltaTime;

            if(theConTime < 0)
            {
                if (Input.GetKey(KeyCode.D))//按下D键
                {
                    theConTime = 10;
                    NutDBG_showAll();       //把当前场景所有GameObject的名字显示在物体上
                }
                if (Input.GetKey(KeyCode.P))//按下P键
                {
                    theConTime = 10;
                    NutDBG_showAllPath();   //显示当前场景所有GameObject的路径
                }
                if (Input.GetKey(KeyCode.S))//按下S键
                {
                    theConTime = 10;
                    NutDBG_showNowSencen(); //打印出当前的场景名字以及Unity版本
                }
      
            }
        }
                
        //显示当前场景所有GameObject的路径
        public static void NutDBG_showAllPath()
        {
            string text = "";
            int thei = 0;
            foreach (GameObject gameObject in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
            {
                MonoBehaviour.print(thei + "==NutDBG_showAllPath==>" + Nut.NutDBG_getThePath(gameObject));
                thei++;
            }
            Debug.Log("-->" + text);
        }
                
        //把当前场景所有GameObject的名字显示在物体上
        public static void NutDBG_showAll()
        {
            Debug.Log("NutDBG_showAll-------------->");
            string text = "";
            foreach (GameObject gameObject in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
            {
                if (gameObject.GetComponent<RectTransform>() != null)
                {
                    GameObject gameObject2 = new GameObject("new=" + gameObject.name);
                    gameObject2.transform.parent = gameObject.transform;
                    gameObject2.transform.localPosition = new Vector3(0f, -30f + UnityEngine.Random.value * 60f, 0f);
                    gameObject2.AddComponent<Text>().text = "==" + gameObject.name;
                    gameObject2.GetComponent<Text>().color = new Color(1f, 0f, 1f);
                    gameObject2.GetComponent<Text>().fontSize = 12;
                    gameObject2.GetComponent<Text>().horizontalOverflow = HorizontalWrapMode.Overflow;
                    gameObject2.GetComponent<Text>().font = Font.CreateDynamicFontFromOSFont(Font.GetOSInstalledFontNames()[0], 20);
                }
                text = text + "|" + gameObject.transform.name;
            }
            Debug.Log("-->" + text);
        }

        
            // 获取Button上绑定的脚本,以及调用的方法名字
            public static void NutDBG_getTheButtonScript(GameObject the_gameObject)
          {
            Button.ButtonClickedEvent onClick = the_gameObject.GetComponent<Button>().onClick;
            for (int i = 0; i < onClick.GetPersistentEventCount(); i++)
            {
                Debug.Log(string.Concat(new object[]
                {
                    "Ugui_getTheButtonScript-->",
                    onClick.GetPersistentTarget(i),
                    ".",
                    onClick.GetPersistentMethodName(i)
                }));
            }
        }

        // 获取GameObject上绑定的所有脚本/组件
        public static void NutDBG_getGameObjectScript(GameObject the_gameObject)
        {
            if(the_gameObject == null)
            {
                Debug.Log("Ugui_gm_getGameObjectScript-->fail-->no GameOBJ-->");
                return;
            }

            Component[] components = the_gameObject.transform.GetComponents(typeof(Component));
            for (int i = 0; i < components.Length; i++)
            {
                Debug.Log("Ugui_getGameObjectScript-->" + components[i]);
            }
        }


        //向游戏中加入自己的图片
        public GameObject NutDBG_create(string theName, Transform theParent, Vector3 theLocalPosition, string theUrl)
        {
            GameObject gameObject = new GameObject(theName);
            gameObject.transform.parent = theParent;
            gameObject.transform.localPosition = theLocalPosition;
            gameObject.AddComponent<RawImage>();
            this.MyMonoBehaviour.StartCoroutine(this.LoadTheImage(theUrl, gameObject));
            return gameObject;
        }


     
        // 加载指定路径的图片
        private IEnumerator LoadTheImage(string theUrl, GameObject theGameOBJ)
        {
            Debug.Log("传入的URL:" + theUrl);
            WWW www = new WWW("jar:file://" + theUrl);
            yield return www;
            if (string.IsNullOrEmpty(www.error))
            {
                Texture2D texture = www.texture;
                theGameOBJ.GetComponent<RawImage>().texture = www.texture;
                theGameOBJ.GetComponent<RawImage>().SetNativeSize();
            }
            yield break;
        }
     
            //向安卓层发消息  methodName为安卓层接收消息的方法名 
        public static void NutDBG_sendMsgToAndroid(string methodName, string msg = null)
        {
            Debug.Log("SendMsgToAndroid " + methodName + " and msg is " + msg);
            using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject [url=home.php?mod=space&uid=452844]@static[/url] = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    if (msg == null)
                    {
                        @static.Call(methodName, new object[0]);
                    }
                    else
                    {
                        @static.Call(methodName, new object[]
                        {
                            msg
                        });
                    }
                }
            }
        }
          
                  //NutDBG_sendMsgToAndroid() 这个方法的封装版本。。
                  //默认安卓层接收消息的方法名为u3dToJava_msg
                  //后面的两个string参数是为了自己方便输出的Log
             public static void NutDBG_SendMessageToJava(string msg, string CallBackObjName, string CallBackMethodName)
                {
                        try
                        {
                                if (msg != null)
                                {
                                        Nut.NutDBG_sendMsgToAndroid("u3dToJava_msg", msg);
                                        Debug.Log(string.Concat(new string[]
                                        {
                                                "SendMsg_msg ",
                                                msg,
                                                " Your CallBack Path is:(\"",
                                                CallBackObjName,
                                                "\",\"",
                                                CallBackMethodName,
                                                "\""
                                        }));
                                }
                                else
                                {
                                        Nut.NutDBG_sendMsgToAndroid("u3dToJava_exit", null);
                                        Debug.Log(string.Concat(new string[]
                                        {
                                                "SendMsg_exit ",
                                                msg,
                                                " Your CallBack Path is:(\"",
                                                CallBackObjName,
                                                "\",\"",
                                                CallBackMethodName,
                                                "\""
                                        }));
                                }
                        }
                        catch (Exception e)
                        {
                                if (e.ToString().Contains("UnityPlayerActivity"))
                                {
                                        Debug.Log(string.Concat(new string[]
                                        {
                                                "SendMsg_Fail  ",
                                                msg,
                                                "  Your CallBack Path is:(\"",
                                                CallBackObjName,
                                                "\",\"",
                                                CallBackMethodName,
                                                "\""
                                        }));
                                }
                                else
                                {
                                        Debug.Log(string.Concat(new object[]
                                        {
                                                "NutDBG_sendMsgToAndroid=",
                                                msg,
                                                "=",
                                                e
                                        }));
                                }
                        }
                }
                //寻找该物体下所有包含Text组件的子物体
        public static void NutDBG_GetTextFromChild(GameObject the_gameObject)      
        {
            int Count = 0;
            Transform[] AllObj = the_gameObject.GetComponentsInChildren<Transform>(true);
            foreach (Transform child in AllObj)
            {
                if (child.gameObject.GetComponent<Text>() != null)
                {
                    Debug.Log("Nut_Ugui__      --FatherName:" + the_gameObject.name + "          ChildName:" + child.name + "      Text:" + child.gameObject.GetComponent<Text>().text);
                    Count++;

                }
            }
            if (Count == 0)
            {
                Debug.Log("Nut_Ugui__GetTextFromChild  Sorry I Can't Find Text with this GameObj");
            }
            else
            {
                Debug.Log("Nut_Ugui__GetTextFromChild  Query completed, " + Count.ToString() + " locations found");
            }

        }
             
                //显示按钮的所有信息
                //此方法加在UnityEngine.UI.Press()方法下 可打印出当前被点击按钮的所有信息
             public static void NutDBG_ShowBtnInfo(GameObject Button)
                {
                 Debug.Log("==UGUI=button=="+Button.name+"==localPosition=="+Button.transform.localPosition+"==anchorMin=="+Button.GetComponent<RectTransform>().anchorMin+"==anchorMax=="+Button.GetComponent<RectTransform>().anchorMax);
         NutUnityDebug.Nut.NutDBG_getTheButtonScript(Button);
                 NutUnityDebug.Nut.NutDBG_getThePath(Button);
                 NutUnityDebug.Nut.NutDBG_getGameObjectScript(Button);
                }
                
        private MonoBehaviour MyMonoBehaviour;
    }
}

免费评分

参与人数 8吾爱币 +11 热心值 +7 收起 理由
冷淘 + 1 + 1 谢谢分享
q1082121 + 1 我很赞同!
苏紫方璇 + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
wwh1004 + 1 + 1 我也在赶比赛作品,唉
为海尔而战 + 1 + 1 我很赞同!
FleTime + 1 好东西!
lxhyjr + 1 + 1 谢谢@Thanks!
雪千渔 + 1 + 1 用心讨论,共获提升!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

leo传说 发表于 2019-9-13 23:59
加油,虽然看不懂
q1082121 发表于 2019-9-16 10:56
yuyu514567915 发表于 2019-9-16 10:57
gunxsword 发表于 2021-3-22 19:25
添加这个类会加,但是调用应该怎么调,能不能给个简短的代码学习一下!
千神奈奈子 发表于 2021-3-28 10:58
正好需要一个类似的东西,不知楼主能不能来点示例图?
0anye0 发表于 2021-8-20 10:01
真厉害,学习了!!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 14:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表