备份CatanBuilding瘦身独立工程
This commit is contained in:
84
Assets/Scripts/Services/HeartBeat.cs
Normal file
84
Assets/Scripts/Services/HeartBeat.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using asap.core;
|
||||
using tysdk;
|
||||
using UniRx;
|
||||
using UnityEngine;
|
||||
|
||||
public class HeartBeat
|
||||
{
|
||||
[Inject]
|
||||
public IStageService stageService {get; set;}
|
||||
|
||||
[Inject]
|
||||
public IConfig config {get; set;}
|
||||
|
||||
private IDisposable disposable;
|
||||
|
||||
private CancellationTokenSource tokenSource;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
//UnityEngine.Debug.Log("HeartBeat Init");
|
||||
disposable = GContext.OnEvent<StageLoadEvent>()
|
||||
.Where(e => e.state == EStageLoadState.Loaded && e.stageId == "FishingStage")
|
||||
.Subscribe(OnFishingStageLoad);
|
||||
Application.focusChanged += OnFocusChanged;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
disposable.Dispose();
|
||||
Application.focusChanged -= OnFocusChanged;
|
||||
if(tokenSource != null)
|
||||
{
|
||||
tokenSource.Cancel();
|
||||
tokenSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFocusChanged(bool focused)
|
||||
{
|
||||
if(focused)
|
||||
{
|
||||
if(tokenSource == null && stageService.GetStage("FishingStage") != null)
|
||||
{
|
||||
tokenSource = new CancellationTokenSource();
|
||||
StartHeartBeat(tokenSource.Token);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(tokenSource != null)
|
||||
{
|
||||
tokenSource.Cancel();
|
||||
tokenSource = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFishingStageLoad(StageLoadEvent @event)
|
||||
{
|
||||
disposable.Dispose();
|
||||
tokenSource = new CancellationTokenSource();
|
||||
StartHeartBeat(tokenSource.Token);
|
||||
//UnityEngine.Debug.Log("HeartBeat Start");
|
||||
}
|
||||
|
||||
private async void StartHeartBeat(CancellationToken token)
|
||||
{
|
||||
int interval =(int) config.GetFloat("HeartBeat", 60);
|
||||
try{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
using(var e = GEvent.TackEvent("HeartBeat", gaSend : false)){
|
||||
e.AddContent("FPS", game.RootCtx.fps);
|
||||
}
|
||||
await Task.Delay(interval * 1000);
|
||||
//UnityEngine.Debug.Log("HeartBeat");
|
||||
}
|
||||
}
|
||||
catch (System.Exception){}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user