Files
Cielonos/Assets/Scripts/MainGame/Base/SubcontrollerBase.cs
SoulliesOfficial 39b43680a9 爆更
2026-07-18 03:16:20 -04:00

54 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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。
/// 需要继续拆分的子逻辑应使用普通 SubmoduleSubmodule 可以作为 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
}
}