AlicizaX/Client/Packages/com.alicizax.unity.ui/Runtime/UI/UIBase/UIHolderObjectBase.cs

90 lines
2.3 KiB
C#
Raw Normal View History

2025-04-28 19:45:45 +08:00
using Sirenix.OdinInspector;
2025-03-04 18:40:14 +08:00
using System;
2025-04-28 19:45:45 +08:00
using Cysharp.Threading.Tasks;
2025-03-04 18:40:14 +08:00
using UnityEngine;
using UnityEngine.UI;
2025-01-24 16:21:00 +08:00
2025-03-04 18:40:14 +08:00
namespace AlicizaX.UI.Runtime
2025-01-24 16:21:00 +08:00
{
2025-03-04 18:40:14 +08:00
[DisallowMultipleComponent]
[HideMonoScript]
public abstract class UIHolderObjectBase : UnityEngine.MonoBehaviour
2025-01-24 16:21:00 +08:00
{
2025-04-28 19:45:45 +08:00
#if UNITY_EDITOR
private void SetAnimtionFlow()
{
// 编辑器模式下自动重置状态
AnimationFlow = GetComponent<AnimationFlow.Runtime.AnimationFlow>();
_hasAnimation = false;
if (AnimationFlow != null)
{
_hasAnimation = AnimationFlow.GetAllAnimationClips.Contains("Show") && AnimationFlow.GetAllAnimationClips.Contains("Close");
}
if (!_hasAnimation) Debug.LogError("请检查是否包含AnimationFlow组件 或者组件内是否包含 [Show] [Close] 动画");
}
#endif
public async UniTask PlayAnimtion(string name)
{
if (!_hasAnimation) return;
await AnimationFlow.PlayAsync(name);
}
[SerializeField] [ReadOnly] private bool _hasAnimation;
2025-01-24 16:21:00 +08:00
2025-03-04 18:40:14 +08:00
private GameObject _target;
2025-01-24 16:21:00 +08:00
2025-03-04 18:40:14 +08:00
/// <summary>
/// UI实例资源对象。
/// </summary>
public GameObject Target => _target ??= gameObject;
2025-01-24 16:21:00 +08:00
2025-03-04 18:40:14 +08:00
private RectTransform _rectTransform;
/// <summary>
/// 窗口矩阵位置组件。
/// </summary>
public RectTransform RectTransform => _rectTransform ??= _target.transform as RectTransform;
/// <summary>
/// 可见性
/// </summary>
2025-04-28 19:45:45 +08:00
public bool Visible
{
get => Target.activeSelf;
internal set { _target.SetActive(value); }
}
#if UNITY_EDITOR
[InlineButton("SetAnimtionFlow")]
#endif
[SerializeField]
private AnimationFlow.Runtime.AnimationFlow AnimationFlow;
2025-03-04 18:40:14 +08:00
private void Awake()
{
_target = gameObject;
}
2025-04-28 19:45:45 +08:00
private bool IsAlive = true;
public static implicit operator bool(UIHolderObjectBase exists)
{
// 先检查Unity对象是否被销毁
if (exists == null) return false;
// 再返回自定义的生命状态
return exists.IsAlive;
}
private void OnDestroy()
{
IsAlive = false;
}
2025-01-24 16:21:00 +08:00
}
}