Exemplo n.º 1
0
        void OnGUI()
        {
            if (_showFPS)
            {
                float msec = fpsDeltaTime * 1000.0f;
                float fps  = 1.0f / fpsDeltaTime;

                // Show FPS counter
                GUILayout.BeginArea(GUIUtils.GetCornerRect(ScreenCorner.BottomRight, 100, 25, new Vector2(15 + fpsTexture.width, 10)));
                GUILayout.Label(string.Format("{0:0.}fps ({1:0.0}ms)", fps, msec), new GUIStyle("label")
                {
                    alignment = TextAnchor.MiddleLeft
                });
                GUILayout.EndArea();

                if (fpsTexture == null)
                {
                    return;
                }

                // Show FPS history
                Color[] colors = new Color[fpsTexture.width * fpsTexture.height];

                for (int i = 0; i < (fpsTexture.width * fpsTexture.height); i++)
                {
                    colors[i] = new Color(0.0f, 0.0f, 0.0f, 0.66f);                     // Half-transparent background for FPS graph
                }
                fpsTexture.SetPixels(colors);

                // Append to history storage
                fpsHistory[fpsIndex] = fps;

                int f = fpsIndex;

                if (fps > fpsHistory.Average())
                {
                    fpsMaximum = fps;
                }

                // Draw graph into texture
                for (int i = fpsTexture.width - 1; i >= 0; i--)
                {
                    float graphVal = (fpsHistory[f] > fpsMaximum) ? fpsMaximum : fpsHistory[f];                 //Clamps
                    int   height   = (int)(graphVal * fpsTexture.height / (fpsMaximum + 0.1f));                 //Returns the height of the desired point with a padding of 0.1f units

                    float p = fpsHistory[f] / fpsMaximum,
                          r = Mathf.Lerp(1, 1 - p, p),
                          g = Mathf.Lerp(p * 2, p, p);

                    fpsTexture.SetPixel(i, height, new Color(r, g, 0));
                    f--;

                    if (f < 0)
                    {
                        f = fpsHistory.Length - 1;
                    }
                }

                // Next entry in rolling history buffer
                fpsIndex++;
                if (fpsIndex >= fpsHistory.Length)
                {
                    fpsIndex = 0;
                }

                // Draw texture on GUI
                fpsTexture.Apply(false, false);
                GUI.DrawTexture(GUIUtils.GetCornerRect(ScreenCorner.BottomRight, fpsTexture.width, fpsTexture.height, new Vector2(5, fpsTexture.height - 15)), fpsTexture);
            }
        }