Ejemplo n.º 1
0
    public void TickUpdate()
    {
        if (m_ShowStats < 1)
        {
            return;
        }

        long  ticks           = m_StopWatch.ElapsedTicks;
        float frameDurationMs = (ticks - m_LastFrameTicks) * 1000 / (float)m_StopWatchFreq;

        m_LastFrameTicks = ticks;

        DebugOverlay.SetColor(Color.yellow);
        DebugOverlay.SetOrigin(0, 0);

        DebugOverlay.Write(1, 0, "FPS:{0,6:###.##}", 1.0f / Time.deltaTime);
        fpsHistory[Time.frameCount % fpsHistory.Length] = 1.0f / Time.deltaTime;
        DebugOverlay.DrawGraph(1, 1, 9, 1.5f, fpsHistory, Time.frameCount % fpsHistory.Length, Color.green);

        DebugOverlay.Write(30, 0, "Open console (F12) and type: \"showstats\" to toggle graphs");

        if (m_ShowStats < 2)
        {
            return;
        }

        DebugOverlay.Write(0, 4, "Hello, {0,-5} world!", Time.frameCount % 100 < 50 ? "Happy" : "Evil");
        DebugOverlay.Write(0, 5, "FrameNo: {0,7}", Time.frameCount);
        DebugOverlay.Write(0, 6, "MonoHeap:{0,7} kb", (int)(UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong() / 1024));

        /// Graphing difference between deltaTime and actual passed time
        float fps = Time.deltaTime * 1000.0f;
        var   idx = Time.frameCount % fpsArray[0].Length;;

        fpsArray[0][idx] = -Mathf.Min(0, frameDurationMs - fps);
        fpsArray[1][idx] = Mathf.Max(0, frameDurationMs - fps);
        float variance, mean, min, max;

        CalcStatistics(fpsArray[0], out mean, out variance, out min, out max);

        // Draw histogram over time differences
        DebugOverlay.DrawHist(20, 10, 20, 3, fpsArray, Time.frameCount, colors, max);
        DebugOverlay.SetColor(new Color(1.0f, 0.3f, 0.0f));
        DebugOverlay.Write(20, 14, "{0,4:#.###} ({1,4:##.#} +/- {2,4:#.##})", frameDurationMs - fps, mean, Mathf.Sqrt(variance));

        DebugOverlay.DrawGraph(45, 10, 40, 3, fpsArray, Time.frameCount, colors, max);

        /// Graphing frametime
        var idx2 = Time.frameCount % frameTimeArray.Length;

        frameTimeArray[idx2] = frameDurationMs;
        CalcStatistics(frameTimeArray, out mean, out variance, out min, out max);
        DebugOverlay.DrawHist(20, 15, 20, 3, frameTimeArray, Time.frameCount, Color.red, max);

        // Draw legend
        float scale = 18.0f - 3.0f / max * 16.6667f;

        DebugOverlay.DrawLine(20, scale, 40, scale, Color.black);
        DebugOverlay.Write(20, 18, "{0,5} ({1} +/- {2})", frameDurationMs, mean, Mathf.Sqrt(variance));

        DebugOverlay.DrawGraph(45, 15, 40, 3, frameTimeArray, Time.frameCount, Color.red, max);

        // Draw some lines to help visualize framerate fluctuations
        float ratio = (float)DebugOverlay.Height / DebugOverlay.Width * Screen.width / Screen.height;
        float time  = (float)Time.frameCount / 60.0f;

        for (var i = 0; i < 10; i++)
        {
            DebugOverlay.DrawLine(60, 20, 60 + Mathf.Sin(Mathf.PI * 0.2f * i + time) * 8.0f, 20 + Mathf.Cos(Mathf.PI * 0.2f * i + time) * 8.0f * ratio, Color.black);
        }
    }