unity3d实现Game窗口的Stats

简介: 示例.pngusing UnityEngine;using System.Text;using UnityEditor;public class ShowFps : MonoBehaviour{ public bool...
img_affa41a4a2742188c99acddc6a6e67ce.png
示例.png
using UnityEngine;
using System.Text;
using UnityEditor;

public class ShowFps : MonoBehaviour
{

    public bool m_isShow = true;

    private int m_FrameCounter;
    private float m_ClientTimeAccumulator;
    private float m_RenderTimeAccumulator;
    private float m_MaxTimeAccumulator;
    private float m_ClientFrameTime;
    private float m_RenderFrameTime;
    private float m_MaxFrameTime;
    private GUIStyle s_SectionHeaderStyle;
    private GUIStyle s_LabelStyle;
    [Range(0, 1)]
    public float position = 0.5f;
    void Start()
    {
    }



    void OnGUI()
    {
        if (m_isShow)
        {
            GameViewStatsGUI();
        }

    }

    private GUIStyle sectionHeaderStyle
    {
        get
        {
            if (s_SectionHeaderStyle == null)
            {
                s_SectionHeaderStyle = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene).GetStyle("BoldLabel");
            }
            return s_SectionHeaderStyle;
        }
    }
    private GUIStyle labelStyle
    {
        get
        {
            if (s_LabelStyle == null)
            {
                s_LabelStyle = new GUIStyle(EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene).label);
                s_LabelStyle.richText = true;
            }
            return s_LabelStyle;
        }
    }
    private string FormatNumber(int num)
    {
        if (num < 1000)
        {
            return num.ToString();
        }
        if (num < 1000000)
        {
            return ((double)num * 0.001).ToString("f1") + "k";
        }
        return ((double)num * 1E-06).ToString("f1") + "M";
    }
    public void UpdateFrameTime()
    {
        float frameTime = UnityStats.frameTime;
        float renderTime = UnityStats.renderTime;
        m_ClientTimeAccumulator += frameTime;
        m_RenderTimeAccumulator += renderTime;
        m_MaxTimeAccumulator += Mathf.Max(frameTime, renderTime);
        m_FrameCounter++;
        bool flag = m_ClientFrameTime == 0f && m_RenderFrameTime == 0f;
        bool flag2 = m_FrameCounter > 30 || m_ClientTimeAccumulator > 0.3f || m_RenderTimeAccumulator > 0.3f;
        if (flag || flag2)
        {
            m_ClientFrameTime = m_ClientTimeAccumulator / (float)m_FrameCounter;
            m_RenderFrameTime = m_RenderTimeAccumulator / (float)m_FrameCounter;
            m_MaxFrameTime = m_MaxTimeAccumulator / (float)m_FrameCounter;
        }
        if (flag2)
        {
            m_ClientTimeAccumulator = 0f;
            m_RenderTimeAccumulator = 0f;
            m_MaxTimeAccumulator = 0f;
            m_FrameCounter = 0;
        }
    }
    private static string FormatDb(float vol)
    {
        if (vol == 0f)
        {
            return "-∞ dB";
        }
        return string.Format("{0:F1} dB", 20f * Mathf.Log10(vol));
    }
    public void GameViewStatsGUI()
    {
        GUI.skin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene);
        GUI.color = new Color(1f, 1f, 1f, 0.75f);
        float num = 300f;
        float num2 = 204f;
        int num3 = Network.connections.Length;
        if (num3 != 0)
        {
            num2 += 220f;
        }
        GUILayout.BeginArea(new Rect(Screen.width * position - num - 10f, 27f, num, num2), "Statistics", GUI.skin.window);
        GUILayout.Label("Audio:", sectionHeaderStyle, new GUILayoutOption[0]);
        StringBuilder stringBuilder = new StringBuilder(400);
        float audioLevel = UnityStats.audioLevel;
        stringBuilder.Append("  Level: " + FormatDb(audioLevel) + ((!EditorUtility.audioMasterMute) ? "\n" : " (MUTED)\n"));
        stringBuilder.Append(string.Format("  Clipping: {0:F1}%", 100f * UnityStats.audioClippingAmount));
        GUILayout.Label(stringBuilder.ToString(), new GUILayoutOption[0]);
        GUI.Label(new Rect(170f, 40f, 120f, 20f), string.Format("DSP load: {0:F1}%", 100f * UnityStats.audioDSPLoad));
        GUI.Label(new Rect(170f, 53f, 120f, 20f), string.Format("Stream load: {0:F1}%", 100f * UnityStats.audioStreamLoad));
        GUILayout.Label("Graphics:", sectionHeaderStyle, new GUILayoutOption[0]);
        UpdateFrameTime();
        string text = string.Format("{0:F1} FPS ({1:F1}ms)", 1f / Mathf.Max(m_MaxFrameTime, 1E-05f), m_MaxFrameTime * 1000f);
        GUI.Label(new Rect(170f, 75f, 120f, 20f), text);
        int screenBytes = UnityStats.screenBytes;
        int num4 = UnityStats.dynamicBatchedDrawCalls - UnityStats.dynamicBatches;
        int num5 = UnityStats.staticBatchedDrawCalls - UnityStats.staticBatches;
        StringBuilder stringBuilder2 = new StringBuilder(400);
        if (m_ClientFrameTime > m_RenderFrameTime)
        {
            stringBuilder2.Append(string.Format("  CPU: main <b>{0:F1}</b>ms  render thread {1:F1}ms\n", m_ClientFrameTime * 1000f, m_RenderFrameTime * 1000f));
        }
        else
        {
            stringBuilder2.Append(string.Format("  CPU: main {0:F1}ms  render thread <b>{1:F1}</b>ms\n", m_ClientFrameTime * 1000f, m_RenderFrameTime * 1000f));
        }
        stringBuilder2.Append(string.Format("  Batches: <b>{0}</b> \tSaved by batching: {1}\n", UnityStats.batches, num4 + num5));
        stringBuilder2.Append(string.Format("  Tris: {0} \tVerts: {1} \n", FormatNumber(UnityStats.triangles), FormatNumber(UnityStats.vertices)));
        stringBuilder2.Append(string.Format("  Screen: {0} - {1}\n", UnityStats.screenRes, EditorUtility.FormatBytes(screenBytes)));
        stringBuilder2.Append(string.Format("  SetPass calls: {0} \tShadow casters: {1} \n", UnityStats.setPassCalls, UnityStats.shadowCasters));
        stringBuilder2.Append(string.Format("  Visible skinned meshes: {0}  Animations: {1}", UnityStats.visibleSkinnedMeshes, UnityStats.visibleAnimations));
        GUILayout.Label(stringBuilder2.ToString(), labelStyle, new GUILayoutOption[0]);
        if (num3 != 0)
        {
            GUILayout.Label("Network:", sectionHeaderStyle, new GUILayoutOption[0]);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            for (int i = 0; i < num3; i++)
            {
                GUILayout.Label(UnityStats.GetNetworkStats(i), new GUILayoutOption[0]);
            }
            GUILayout.EndHorizontal();
        }
        else
        {
            GUILayout.Label("Network: (no players connected)", sectionHeaderStyle, new GUILayoutOption[0]);
        }
        GUILayout.EndArea();
    }
}

img_ee7574b7c2c8b7b83c7b3800083595e6.png
示例.png

显示FPS与内存

using UnityEngine;
using System.Collections;
using System.Text;
using UnityEditor;

/// <summary>
/// 帧率计算器
/// </summary>
public class FPSCounterManager : MonoBehaviour
{
    // 帧率计算频率
    private const float calcRate = 0.5f;
    // 本次计算频率下帧数
    private int frameCount = 0;
    // 频率时长
    private float rateDuration = 0f;
    // 显示帧率
    private int fps = 0;


#if UNITY_EDITOR
    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;
        }
    }

    void OnGUI()
    {
        GUI.color = Color.white;
        GUILayout.TextField("总内存:" + ByteToM(Profiler.GetTotalAllocatedMemory()) + "M");
        GUILayout.TextField("堆内存:" + ByteToM(Profiler.GetMonoUsedSize()) + "M");
        GUILayout.TextField("FPS:" + this.fps.ToString());
    }

    float ByteToM(uint byteCount)
    {
        return (float)(byteCount / (1024.0f * 1024.0f));
    }
#endif

}

相关文章
|
编解码 图形学
|
6月前
|
JavaScript 前端开发 C#
初识Unity——创建代码、场景以及五个常用面板(创建C#代码、打开代码文件、场景的创建、Project、Hierarchy、Inspector、Scene、Game )
初识Unity——创建代码、场景以及五个常用面板(创建C#代码、打开代码文件、场景的创建、Project、Hierarchy、Inspector、Scene、Game )
410 0
|
编解码 API 图形学
|
API 图形学
【unity细节】unity中实例化的物体在Game视图无法显示
【unity细节】unity中实例化的物体在Game视图无法显示
377 0
|
编解码 监控 图形学
Unity 窗口界面的简单介绍
Unity 窗口界面的简单介绍
501 0
Unity 窗口界面的简单介绍
|
图形学
Unity3D初识---窗口和菜单基础介绍
目录 目录 窗口界面介绍: scene: Game : Hierarchy: Project: Inspector: 其他窗口: 菜单界面 File文件菜单 Edit编辑菜单 窗口界面介绍: scene: 在场景编译窗口中可以鼠标右键选中之后移动鼠标就可以自由的浏览场景,并选择物体对其可以进行操作,按下Q\W\E\R四个键时可以画面中的将会进行变换,第一个图标可以进行拖动、第二个图标可以对物体进行位移,第三个图标可以对物体进行旋转,第四个图标可以对物体进行放大缩小。
1302 0
|
Go 图形学
unity Hierarchy窗口对象的显示和隐藏与锁定
setting.png 将脚本QuickToggle放置到Editor目标下 /* Copyright 2017, Jeiel Aranal Permission is hereby granted, free of charge, t...
2603 0
|
测试技术 图形学
Unity用GUI绘制Debug/print窗口/控制台-打包后测试
Unity游戏视窗控制台输出 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar —— 心...
2093 0
|
图形学 Windows
unity 实现调用Windows窗口/对话框交互
Unity调用Window窗口 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar ——...
1855 0
|
Web App开发 图形学 Windows
Unity 3D调用Windows打开、保存窗口、文件浏览器
Unity调用Window窗口 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar ——...
3335 0