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();
}
}
}
MemoryTracker
最新推荐文章于 2024-06-14 14:28:22 发布