📜  使按钮的行为类似于切换 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:43.101000             🧑  作者: Mango

代码示例1
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class ToggleButton : MonoBehaviour, IPointerClickHandler
{
    public ToggleEvent CheckedChanged = new ToggleEvent();

    Image _image;
    Color _originalColor;

    bool _checked;
    [SerializeField] Color _checkedColor;
    [SerializeField] ToggleButtonGroup _group;


    [SerializeField]
    public bool Checked
    {
        get
        {
            return _checked;
        }
        set
        {
            if (_checked != value)
            {
                _checked = value;
                UpdateVisual();
                CheckedChanged.Invoke(this);
            }
        }
    }

    void Start()
    {
        _image = GetComponent();
        _originalColor = _image.color;

        if (_group != null)
            _group.RegisterToggle(this);
    }

    private void UpdateVisual()
    {
        _image.color = Checked ? _checkedColor : _originalColor;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Checked = !Checked;
    }

    [Serializable]
    public class ToggleEvent : UnityEvent
    {
    }
}