com.alicizax.unity.framework/Runtime/UI/UIBase/UIHolderObjectBase.cs

134 lines
3.1 KiB
C#
Raw Normal View History

2025-09-05 19:46:30 +08:00
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Sirenix.Utilities;
using UnityEngine;
using UnityEngine.UI;
namespace AlicizaX.UI.Runtime
{
[DisallowMultipleComponent]
[HideMonoScript]
public abstract class UIHolderObjectBase : UnityEngine.MonoBehaviour
{
#if UNITY_EDITOR && ALICIZAX_UI_ANIMATION_SUPPORT
private void SetAnimtionFlow()
{
// 编辑器模式下自动重置状态
AnimationFlow = GetComponent<AnimationFlow.Runtime.AnimationFlow>();
}
#endif
#if UNITY_EDITOR && ALICIZAX_UI_EXTENSION_SUPPORT
private void SetHotKeyButtons()
{
2025-09-08 10:32:45 +08:00
var btns = transform.GetComponentsInChildren<UXButton>(true);
var hotBtnList = new List<UXButton>();
for (int i = 0; i < btns.Length; i++)
{
if (btns[i].HasHotKeyRefrenced())
{
hotBtnList.Add(btns[i]);
}
}
hotButtons = hotBtnList.ToArray();
2025-09-05 19:46:30 +08:00
}
#endif
#if ALICIZAX_UI_EXTENSION_SUPPORT
2025-09-05 20:56:26 +08:00
#if UNITY_EDITOR
[InlineButton("SetHotKeyButtons")]
#endif
[SerializeField]
[HideLabel]
2025-09-10 14:26:54 +08:00
private UXButton[] hotButtons;
2025-09-05 19:46:30 +08:00
internal void BindHotKeys()
{
for (int i = 0; i < hotButtons.Length; i++)
{
hotButtons[i].BindHotKey();
}
}
internal void UnBindHotKeys()
{
for (int i = 0; i < hotButtons.Length; i++)
{
hotButtons[i].UnBindHotKey();
}
}
#endif
#if ALICIZAX_UI_ANIMATION_SUPPORT
public async UniTask PlayAnimtion(string name)
{
await AnimationFlow.PlayAsync(name);
}
#endif
private GameObject _target;
/// <summary>
/// UI实例资源对象。
/// </summary>
public GameObject Target => _target ??= gameObject;
private RectTransform _rectTransform;
/// <summary>
/// 窗口矩阵位置组件。
/// </summary>
public RectTransform RectTransform => _rectTransform ??= _target.transform as RectTransform;
/// <summary>
/// 可见性
/// </summary>
public bool Visible
{
get => Target.activeSelf;
internal set { _target.SetActive(value); }
}
#if UNITY_EDITOR && ALICIZAX_UI_ANIMATION_SUPPORT
[InlineButton("SetAnimtionFlow")]
#endif
#if ALICIZAX_UI_ANIMATION_SUPPORT
[SerializeField]
[HideLabel]
private AnimationFlow.Runtime.AnimationFlow AnimationFlow;
#endif
private void Awake()
{
_target = gameObject;
}
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;
}
}
}