Latency.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { _decorator, Color, Component, Label, Node, UIRenderer } from 'cc';
  2. import { gameNet } from './NetGameServer';
  3. const { ccclass, property } = _decorator;
  4. const COLORS = [
  5. new Color(0,255,0),
  6. new Color(255,255,0),
  7. new Color(255,0,0),
  8. ]
  9. @ccclass('Latency')
  10. export class Latency extends Component {
  11. @property(Label) lblLatency:Label;
  12. start() {
  13. }
  14. private _lastLatency = -1;
  15. update(deltaTime: number) {
  16. if(this._lastLatency != gameNet.latency){
  17. this._lastLatency = gameNet.latency;
  18. this.lblLatency.string = `${this._lastLatency}ms`;
  19. let colorIndex = 0;
  20. if(this._lastLatency <= 200){
  21. colorIndex = 0;
  22. }
  23. else if(this._lastLatency <= 500){
  24. colorIndex = 1;
  25. }
  26. else{
  27. colorIndex = 2;
  28. }
  29. let color = COLORS[colorIndex];
  30. this.node.children.forEach(child => {
  31. let uirenerer = child.getComponent(UIRenderer);
  32. uirenerer.color = color;
  33. });
  34. }
  35. }
  36. }