Files
Cielonos/Assets/Scripts/MainGame/Base/SubcontrollerBase.cs

41 lines
968 B
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
using System;
using Sirenix.OdinInspector;
using UnityEngine;
namespace Cielonos.MainGame
{
2026-06-05 04:21:00 -04:00
/// <summary>
/// 子控制器通用接口。
/// </summary>
public interface ISubcontroller<T>
{
T Owner { get; set; }
void Initialize();
}
public class SubcontrollerBase<T> : SerializedMonoBehaviour, ISubcontroller<T> where T : MonoBehaviour
2025-11-25 08:19:33 -05:00
{
2026-05-23 08:27:50 -04:00
[HideInEditorMode]
2025-11-25 08:19:33 -05:00
public T owner;
2026-06-05 04:21:00 -04:00
// 显式接口实现以满足 ISubcontroller 接口,保持原有 public 字段完整兼容性
T ISubcontroller<T>.Owner
{
get => owner;
set => owner = value;
}
2025-11-25 08:19:33 -05:00
public virtual void Initialize()
{
owner ??= GetComponent<T>() ?? GetComponentInParent<T>();
}
#if UNITY_EDITOR
protected virtual void Reset()
{
owner ??= GetComponent<T>() ?? GetComponentInParent<T>();
}
#endif
}
}