hch
63 分钟以前 5c61793c3404cd912c8357eef5fef70bae5bd3d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2015 hugula
// direct https://github.com/tenvick/hugula
//
using UnityEngine;
using System.Collections;
 
public class ProfilerPanel : MonoBehaviour {
    const float m_KBSize = 1024.0f * 1024.0f;
    private string memory, framerate;
 
    private float updateInterval = 0.2F;
    private float accum = 0; // FPS accumulated over the interval
    private int frames = 0; // Frames drawn over the interval
    private float timeleft; // Left time for current interval
 
    void Start() {
        timeleft = updateInterval;
        //memory = Util.Get<UILabel>(gameObject, "MemoryInfo");
        //framerate = Util.Get<UILabel>(gameObject, "FrameRate");
    }
 
    void Update() {
        UpdateGameInfo();
    }
 
    void OnGUI() {
        GUILayout.Label("fps:" + framerate);
        GUILayout.Label("" + memory);
 
    }
    /// <summary>
    /// 更新游戏信息
    /// </summary>
    void UpdateGameInfo() {
        float totalMemory = (float)(UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / m_KBSize);
        float totalReservedMemory = (float)(UnityEngine.Profiling.Profiler.GetTotalReservedMemoryLong() / m_KBSize);
        float totalUnusedReservedMemory = (float)(UnityEngine.Profiling.Profiler.GetTotalUnusedReservedMemoryLong() / m_KBSize);
        float monoHeapSize = (float)(UnityEngine.Profiling.Profiler.GetMonoHeapSizeLong() / m_KBSize);
        float monoUsedSize = (float)(UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong() / m_KBSize);
 
        timeleft -= Time.deltaTime;
        accum += Time.timeScale / Time.deltaTime;
        ++frames;
 
        // Interval ended - update GUI text and start new interval
        if (timeleft <= 0.0) {
            // display two fractional digits (f2 format)
            float fps = accum / frames;
            framerate = fps.ToString();// ioo.f("fps:{0:F2}", fps);
 
            //if (fps < 30) {
            //    framerate.color = Color.yellow;
            //} else {
            //    if (fps < 10) {
            //        framerate.color = Color.red;
            //    } else {
            //        framerate.color = Color.green;
            //    }
            //}
            timeleft = updateInterval;
            accum = 0.0F;
            frames = 0;
        }
 
        memory = string.Format("TotalAllocatedMemory:{0}MB\n" + //TotalAllocatedMemory
                               "TotalReservedMemory:{1}MB\n" +
                               "TotalUnusedReservedMemory:{2}MB\n" +
                               "MonoHeapSize:{3}MB\nMonoUsedSize:{4}MB",
                               totalMemory,
                               totalReservedMemory,
                               totalUnusedReservedMemory,
                               monoHeapSize,
                               monoUsedSize);
    }
}