FPS counter not working with Aspect ratio C#

Alright, so i have 2 scripts that contradict each other. When both are used, the fps counter would leave a trail of its previous number, making it overlap. Do note that these are public to use scripts that I have not made. Note that one does work without the other.

//Author: Adrian Lopez

// Use this for initialization
void Start () 
{
    // set the desired aspect ratio (the values in this example are
    // hard-coded for 16:9, but you could make them into public
    // variables instead so you can set them at design time)
    float targetaspect = 16.0f / 9.0f;

    // determine the game window's current aspect ratio
    float windowaspect = (float)Screen.width / (float)Screen.height;

    // current viewport height should be scaled by this amount
    float scaleheight = windowaspect / targetaspect;

    // obtain camera component so we can modify its viewport
    Camera camera = GetComponent<Camera>();

    // if scaled height is less than current height, add letterbox
    if (scaleheight < 1.0f)
    {  
        Rect rect = camera.rect;

        rect.width = 1.0f;
        rect.height = scaleheight;
        rect.x = 0;
        rect.y = (1.0f - scaleheight) / 2.0f;
        
        camera.rect = rect;
    }
    else // add pillarbox
    {
        float scalewidth = 1.0f / scaleheight;

        Rect rect = camera.rect;

        rect.width = scalewidth;
        rect.height = 1.0f;
        rect.x = (1.0f - scalewidth) / 2.0f;
        rect.y = 0;

        camera.rect = rect;
    }
}

//Author: Dave Hampson
//This is only used for testing.


using UnityEngine;
using System.Collections;

public class FPSCounter : MonoBehaviour {
	float deltaTime = 0.0f;

	void Update () {
		deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
	}

	void OnGUI()
	{
		int w = Screen.width, h = Screen.height;

		GUIStyle style = new GUIStyle();

		Rect rect = new Rect(0, 0, w, h * 2 / 100);
		style.alignment = TextAnchor.MiddleLeft;
		style.fontSize = h * 2 / 100;
		style.normal.textColor = new Color (1.0f, 1.0f, 1.0f, 1.0f);
		float msec = deltaTime * 1000.0f;
		float fps = 1.0f / deltaTime;
		string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
		GUI.Label(rect, text, style);
	}
}

Well, that’s the expected behaviour. What happens is that the first script uses a smaller camera rectangle if the desired aspect ratio doesn’t fit the screen aspect ratio. However that means the letterbox / pillar box areas are not drawn to by any camera. That means that area doesn’t get cleared either. So if you now draw “anything” in that area it will stick in the frame buffer forever (or until it gets partially overwritten by other stuff).

The easiest solution would be to add a second camera, set the culling mask to “nothing” (so the camera doesn’t draw anything) and make sure to set the “depth” value of that camera to a value lower than your normal camera so it’s rendered before the normal camera. That second camera will now clear the entire screen with the clear parameters you have specified. This should fix the problem.

Another way would be to simply not draw in those areas at all. So you would need to change the FPS script to not simply draw the FPS display at the top left (0, 0) but use the same logic as in the other script to calculate the y or x offset so the display is within the camera rect. Of course you could simply reference the camera in the FPS script and let it use the pixelRect of the camera to get the offset right away.

It depends on where you want the FPS display to be.