其他分享
首页 > 其他分享> > 【UI界面开发】基本组件——开关

【UI界面开发】基本组件——开关

作者:互联网

文章目录

摘要

Toggle基本要素

UGUI之Toggle

Unity预设Toggle组成分析

分析

Toggle物体组成

总结

Inspector窗口参数

分析

Toggle的Inspector窗口参数

总结

Toggle常用成员分析

  1. onValueChanged : 提供事件监听的注册和移除方法,可以通过代码的方式在开关状态转换时进行处理。
  2. isOn:可以动态获取和修改Toggle的开关状态
  3. group:可以动态获取或指派group

自制Toggle

这里只进行粗略地实现,只实现基本功能,一些细节则不具体完成,如动态设置isOn时,随时调整图片的显示状态等。

实现Button部分的交互

实现Toggle特定的交互

源码

public class CToggle : MonoBehaviour, IPointerEnterHandler, IPointerUpHandler, IPointerExitHandler, IPointerDownHandler, IPointerClickHandler
{
        #region Inspector Setting
        [SerializeField] private bool interactivable;
        [SerializeField] private Image targetImage;
        [SerializeField] private Text targetText;
        [SerializeField] private Color normalColor;
        [SerializeField] private Color highlightColor;
        [SerializeField] private Color pressedColor;
        [SerializeField] private Color selectColor;
        [SerializeField] private Color disableColor;

        [SerializeField] private bool IsOn;
        [SerializeField] private Image toggleGrapics;
        #endregion

        #region callBack Methods

        private void Awake()
        {
            if (interactivable)
            {
                if (targetImage != null)
                    targetImage.color = normalColor;
                if (targetText != null)
                    targetText.color = normalColor;
            }
            else
            {
                if (targetImage != null)
                    targetImage.color = disableColor;
                if (targetText != null)
                    targetText.color = disableColor;
            }

            if (isOn)
                toggleGrapics.gameObject.SetActive(true);
            else
                toggleGrapics.gameObject.SetActive(false);

        }

        //聚焦
        public void OnPointerEnter(PointerEventData eventData)
        {
            if (!Interactivable) return;
            //聚焦时,颜色变为高亮色
            if (targetImage != null)
                targetImage.color = highlightColor;
            if (targetText != null)
                targetText.color = highlightColor;
        }

        //失焦
        public void OnPointerExit(PointerEventData eventData)
        {
            if (!Interactivable) return;
            if (targetImage != null)
                targetImage.color = normalColor;
            if (targetText != null)
                targetText.color = normalColor;
        }

        //点下
        public void OnPointerDown(PointerEventData eventData)
        {
            if (!Interactivable) return;
            if (targetImage != null)
                targetImage.color = pressedColor;
            if (targetText != null)
                targetText.color = pressedColor;
        }
        //释放
        public void OnPointerUp(PointerEventData eventData)
        {
            if (!Interactivable) return;

            if (targetImage != null)
                targetImage.color = selectColor;
            if (targetText != null)
                targetText.color = selectColor;
        }
        //点击
        public void OnPointerClick(PointerEventData eventData)
        {
            isOn = !isOn;
            if (isOn)
                toggleGrapics.gameObject.SetActive(true);
            else
                toggleGrapics.gameObject.SetActive(false);

            onValueChanged?.Invoke(isOn);
        }
        #endregion

        #region code read-in references
        //点击委托
        public Action<bool> onValueChanged;
        //是否可交互
        public bool Interactivable { set => interactivable = value; get => interactivable; }
        //控制的渲染对象
        public Image TargetImage { set => targetImage = value; get => targetImage; }
        public Text TargetText { set => targetText = value; get => targetText; }

        //普通状态的颜色
        public Color NormalColor { set => normalColor = value; get => normalColor; }
        //聚焦状态的颜色
        public Color HighlightColor { set => highlightColor = value; get => highlightColor; }
        //点击状态的颜色
        public Color PressedColor { set => pressedColor = value; get => pressedColor; }
        //选中状态的颜色
        public Color SelectColor { set => selectColor = value; get => selectColor; }
        //禁用状态的颜色
        public Color DisableColor { set => disableColor = value; get => disableColor; }

        public bool isOn { get => IsOn; set => IsOn = value; }
        public Image grapics { get => toggleGrapics; set => toggleGrapics = value; }
        #endregion



}

标签:界面,color,targetImage,Toggle,targetText,组件,UI,null,public
来源: https://blog.csdn.net/weixin_45764934/article/details/123098457