MemoryTracker

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MemoryTracker : MonoBehaviour {

    Vector2 scrollPositino = Vector2.zero;
    Rect windowRect = new Rect(20, 20, 200, 500);
    bool isClose = false;
    Vector2 backTopLeftPos = new Vector2(0, 20);
    Texture2D back;
    void Start()
    {
        back = (Texture2D)Resources.Load("Textures/logo", typeof(Texture2D));
    }
    void OnDiffGUI()
    {
        // active tran
        Transform[] activeTranList = FindObjectsOfType(typeof(Transform)) as Transform[];
        Dictionary<Transform, bool> activeDict = new Dictionary<Transform, bool>();

        for (int i = 0; i < activeTranList.Length; i++)
        {
            activeDict.Add(activeTranList[i], true);
        }

        // all tran
        Dictionary<Transform, bool> allDict = new Dictionary<Transform, bool>();
        Transform[] allTranList = Resources.FindObjectsOfTypeAll(typeof(Transform)) as Transform[];
        for (int i = 0; i < allTranList.Length; i++)
        {
            bool isActive = false;
            Transform t = allTranList[i];
            if (activeDict.ContainsKey(t)) isActive = true;
            allDict.Add(t, isActive);
        }


        // root obj
        List<GameObject> rootObjList = new List<GameObject>();
        foreach (Transform tran in allTranList)
        {
            if (tran.parent == null)
            {
                rootObjList.Add(tran.gameObject);
            }
        }

        scrollPositino = GUILayout.BeginScrollView(scrollPositino, GUILayout.Width(400));
        GUI.color = Color.white;
        foreach (GameObject go in rootObjList)
        {
            OnDiffGUIForSubGo(go, allDict, 0);
        }
        GUI.color = Color.white;

        GUILayout.EndScrollView();

        
    }

    void OnDiffGUIForSubGo(GameObject go, Dictionary<Transform, bool> allDict, int indentLevel)
    {
        Color guiCol = Color.red;

        string txt = "";
        for (int i = 0; i < indentLevel; i++) txt += "    ";

        txt += "+ " + go.name;

        if (allDict[go.transform])
        {
            guiCol = Color.green;
        }

        GUI.color = guiCol;
        
        if (indentLevel == 0)
        {
            GUIStyle style = new GUIStyle();
            style.fontSize = 20;
            style.normal.textColor = guiCol;
            GUILayout.Label(txt, style);
        }
        else
        {
            GUILayout.Label(txt);
        }
        
        

        foreach (Transform child in go.transform)
        {
            OnDiffGUIForSubGo(child.gameObject, allDict, indentLevel + 1);
        }
    }

    

    void OnGUI()
    {
        windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "My Window");
    }

    void DoMyWindow(int windowID)
    {
        if (isClose)
        {
            //GUI.DrawTexture(new Rect(backTopLeftPos, Vector2.one), back);
            isClose = GUILayout.Toggle(isClose, "开关");
            GUI.DragWindow();
        }
        else
        {
            GUI.DrawTexture(new Rect(backTopLeftPos, windowRect.size), back);
            isClose = GUILayout.Toggle(isClose, "开关");
            OnDiffGUI();
            GUI.DragWindow();
        }
        
        
        
    }

    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值