using System; using Sirenix.OdinInspector; using UnityEngine; namespace Cielonos.MainGame { /// /// Subcontroller 是可挂载到 GameObject 上的 Unity Component。 /// 所有继承 SubcontrollerBase 的具体类型必须保持为 namespace 下的顶层类,不能写成 owner 的 nested class。 /// 具体 Subcontroller 类声明上方必须添加 RequireComponent(typeof(OwnerType)),明确它依附的 owner component。 /// 需要继续拆分的子逻辑应使用普通 Submodule;Submodule 可以作为 owner 的 nested class。 /// /// /// 子控制器通用接口。 /// public interface ISubcontroller { T Owner { get; set; } void Initialize(); } public class SubcontrollerBase : SerializedMonoBehaviour, ISubcontroller, ISubstructureOwner where T : MonoBehaviour { [SerializeField, HideInEditorMode] protected T owner; public T Owner => owner; T ISubcontroller.Owner { get => Owner; set => owner = value; } public virtual void Initialize() { owner ??= GetComponent() ?? GetComponentInParent(); } void ISubstructureOwner.InitializeSubstructures() { Initialize(); } #if UNITY_EDITOR protected virtual void Reset() { owner ??= GetComponent() ?? GetComponentInParent(); } #endif } }