64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using System;
|
|
using AibisDream.Utility;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Localization.Components;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream
|
|
{
|
|
[RequireComponent(typeof(Button))]
|
|
public class BoolButton : MonoBehaviour
|
|
{
|
|
private Image _buttonImage;
|
|
private LocalizeStringEvent _stringEvent;
|
|
private Button _button;
|
|
public BoolButtonData data;
|
|
public Func<bool> getBool;
|
|
|
|
public UnityEvent OnClick
|
|
{
|
|
get
|
|
{
|
|
if (_button == null) _button = GetComponent<Button>();
|
|
return _button.onClick;
|
|
}
|
|
}
|
|
|
|
public void Awake()
|
|
{
|
|
_stringEvent = GetComponentInChildren<LocalizeStringEvent>();
|
|
_buttonImage = GetComponentInChildren<Image>();
|
|
// 监听按钮点击
|
|
_button = GetComponent<Button>();
|
|
OnClick.AddListener(() => _ = CommonUtil.Delay(100, AfterClick));
|
|
}
|
|
|
|
private void AfterClick()
|
|
{
|
|
if (_buttonImage)
|
|
{
|
|
_buttonImage.sprite = getBool.Invoke() ? data.trueSprite : data.falseSprite;
|
|
}
|
|
|
|
if (_stringEvent)
|
|
{
|
|
_stringEvent.SetEntry(getBool.Invoke() ? data.trueKey : data.falseKey);
|
|
}
|
|
}
|
|
|
|
public void FreshState()
|
|
{
|
|
AfterClick();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public struct BoolButtonData
|
|
{
|
|
public string trueKey;
|
|
public Sprite trueSprite;
|
|
public string falseKey;
|
|
public Sprite falseSprite;
|
|
}
|
|
} |