chenxin
2020-12-08 87fcdf345e1ee63161f0ca9378e837d05a8c0d18
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Profiling;
using UnityEngine.UI;
 
public class FPSDisplay : MonoBehaviour
{
    /// <summary>
    /// 用于显示FPS数据的Text
    /// </summary>
    public Text fpsText;
 
    // 帧率计算频率
    private const float calcRate = 0.5f;
    // 本次计算频率下帧数
    private int frameCount = 0;
    // 频率时长
    private float rateDuration = 0f;
    // 显示帧率
    private int fps = 0;
 
    protected bool fpsDisplay = false;
 
    void Start()
    {
        this.frameCount = 0;
        this.rateDuration = 0f;
        this.fps = 0;
    }
 
    void Update()
    {
        ++this.frameCount;
        this.rateDuration += Time.deltaTime;
        if (this.rateDuration > calcRate)
        {
            // 计算帧率
            this.fps = (int)(this.frameCount / this.rateDuration);
            this.frameCount = 0;
            this.rateDuration = 0f;
        }
 
        if (UnityEngine.Input.GetKeyDown(KeyCode.P))
            fpsDisplay = !fpsDisplay;
    }
 
    void OnGUI()
    {
        GUIStyle bb = new GUIStyle();
        bb.normal.background = null; //这是设置背景填充的
        //bb.normal.textColor = new Color(1, 1, 1,1);   //设置字体颜色的
        bb.fontSize = 30; //当然,这是字体颜色
 
        if (fpsDisplay)
            bb.normal.textColor = new Color(1, 1, 1, 1);   //设置字体颜色的
        else
            bb.normal.textColor = new Color(1, 1, 1, 0);
 
        string disStr = "";
        if (fps > 0)
            disStr += ("FPS:" + this.fps.ToString() + "\n");
 
        if (fpsText)
            fpsText.text = disStr;
        else
            GUI.Label(new Rect(20, 20, 300, 200), disStr,bb);
    }
 
    float ByteToM(long byteCount)
    {
        return (float)(byteCount / (1024.0f * 1024.0f));
    }
}