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

View File

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

View File

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

View File

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