添加线程逻辑执行

This commit is contained in:
PC-20230316NUNE\Administrator
2024-09-12 18:06:22 +08:00
parent 77b305be7a
commit bf7b5b1160
13 changed files with 4818 additions and 49 deletions

View File

@@ -72,7 +72,7 @@ namespace JNGame.Sync.Frame
#endif
if (!IsStart) return;
base.Execute();
int deltaTime = (int)(Time.deltaTime * 1000f);
int deltaTime = TickTime;
dtTotal += deltaTime;
dtInputTotal += deltaTime;

View File

@@ -35,7 +35,7 @@ namespace JNGame.Sync.State
#endif
if (!IsStart) return;
base.Execute();
dtTime += (int)(Time.deltaTime * 1000);
dtTime += TickTime;
if (DeltaTime <= dtTime)
{
dtTime -= DeltaTime;

View File

@@ -37,7 +37,7 @@ namespace JNGame.Sync.State
#endif
if (!IsStart) return;
base.Execute();
dtTime += (int)(Time.deltaTime * 1000);
dtTime += TickTime;
if (DeltaTime <= dtTime)
{
dtTime -= DeltaTime;

View File

@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Entitas;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity;
using JNGame.Sync.Frame.Service;
using JNGame.Sync.System;
using JNGame.Sync.System.View;
using JNGame.Util;
using UnityEngine;
namespace JNGame.Sync.Frame
@@ -20,6 +23,11 @@ namespace JNGame.Sync.Frame
public JNContexts Contexts;
public abstract bool IsStartGame { get; }
private Thread thread;
//执行时间
protected int TickTime;
public JNSyncDefaultService() : base()
@@ -52,6 +60,13 @@ namespace JNGame.Sync.Frame
}
public void Dispose()
{
base.Cleanup();
thread?.Abort();
thread = null;
}
public override Systems Add(ISystem system)
{
(system as SLogicSystem)?.OnSyncStart();
@@ -85,7 +100,7 @@ namespace JNGame.Sync.Frame
}
/// <summary>
/// 自动更新视图帧
/// 更新(不可在外部调用)
/// </summary>
public override void Execute()
{
@@ -93,9 +108,44 @@ namespace JNGame.Sync.Frame
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
if (paused) return;
#endif
base.Execute();
UnityMainThreadDispatcher.Instance.Enqueue(base.Execute);
}
/// <summary>
/// Unity主线程更新
/// </summary>
/// <param name="ms"></param>
public void MExecute(int ms = -1)
{
if (ms <= -1) ms = (int)Time.deltaTime * 1000;
TickTime = ms;
Execute();
}
/// <summary>
/// 线程更新
/// </summary>
public void TStartExecute(int ms = -1)
{
if (ms <= -1) ms = DeltaTime;
thread = new Thread(() =>
{
while (thread.ThreadState != ThreadState.Aborted)
{
Thread.Sleep(ms);
TickTime = ms;
Execute();
}
});
thread.Start();
}
public void TStopExecute()
{
thread?.Abort();
thread = null;
}
/// <summary>
/// 推逻辑帧

View File

@@ -27,7 +27,7 @@ namespace JNGame.Sync.System
{
}
public void OnSyncDestroy(){}
public virtual void OnSyncDestroy(){}
}
}