This commit is contained in:
CortexCore 2024-06-26 17:53:10 +08:00
parent 46188dc637
commit 4a8c97d1fd
4 changed files with 50 additions and 16 deletions

View File

@ -15,10 +15,12 @@ namespace BITKit.Entities
{ {
[CustomType(typeof(IUnityEntity))] [CustomType(typeof(IUnityEntity))]
[CustomType(typeof(IEntity))] [CustomType(typeof(IEntity))]
public class Entity : MonoBehaviour, IUnityEntity public class Entity : MonoBehaviour, IUnityEntity,IEntity
{ {
private readonly GenericEvent genericEvent = new(); private readonly GenericEvent genericEvent = new();
[SerializeField,ReadOnly] private int id; [SerializeField,ReadOnly] private int id;
[SerializeField] private bool useAwake;
[SerializeField, ReadOnly] private string serviceReport = "Waiting";
public int Id public int Id
{ {
get => id; get => id;
@ -140,7 +142,7 @@ namespace BITKit.Entities
public void WaitForInitializationComplete() public void WaitForInitializationComplete()
{ {
if (isInitialized) return; if (isInitialized) return;
Start(); InitializeInternel();
isInitialized = true; isInitialized = true;
} }
private void Awake() private void Awake()
@ -149,13 +151,32 @@ namespace BITKit.Entities
Id = GetInstanceID(); Id = GetInstanceID();
CancellationToken = gameObject.GetCancellationTokenOnDestroy(); CancellationToken = gameObject.GetCancellationTokenOnDestroy();
Set(CancellationToken); Set(CancellationToken);
if (useAwake)
{
InitializeInternel();
}
} }
private void Start() private void Start()
{
if (!useAwake)
{
InitializeInternel();
}
Behaviors.ForEach(x => x.OnStart());
UnityEntitiesService.Register(this);
destroyCancellationToken.Register(() => UnityEntitiesService.UnRegister(this));
}
private void InitializeInternel()
{ {
if (isInitialized) return; if (isInitialized) return;
try try
{ {
var reportBuilder = new StringBuilder();
AddService<IEntity>(this); AddService<IEntity>(this);
AddService<Entity>(this); AddService<Entity>(this);
AddService<IUnityEntity>(this); AddService<IUnityEntity>(this);
@ -165,18 +186,28 @@ namespace BITKit.Entities
Behaviors.ForEach(x => x.Initialize(this)); Behaviors.ForEach(x => x.Initialize(this));
foreach (var x in monoBehaviours) foreach (var x in monoBehaviours)
{ {
foreach (var att in x try
//.GetCustomAttributes<CustomTypeAttribute>()
.GetType()
//.GetCustomAttributes<CustomTypeAttribute>()
.GetCustomAttributes()
.OfType<CustomTypeAttribute>()
)
{ {
AddService(att.Type, x); foreach (var att in x
if (att.AsGlobal) //.GetCustomAttributes<CustomTypeAttribute>()
DI.Register(att.Type, x); .GetType()
//.GetCustomAttributes<CustomTypeAttribute>()
.GetCustomAttributes()
.OfType<CustomTypeAttribute>()
)
{
reportBuilder.AppendLine(att.Type.FullName);
AddService(att.Type, x);
if (att.AsGlobal)
DI.Register(att.Type, x);
}
} }
catch (Exception e)
{
BIT4Log.Warning<Entity>("Failed to inject " + x.GetType().Name);
throw;
}
serviceReport = reportBuilder.ToString();
genericEvent.Set(x.GetType(),x); genericEvent.Set(x.GetType(),x);
} }
@ -187,7 +218,7 @@ namespace BITKit.Entities
Behaviors.ForEach(x => x.OnAwake()); Behaviors.ForEach(x => x.OnAwake());
Behaviors.ForEach(x => x.OnStart());
isInitialized = true; isInitialized = true;
} }
catch (Exception e) catch (Exception e)
@ -195,8 +226,8 @@ namespace BITKit.Entities
Debug.LogWarning(name); Debug.LogWarning(name);
Debug.LogException(e); Debug.LogException(e);
} }
UnityEntitiesService.Register(this);
destroyCancellationToken.Register(() => UnityEntitiesService.UnRegister(this));
_initialized = true; _initialized = true;
} }

View File

@ -89,7 +89,7 @@ public class UnityEntitiesServiceSingleton:IEntitiesService
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
[CustomType(typeof(IEntitiesService))]
public class UnityEntitiesService : MonoBehaviour,IEntitiesService public class UnityEntitiesService : MonoBehaviour,IEntitiesService
{ {
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]

View File

@ -6,6 +6,8 @@ using UnityEngine;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
namespace BITKit.Net.Kcp namespace BITKit.Net.Kcp
{ {
[CustomType(typeof(INetClient))]
[CustomType(typeof(INetProvider))]
public class MonoKcpClient : MonoBehaviour,INetClient,INetProvider public class MonoKcpClient : MonoBehaviour,INetClient,INetProvider
{ {
internal static MonoKcpClient Singleton { get; private set; } internal static MonoKcpClient Singleton { get; private set; }

View File

@ -22,6 +22,7 @@ namespace BITKit
public void Remove(Action<float> action)=>GameTickService.Remove(action); public void Remove(Action<float> action)=>GameTickService.Remove(action);
} }
[CustomType(typeof(ITicker))]
public class GameTickService : MonoBehaviour,ITicker public class GameTickService : MonoBehaviour,ITicker
{ {
[RuntimeInitializeOnLoadMethod] [RuntimeInitializeOnLoadMethod]