1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UniRx;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- namespace KairoEngine.Multiplayer
- {
- [HideMonoScript]
- public class NetworkTick : MonoBehaviour
- {
- [LabelText("Current Tick"), ShowInInspector, ReadOnly, PropertyOrder(0)] public int tick { get; private set; }
- [LabelText("Refresh rate"), SuffixLabel("ms", true), PropertyOrder(1)] public int rate = 60;
- [PropertyOrder(2)] public string eventStreamName = "EventStream";
- [PropertyOrder(3)] public bool fixedCurrentTick = false;
- [PropertyOrder(4)] public bool autoStart = false;
- private bool running = false;
- private void Start()
- {
- if(autoStart) StartTickSystem();
- }
- private void OnDisable() => StopTickSystem();
- public void StartTickSystem()
- {
- running = true;
- tick = 0;
- UpdateTick();
- }
- public void StopTickSystem()
- {
- running = false;
- }
- private void UpdateTick()
- {
- if(!running) return;
- Timer.ExecuteRealTime(rate, () => {
- tick += 1;
- GenericEvents.Trigger($"{eventStreamName}_OnTick", tick);
- UpdateTick();
- });
- }
- public void ChangeCurrentTick(int newCurrentTick)
- {
- if(fixedCurrentTick) return;
- tick = newCurrentTick;
- }
- }
- }
|