forked from zxl/LaboratoryProtection
63 lines
1.4 KiB
C#
63 lines
1.4 KiB
C#
using System;
|
|
using Script;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class ToggleButton : BaseBehaviour
|
|
{
|
|
public Button[] btns;
|
|
[Serializable]
|
|
public class ToggleButtonEvent : UnityEvent<bool> { }
|
|
|
|
public ToggleButtonEvent onValueChanged = new ToggleButtonEvent();
|
|
|
|
public bool IsOn
|
|
{
|
|
get
|
|
{
|
|
if (btns == null || btns.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
return btns[0].gameObject.activeSelf;
|
|
}
|
|
set
|
|
{
|
|
if (btns == null || btns.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
if (value)
|
|
{
|
|
btns[0].gameObject.SetActive(true);
|
|
btns[1].gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
btns[0].gameObject.SetActive(false);
|
|
btns[1].gameObject.SetActive(true);
|
|
}
|
|
onValueChanged?.Invoke(value);
|
|
}
|
|
}
|
|
|
|
private void Reset()
|
|
{
|
|
btns = GetComponentsInChildren<Button>(true);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
btns[0].onClick.AddListener(() => {
|
|
this.IsOn = false;
|
|
AudioManager.Instance.UnMuteAudio();
|
|
|
|
});
|
|
|
|
btns[1].onClick.AddListener(() => {
|
|
this.IsOn = true;
|
|
AudioManager.Instance.MuteAudio();
|
|
|
|
});
|
|
}
|
|
} |