更改文件架构
This commit is contained in:
88
Packages/Common~/Scripts/Helper/BehaviourHelper.cs
Normal file
88
Packages/Common~/Scripts/Helper/BehaviourHelper.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using System.Threading.Tasks;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Collections.Concurrent;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public class BehaviourHelper : MonoBehaviour, IMainTicker
|
||||
{
|
||||
public static Action OnStop;
|
||||
public static bool Actived;
|
||||
static BehaviourHelper Singleton;
|
||||
ConcurrentQueue<Action> queue = new();
|
||||
List<Action<float>> Updates = new();
|
||||
List<Action<float>> FixedUpdates = new();
|
||||
public void Add(Action action)
|
||||
{
|
||||
queue.Enqueue(action);
|
||||
}
|
||||
public void AddUpdate(Action<float> action)
|
||||
{
|
||||
Updates.Add(action);
|
||||
}
|
||||
public void AddFixedUpdate(Action<float> action)
|
||||
{
|
||||
FixedUpdates.Add(action);
|
||||
}
|
||||
public void RemoveUpdate(Action<float> action)
|
||||
{
|
||||
Updates.Remove(action);
|
||||
}
|
||||
public void RemoveFixedUpdate(Action<float> action)
|
||||
{
|
||||
FixedUpdates.Remove(action);
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
lock (queue)
|
||||
{
|
||||
while (queue.TryDequeue(out var action))
|
||||
{
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var update in Updates.ToArray())
|
||||
{
|
||||
update.Invoke(Time.deltaTime);
|
||||
}
|
||||
}
|
||||
void FixedUpdate()
|
||||
{
|
||||
foreach (var fixedUpdate in FixedUpdates.ToArray())
|
||||
{
|
||||
fixedUpdate.Invoke(Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
OnStop?.Invoke();
|
||||
}
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void Initialize()
|
||||
{
|
||||
GameObject go = new();
|
||||
GameObject.DontDestroyOnLoad(go);
|
||||
var behaviourHelper = go.AddComponent<BehaviourHelper>();
|
||||
Singleton = behaviourHelper;
|
||||
DI.Register<IMainTicker>(behaviourHelper);
|
||||
Debug.Log($"{nameof(IMainTicker)}已创建");
|
||||
go.name = nameof(BehaviourHelper);
|
||||
go.hideFlags = HideFlags.NotEditable;
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Helper/BehaviourHelper.cs.meta
Normal file
11
Packages/Common~/Scripts/Helper/BehaviourHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d40f7cbccced56a44913d38b56e9810c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
50
Packages/Common~/Scripts/Helper/BehaviourUpdater.cs
Normal file
50
Packages/Common~/Scripts/Helper/BehaviourUpdater.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public static partial class BehaviourUpdaterExtensions
|
||||
{
|
||||
public static void Init(this BehaviorUpdater self, Component root)
|
||||
{
|
||||
self.awakes = root.GetComponentsInChildren<IAwake>(true);
|
||||
self.starts = root.GetComponentsInChildren<IStart>(true);
|
||||
self.updates = root.GetComponentsInChildren<IUpdate>(true);
|
||||
self.fixedUpdates = root.GetComponentsInChildren<IFixedUpdate>(true);
|
||||
self.lateUpdates = root.GetComponentsInChildren<ILateUpdate>(true);
|
||||
self.destroys = root.GetComponentsInChildren<IDestroy>(true);
|
||||
}
|
||||
public static void Init(this BehaviorUpdater self, object root)
|
||||
{
|
||||
if (root is IAwake awake)
|
||||
{
|
||||
self.awakes = new IAwake[1] { awake };
|
||||
}
|
||||
if (root is IStart start)
|
||||
{
|
||||
self.starts = new IStart[1] { start };
|
||||
}
|
||||
if (root is IStop stop)
|
||||
{
|
||||
self.stops = new IStop[1] { stop };
|
||||
}
|
||||
if (root is IUpdate update)
|
||||
{
|
||||
self.updates = new IUpdate[1] { update };
|
||||
}
|
||||
if (root is IFixedUpdate fixedUpdate)
|
||||
{
|
||||
self.fixedUpdates = new IFixedUpdate[1] { fixedUpdate };
|
||||
}
|
||||
if (root is ILateUpdate lateUpdate)
|
||||
{
|
||||
self.lateUpdates = new ILateUpdate[1] { lateUpdate };
|
||||
}
|
||||
if (root is IDestroy destroy)
|
||||
{
|
||||
self.destroys = new IDestroy[1] { destroy };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Helper/BehaviourUpdater.cs.meta
Normal file
11
Packages/Common~/Scripts/Helper/BehaviourUpdater.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cfba5f301e42b224faf7f9f0d23cbdc4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user