(client) feat:添加Tick接口及时钟控制
This commit is contained in:
8
Client/Assets/Scripts/Base.meta
Normal file
8
Client/Assets/Scripts/Base.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45b1e814df7542342aa8a366c514a477
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
124
Client/Assets/Scripts/Base/Clock.cs
Normal file
124
Client/Assets/Scripts/Base/Clock.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Base/Clock.cs.meta
Normal file
2
Client/Assets/Scripts/Base/Clock.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cd0367766d04d8c4db80bcfe0202e619
|
8
Client/Assets/Scripts/Interface.meta
Normal file
8
Client/Assets/Scripts/Interface.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e255bfd5da1971b47bd84828bf348d0c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Client/Assets/Scripts/Interface/Tick.meta
Normal file
8
Client/Assets/Scripts/Interface/Tick.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f07f237520336f549bf53af368893863
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
11
Client/Assets/Scripts/Interface/Tick/ITick.cs
Normal file
11
Client/Assets/Scripts/Interface/Tick/ITick.cs
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
namespace Cell
|
||||
{
|
||||
public interface ITick
|
||||
{
|
||||
public void Tick()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Interface/Tick/ITick.cs.meta
Normal file
2
Client/Assets/Scripts/Interface/Tick/ITick.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3897d32ba1dc53c4ca44915fd7372a99
|
9
Client/Assets/Scripts/Interface/Tick/ITickPhysics.cs
Normal file
9
Client/Assets/Scripts/Interface/Tick/ITickPhysics.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Cell
|
||||
{
|
||||
public interface ITickPhysics
|
||||
{
|
||||
public void TickPhysics()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13a5a7579420a2a43bdb17c2f7e586f5
|
9
Client/Assets/Scripts/Interface/Tick/ITickUI.cs
Normal file
9
Client/Assets/Scripts/Interface/Tick/ITickUI.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Cell
|
||||
{
|
||||
public interface ITickUI
|
||||
{
|
||||
public void TickUI()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Interface/Tick/ITickUI.cs.meta
Normal file
2
Client/Assets/Scripts/Interface/Tick/ITickUI.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db424d45aa0c9dd41b739434758b274a
|
8
Client/Assets/Scripts/Test.meta
Normal file
8
Client/Assets/Scripts/Test.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49d0f94faee5d0f4bba9b2e76bafdc9e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
30
Client/Assets/Scripts/Test/InitClock.cs
Normal file
30
Client/Assets/Scripts/Test/InitClock.cs
Normal 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;
|
||||
//}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Test/InitClock.cs.meta
Normal file
2
Client/Assets/Scripts/Test/InitClock.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be939c7ca1c4f374b83b6535b3cbb656
|
Reference in New Issue
Block a user