From a8bf2e042d24e14d0e42ac6bf4e7b201c21f83e6 Mon Sep 17 00:00:00 2001 From: CreatureSurvive Date: Fri, 20 Sep 2019 13:51:39 -0500 Subject: [PATCH] Taking out the garbage - Significantly reduced garbage allocation produced - Added display string buffer for caching all possible display values eg. the FPS label - Added a global YieldInstruction for reducing garbage produced by the coroutine --- .../FPS Counter/FPSCounter.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Assets/Unity FPS counter with buffer/FPS Counter/FPSCounter.cs b/Assets/Unity FPS counter with buffer/FPS Counter/FPSCounter.cs index 0eaff10..caedaa0 100644 --- a/Assets/Unity FPS counter with buffer/FPS Counter/FPSCounter.cs +++ b/Assets/Unity FPS counter with buffer/FPS Counter/FPSCounter.cs @@ -36,6 +36,8 @@ public class FPSCounter : MonoBehaviour static Transform stTr; static GameObject[] stLines; static int stNumLines; + static string[] displayBuffer; + static WaitForSeconds graphWait; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -59,6 +61,13 @@ void Awake() img.color = graphColor; } + graphWait = new WaitForSeconds(graphUpdate); + displayBuffer = new string[highestPossibleFPS + 1]; + for (int i = 0; i <= highestPossibleFPS; i++) + { + displayBuffer[i] = "FPS: " + i.ToString(); + } + StartCoroutine(DrawGraph()); //--------------------------------- @@ -73,7 +82,7 @@ void Update() //--------------------------------- curCount = StFPS.Counter(frameUpdate, Time.deltaTime); - counterText.text = "FPS: " + curCount.ToString(); + counterText.text = displayBuffer[curCount]; //--------------------------------- } @@ -88,7 +97,7 @@ IEnumerator DrawGraph() while (true) { - yield return new WaitForSeconds(graphUpdate); + yield return graphWait; GameObject obj = GiveLine(); Image img = obj.GetComponent();