1
This commit is contained in:
@@ -28,10 +28,48 @@ namespace BITKit.Entities
|
||||
}
|
||||
|
||||
IServiceProvider Core.Entites.IEntity.ServiceProvider=> throw new InvalidOperationException("Unity Entity can't register component");
|
||||
public void Inject(object obj)
|
||||
{
|
||||
foreach (var fieldInfo in obj
|
||||
.GetType()
|
||||
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Where(fieldInfo=>fieldInfo.GetCustomAttribute<InjectAttribute>() is not null))
|
||||
{
|
||||
var type = fieldInfo.FieldType;
|
||||
var attribute = fieldInfo.GetCustomAttribute<InjectAttribute>();
|
||||
var currentValue = fieldInfo.GetValue(obj);
|
||||
try
|
||||
{
|
||||
switch (currentValue)
|
||||
{
|
||||
case null:
|
||||
break;
|
||||
case Core.Entites.IEntityComponent entityComponent:
|
||||
if(entityComponent.Entity.Id == Id)
|
||||
continue;
|
||||
break;
|
||||
case MonoBehaviour { destroyCancellationToken: { IsCancellationRequested: false } }:
|
||||
continue;
|
||||
case not null:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch (MissingReferenceException){}
|
||||
if(genericEvent.TryGetObjectDirect(type.FullName,out var value))
|
||||
{
|
||||
fieldInfo.SetValue(obj,value);
|
||||
}
|
||||
else if(attribute?.CanBeNull is false)
|
||||
{
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{type.FullName}");
|
||||
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CancellationToken _cancellationToken;
|
||||
private bool isInitialized;
|
||||
private Core.Entites.IEntityComponent[] _components => entityComponents;
|
||||
private Core.Entites.IEntityComponent[] _components => entityComponents.Cast<Core.Entites.IEntityComponent>().ToArray();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -44,39 +82,41 @@ namespace BITKit.Entities
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
foreach (var x in entityComponents)
|
||||
try
|
||||
{
|
||||
foreach (var att in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
|
||||
var monoBehaviours = GetComponentsInChildren<MonoBehaviour>(true);
|
||||
foreach (var x in monoBehaviours)
|
||||
{
|
||||
genericEvent.Set(att.Type,x);
|
||||
genericEvent.Set(att.Type.FullName, x);
|
||||
genericEvent.SetDirect(att.Type.FullName,x);
|
||||
foreach (var att in x
|
||||
//.GetCustomAttributes<CustomTypeAttribute>()
|
||||
.GetType()
|
||||
//.GetCustomAttributes<CustomTypeAttribute>()
|
||||
.GetCustomAttributes()
|
||||
.OfType<CustomTypeAttribute>()
|
||||
)
|
||||
{
|
||||
genericEvent.Set(att.Type,x);
|
||||
genericEvent.Set(att.Type.FullName, x);
|
||||
genericEvent.SetDirect(att.Type.FullName,x);
|
||||
}
|
||||
genericEvent.Set(x.GetType(),x);
|
||||
}
|
||||
}
|
||||
foreach (var x in GetComponentsInChildren<MonoBehaviour>(true))
|
||||
{
|
||||
foreach (var fieldInfo in x
|
||||
.GetType()
|
||||
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Where(fieldInfo=>fieldInfo.GetCustomAttribute<InjectAttribute>() is not null))
|
||||
foreach (var x in monoBehaviours)
|
||||
{
|
||||
var type = fieldInfo.FieldType;
|
||||
if(genericEvent.TryGetObjectDirect(type.FullName,out var value))
|
||||
{
|
||||
fieldInfo.SetValue(x,value);
|
||||
}
|
||||
else
|
||||
{
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{type.FullName}");
|
||||
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
}
|
||||
Inject(x);
|
||||
}
|
||||
}
|
||||
entityComponents.ForEach(x => x.Initialize(this));
|
||||
entityComponents.ForEach(x => x.Initialize(this));
|
||||
|
||||
entityComponents.ForEach(x => x.OnAwake());
|
||||
entityComponents.ForEach(x => x.OnStart());
|
||||
isInitialized = true;
|
||||
entityComponents.ForEach(x => x.OnAwake());
|
||||
entityComponents.ForEach(x => x.OnStart());
|
||||
isInitialized = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning(name);
|
||||
Debug.LogException(e);
|
||||
}
|
||||
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
|
@@ -18,8 +18,14 @@ namespace BITKit.Entities
|
||||
public abstract class EntityComponent : MonoBehaviour, IEntityComponent
|
||||
{
|
||||
public IEntity entity { get; private set; }
|
||||
protected Transform Transform { get; private set; }
|
||||
private IEntity mEntity;
|
||||
public virtual void Initialize(IEntity _entity) { entity = _entity; }
|
||||
public virtual void Initialize(IEntity _entity)
|
||||
{
|
||||
Transform = transform;
|
||||
entity = _entity;
|
||||
Entity = _entity;
|
||||
}
|
||||
public virtual void OnAwake() { }
|
||||
public virtual void OnStart() { }
|
||||
public virtual void OnUpdate(float deltaTime) { }
|
||||
|
@@ -9,17 +9,22 @@ namespace BITKit.Entities
|
||||
bool IsOvering { get; }
|
||||
void AddOverride(object key);
|
||||
void RemoveOverride(object key);
|
||||
event Action<bool> OnOverride;
|
||||
}
|
||||
public interface IEntityOverrideCallback
|
||||
{
|
||||
void OnEntryOverride(bool @override);
|
||||
}
|
||||
[CustomType(typeof(IEntityOverride))]
|
||||
public class EntityOverride : EntityComponent,IEntityOverride
|
||||
{
|
||||
[SerializeField,ReadOnly] private bool isOvering;
|
||||
public bool IsOvering => _allowOverrideHandle;
|
||||
private readonly ValidHandle _allowOverrideHandle = new();
|
||||
public void AddOverride(object key) => _allowOverrideHandle.AddElement(key);
|
||||
public void RemoveOverride(object key)=>_allowOverrideHandle.RemoveElement(key);
|
||||
public event Action<bool> OnOverride;
|
||||
|
||||
public override void Initialize(IEntity _entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
@@ -32,10 +37,8 @@ namespace BITKit.Entities
|
||||
}
|
||||
private void Override(bool @override)
|
||||
{
|
||||
foreach (var x in entity.GetCallbacks<IEntityOverrideCallback>())
|
||||
{
|
||||
x.OnEntryOverride(@override);
|
||||
}
|
||||
OnOverride?.Invoke(@override);
|
||||
isOvering=@override;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user