NxrFPS.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. using UnityEngine;
  14. //Frame rate printing.Just mount on any object.
  15. namespace Nxr.Internal
  16. {
  17. public class NxrFPS : MonoBehaviour
  18. {
  19. private string fpsFormat;
  20. private float updateInterval = 0.2f;//设定更新帧率的时间间隔为0.2秒
  21. private float accum = .0f;
  22. private int frames = 0;
  23. private float timeLeft;
  24. public static float fpsDeltaTime;
  25. TextMesh textMesh;
  26. // Use this for initialization
  27. void Start()
  28. {
  29. textMesh = GetComponent<TextMesh>();
  30. }
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. calculate_fps();
  35. fpsDeltaTime += Time.deltaTime;
  36. if (fpsDeltaTime > 1)
  37. {
  38. //Debug.Log(fpsFormat);
  39. fpsDeltaTime = 0;
  40. if (textMesh != null)
  41. {
  42. textMesh.text = fpsFormat;
  43. }
  44. }
  45. }
  46. private void calculate_fps()
  47. {
  48. timeLeft -= Time.deltaTime;
  49. accum += Time.timeScale / Time.deltaTime;
  50. ++frames;
  51. if (timeLeft <= 0)
  52. {
  53. float fps = accum / frames;
  54. fpsFormat = System.String.Format("{0:F3}fps", fps);
  55. // Debug.Log("FPS:" + fpsFormat);
  56. timeLeft = updateInterval;
  57. accum = .0f;
  58. frames = 0;
  59. }
  60. }
  61. }
  62. }