(client) feat:添加Tick接口及时钟控制

This commit is contained in:
m0_75251201
2025-07-09 11:35:52 +08:00
parent 11004d5595
commit 10136a987d
14 changed files with 225 additions and 0 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 45b1e814df7542342aa8a366c514a477
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,124 @@

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Cell
{
public class Clock : MonoBehaviour
{
private static Clock _instance;
public bool pause = false;
public List<ITick> ticks = new();
public List<ITickPhysics> tickPhysics = new();
public List<ITickUI> tickUIs = new();
//float timer = 0;
public static Clock Instance
{
get
{
if (_instance == null)
{
// 检查场景中是否已存在实例
_instance = FindAnyObjectByType<Clock>();
// 如果不存在,创建一个新的
if (_instance == null)
{
GameObject clockObject = new GameObject("[Clock]");
_instance = clockObject.AddComponent<Clock>();
DontDestroyOnLoad(clockObject); // 确保对象不被销毁
}
}
return _instance;
}
}
private void Awake()
{
// 确保只有一个实例存在
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this; // 设置当前实例为静态变量
SceneManager.sceneLoaded += OnSceneLoaded; // 注册场景加载事件
}
private void OnDestroy()
{
// 移除事件监听
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// 场景加载完成后调用 Init 方法
Init();
}
/// <summary>
/// 初始化方法
/// </summary>
public void Init()
{
ticks.Clear();
tickPhysics.Clear();
tickUIs.Clear();
foreach (var obj in UnityEngine.Object.FindObjectsByType<MonoBehaviour>(FindObjectsSortMode.None))
{
if (obj is ITick tickObj)
{
ticks.Add(tickObj);
}
if (obj is ITickPhysics physicsObj)
{
tickPhysics.Add(physicsObj);
}
if (obj is ITickUI uiObj)
{
tickUIs.Add(uiObj);
}
}
}
private void Update()
{
if (!pause)
{
foreach (var tick in ticks)
{
tick.Tick();
}
}
foreach (var uiTick in tickUIs)
{
uiTick.TickUI();
}
//if (timer > 1)
//{
// timer -= 1;
// Debug.Log("滴答");
//}
//timer += Time.deltaTime;
}
void FixedUpdate()
{
if(!pause)
{
foreach (var physicsTick in tickPhysics)
{
physicsTick.TickPhysics();
}
}
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cd0367766d04d8c4db80bcfe0202e619

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e255bfd5da1971b47bd84828bf348d0c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f07f237520336f549bf53af368893863
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,11 @@
namespace Cell
{
public interface ITick
{
public void Tick()
{
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3897d32ba1dc53c4ca44915fd7372a99

View File

@ -0,0 +1,9 @@
namespace Cell
{
public interface ITickPhysics
{
public void TickPhysics()
{
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 13a5a7579420a2a43bdb17c2f7e586f5

View File

@ -0,0 +1,9 @@
namespace Cell
{
public interface ITickUI
{
public void TickUI()
{
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: db424d45aa0c9dd41b739434758b274a

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 49d0f94faee5d0f4bba9b2e76bafdc9e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,30 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Cell
{
public class InitClock : MonoBehaviour
{
static float timer = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
var clock= Clock.Instance;
}
// Update is called once per frame
//void Update()
//{
// if (Input.GetKeyUp(KeyCode.W))
// {
// SceneManager.LoadScene("SampleScene");
// }
// if (timer > 1)
// {
// timer -= 1;
// Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӳ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>");
// }
// timer += Time.deltaTime;
//}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: be939c7ca1c4f374b83b6535b3cbb656