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