com.alicizax.unity.ui.exten.../Runtime/UXComponent/Hotkey/HotkeyBindComponent.cs

67 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using AlicizaX.UI.Runtime;
using Sirenix.OdinInspector;
using UnityEngine;
namespace AlicizaX.UI.Extension.UXComponent.Hotkey
{
public class HotkeyBindComponent : MonoBehaviour
{
private UIHolderObjectBase _holderObjectBase;
private void Awake()
{
_holderObjectBase = GetComponent<UIHolderObjectBase>();
_holderObjectBase.OnWindowShowEvent += BindHotKeys;
_holderObjectBase.OnWindowClosedEvent += UnBindHotKeys;
}
private void OnDestroy()
{
_holderObjectBase.OnWindowShowEvent -= BindHotKeys;
_holderObjectBase.OnWindowClosedEvent -= UnBindHotKeys;
}
#if UNITY_EDITOR
[InlineButton("SetHotKeyButtons")]
#endif
[SerializeField]
[HideLabel]
private UXButton[] hotButtons;
#if UNITY_EDITOR
private void SetHotKeyButtons()
{
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();
}
#endif
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();
}
}
}
}