添加线程逻辑执行

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(){}
}
}

View File

@@ -33,11 +33,6 @@ public class DApplication : MonoBehaviour
}
private void Update()
{
App.Game.Update();
}
public void OnClickPlayerCreate()
{
var input = App.Game.GetInput<IDWorld>();

View File

@@ -26,6 +26,13 @@ namespace AppGame.Systems
}
public override void OnClose()
{
base.OnClose();
client?.Dispose();
server?.Dispose();
}
/// <summary>
/// 运行同步类
/// </summary>
@@ -33,24 +40,17 @@ namespace AppGame.Systems
{
client = new T();
client.Initialize();
client.TStartExecute();
return client as T;
}
public T StartServer<T>() where T : JNSStateServerService,new()
{
server = new T();
server.Initialize();
server.TStartExecute();
return server as T;
}
/// <summary>
/// 更新周期
/// </summary>
public void Update()
{
client?.Execute();
server?.Execute();
}
/// <summary>
/// 获取第一个客户端的输入类
/// </summary>

View File

@@ -1,6 +1,7 @@
using Game.JNGFrame.View.Entity;
using JNGame.Sync.System;
using JNGame.Sync.View;
using JNGame.Util;
namespace Game.JNGFrame.View
{
@@ -23,7 +24,8 @@ namespace Game.JNGFrame.View
base.OnSyncUpdate(dt);
foreach (var view in views)
{
view.Execute();
//视图逻辑交给主线程运行
UnityMainThreadDispatcher.Instance.Enqueue(view.Execute);
}
}
// public override void Execute()