unity如何計算幀率FPS

unity如何計算幀率FPS

在使用unity開發過程中,許多時候需要顯示當前項目的幀率FPS,用於觀察項目的流程度,那麼如何計算FPS呢?請看下面代碼演示:

public class FPSShow:MonoBehaviour

{

public Text[] ShowText;

private float accum;

private int frames;

void Start()

{

StartCoroutine(FPS());

}

void Update()

{

accum += Time.timeScale / Time.deltaTime;

++frames;

}

IEnumerator FPS()

{

// Infinite loop executed every "frenquency" secondes.

while (true)

{

// Update the FPS

float fps = accum / frames;

foreach (var ttt in ShowText)

{

ttt.text = fps.ToString("F1");

}

accum = 0.0F;

frames = 0;

yield return new WaitForSeconds(0.5f);

}

}

}


分享到:


相關文章: