123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- namespace YLBattle
- {
- /// <summary>
- /// 显示 fps
- /// </summary>
- public class ShowFPS : MonoBehaviour
- {
- /// <summary>
- /// 更新间隔时间,单位:秒
- /// </summary>
- private float mInterval = 0.5f;
- /// <summary>
- /// 上一次计算 fps 的时间
- /// </summary>
- private float mLastTime = 0.0f;
- /// <summary>
- /// 帧数计数器
- /// </summary>
- private int mFrameNum = 0;
- /// <summary>
- /// 当前 fps
- /// </summary>
- private float mCurFPS = 0.0f;
- /// <summary>
- /// 当前ping/pong循环时间
- /// </summary>
- private int curTTs = 0;
- /// <summary>
- /// gui style
- /// </summary>
- private GUIStyle myStyle = new GUIStyle();
- private GUIStyle ttsStyle = new GUIStyle();
- /// <summary>
- /// 初始化
- /// </summary>
- private void Start()
- {
- mLastTime = Time.realtimeSinceStartup;
- mFrameNum = 0;
- myStyle.fontSize = 40;
- myStyle.normal.textColor = Color.yellow;
- ttsStyle.fontSize = 24;
- ttsStyle.normal.textColor = Color.green;
- }
- /// <summary>
- /// 更新
- /// </summary>
- 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
- };
- }
- /// <summary>
- /// 显示 fps
- /// </summary>
- 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);
- }
- }
- }
|