UGUI—单选按钮

作者:追风剑情 发布于:2020-7-23 18:10 分类:Unity3d

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// 单选按钮
/// </summary>
public class UIRadioButton : MonoBehaviour, IPointerClickHandler
{
    public int id;
    [SerializeField]
    private Image m_Image;
    [SerializeField]
    private Sprite m_SelectSprite;
    [SerializeField]
    private Sprite m_UnSelectSprite;
    [SerializeField]
    private Color m_SelectColor = Color.white;
    [SerializeField]
    private Color m_UnSelectColor = Color.white;
    [SerializeField]
    private bool m_Check = false;
    [NonSerialized]
    public UIRadioButtonGroup group;

    public void OnPointerClick(PointerEventData eventData)
    {
        if (m_Check)
            return;
        Check = true;
    }

    public bool Check
    {
        get { return m_Check; }
        set { SetCheck(value);}
    }

    void Start()
    {
        InternalSetCheck(m_Check);
        if (m_Check && group != null)
            group.Check(this);
    }

    private void InternalSetCheck(bool value)
    {
        m_Check = value;
        m_Image.sprite = m_Check ? m_SelectSprite : m_UnSelectSprite;
        m_Image.color = m_Check ? m_SelectColor : m_UnSelectColor;
    }

    public void SetCheck(bool value)
    {
        if (m_Check == value)
            return;
        InternalSetCheck(value);
        if (m_Check && group != null)
            group.Check(this);
    }

    public void SetCheckWithoutNotify(bool value)
    {
        InternalSetCheck(value);
    }
}


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 单选按钮组
/// </summary>
public class UIRadioButtonGroup : MonoBehaviour
{
    [SerializeField]
    private List<UIRadioButton> list = new List<UIRadioButton>();

    [Serializable]
    // 定义按钮OnRadioCheck事件类
    public class OnRadioCheckEvent : UnityEvent<UIRadioButtonGroup> { }

    // 防止序列化变量重命名后丢失引用
    [FormerlySerializedAs("onCheck")]
    [SerializeField]
    private OnRadioCheckEvent m_OnCheck = new OnRadioCheckEvent();

    void Awake()
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == null)
                continue;
            list[i].group = this;
        }
    }

    public void Check(UIRadioButton radio)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == null || list[i] == radio)
                continue;
            list[i].Check = false;
        }
        m_OnCheck.Invoke(this);
    }

    public void SetCheck(int index)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == null)
                continue;
            list[i].Check = (i == index);
        }
    }

    public void SetCheckWithoutNotify(int index)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == null)
                continue;
            list[i].SetCheckWithoutNotify(i == index);
        }
    }

    public UIRadioButton GetFirstActiveRadio()
    {
        UIRadioButton radio = list.Find(x => x.Check);
        return radio;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号