123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System;
- using XGame.Framework.Interfaces;
- namespace XGame.Framework.Components
- {
- /// <summary>
- /// 逻辑行为组件基类
- /// </summary>
- /// <typeparam name="TContext"></typeparam>
- public abstract class Component<TContext> : IComponent, IReference, IActiveable, IDisposable
- {
- public TContext Context { get; private set; }
- public virtual int Order => 0;
- void IComponent.Bind(object handle)
- {
- Context = (TContext)handle;
- }
- public bool Active { get; private set; }
- void IActiveable.Enable(object intent)
- {
- if (Active) return;
- Active = true;
- OnEnable(intent);
- }
- void IActiveable.Disable()
- {
- if (!Active) return;
- Active = false;
- OnDisable();
- }
- void IReference.Clear()
- {
- OnDispose();
- }
- void IDisposable.Dispose()
- {
- OnDispose();
- Context = default;
- }
- protected virtual void OnEnable(object intent)
- {
- }
- protected virtual void OnDisable()
- {
- }
- /// <summary>
- /// 销毁事件回调
- /// 缓存池回收也会触发OnDispose,但不真正销毁
- /// </summary>
- protected abstract void OnDispose();
- }
- }
|