using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace YLBattle
{
///
/// 显示 fps
///
public class ShowFPS : MonoBehaviour
{
///
/// 更新间隔时间,单位:秒
///
private float mInterval = 0.5f;
///
/// 上一次计算 fps 的时间
///
private float mLastTime = 0.0f;
///
/// 帧数计数器
///
private int mFrameNum = 0;
///
/// 当前 fps
///
private float mCurFPS = 0.0f;
///
/// 当前ping/pong循环时间
///
private int curTTs = 0;
///
/// gui style
///
private GUIStyle myStyle = new GUIStyle();
private GUIStyle ttsStyle = new GUIStyle();
///
/// 初始化
///
private void Start()
{
mLastTime = Time.realtimeSinceStartup;
mFrameNum = 0;
myStyle.fontSize = 40;
myStyle.normal.textColor = Color.yellow;
ttsStyle.fontSize = 24;
ttsStyle.normal.textColor = Color.green;
}
///
/// 更新
///
private void Update()
{
mFrameNum++;
float curTime = Time.realtimeSinceStartup;
if ((mLastTime + mInterval) < curTime)
{
mCurFPS = (float)mFrameNum / (curTime - mLastTime);
mFrameNum = 0;
mLastTime = curTime;
}
curTTs = MapTcpProxy.Instance?.Peer.tts ?? -1;
ttsStyle.normal.textColor = curTTs switch
{
> 100 => Color.red,
> 50 => Color.yellow,
_ => Color.green
};
}
///
/// 显示 fps
///
private void OnGUI()
{
GUI.Label(new Rect(0, 0, 50, 50), string.Format("FPS: {0:f2}", mCurFPS), myStyle);
GUI.Label(new Rect(800, 0, 50, 50), $" rtts:{curTTs}ms", ttsStyle);
}
}
}