private void DrawToolBar() { // // 1. 自适应窗口 // int pos = 0; int space = 5; int width = (int)(Screen.width * 0.1f); width = Mathf.Max(width, 75); width = Mathf.Min(width, 100); pos = (Screen.width - width * 3 - space * 2) / 2; // // 2. 分析按钮 // bool bProfiling = GUI.Toggle(new Rect(pos, 5, width, 20), mbProfiling, "Profiler", "button"); if (mbProfiling != bProfiling) { if (bProfiling) { mDeltaTime = 0.0f; mbProfiling = true; MonoProfilerHelper.Resume(); } else { mDeltaTime = 0.0f; mbProfiling = false; MonoProfilerHelper.Pause(); } } // // 3. 清除数据 // if (GUI.Button(new Rect(pos + width + space, 5, width, 20), "Clear")) { mMethodAllocations.Clear(); mCurrentMethodCallStack = null; mCurrentMethodAlloction = null; MonoProfilerHelper.Clear(); } // // 4. 输出到文件 // if (GUI.Button(new Rect(pos + 2 * (width + space), 5, width, 20), "Dump")) { string path = EditorUtility.SaveFilePanel("Dump mono memory", "", "mono", "txt"); if (path != null) { MonoProfilerHelper.Dump(path); } } }
private void Update() { if (mbProfiling == false) { return; } mDeltaTime += Time.deltaTime; if (mDeltaTime > 1.0f) { // // 1. 清除当前数据 // mMethodAllocations.Clear(); mCurrentMethodCallStack = null; mCurrentMethodAlloction = null; // // 2. 重新获得数据 // MonoProfilerHelper.Pause(); MonoProfilerHelper.BeginMethodIterator(); { do { if (Filter(MonoProfilerHelper.GetMethodCallStackString())) { MethodAlloction method = new MethodAlloction(); { method.name = MonoProfilerHelper.GetMethodString(); method.stack = MonoProfilerHelper.GetMethodCallStackString(); method.allocated = MonoProfilerHelper.GetMethodAllocSize(); method.allocatedDelta = MonoProfilerHelper.GetMethodAllocSizeDelta(); MonoProfilerHelper.BeginObjectIterator(); { do { ObjectAlloction obj = new ObjectAlloction(); { obj.name = MonoProfilerHelper.GetObjectString(); obj.allocated = MonoProfilerHelper.GetObjectAllocSize(); } method.objects.Add(obj); } while (MonoProfilerHelper.NextObjectIterator()); } MonoProfilerHelper.EndObjectIterator(); } mMethodAllocations.Add(method); } } while (MonoProfilerHelper.NextMethodIterator()); } MonoProfilerHelper.EndMethodIterator(); MonoProfilerHelper.Resume(); // // 3. 排序 // if (mMethodAllocations.Count > 0) { mMethodAllocations.Sort(CompareMethod); mCurrentMethodCallStack = mMethodAllocations[0].stack; mCurrentMethodAlloction = mMethodAllocations[0]; } // // 4. 重绘 // Repaint(); // // 5. 重新计数 // mDeltaTime = 0.0f; } }