Unity实现多个按钮互斥效果或者单个按钮的互斥效果

本文介绍了Unity新手如何通过自定义脚本来实现按钮的按下、抬起效果,并利用按钮管理器控制多个按钮的互斥行为。作者分享了单个按钮和多组互斥按钮的实现方法,包括使用颜色变化、UnityEvents和Prefab来管理状态。
摘要由CSDN通过智能技术生成

注意:Unity新手工作项目工作总结(强迫自己做笔记嘿嘿)

多个按钮互斥效果

这里是自己项目上面的。

单个按钮脚本。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class PushButton : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
	//这里是按钮按下的颜色
	public Color pushColor;
	//这里是当鼠标触碰到按钮的时候,按钮有hover效果
	private Color _hoverColor;
	//这里是保存按钮最初的颜色方便按钮抬起的时候复原
	private Color _originColor;
	//这里是指要修改按钮的物体
	public GameObject backGround;

	//这里分别指按钮按下和抬起事件
	public UnityEvent onPushAction;
	public UnityEvent onPopAction;
	
	private bool _isPused;
	public bool IsPushed
	{
		get{return _isPushed;}
		set
		{	
			if(_isPushed !=value)
			{
				_isPushed = value;
				var material = backGround.GetComponent<Renderer>().material;
				material.color = _isPushed ? pushColor : _originColor;
				if(_isPushed)
				{
					//这里是按钮按下时候要响应的事件,我们可以通过在这里告诉管理器哪一个按钮被按下,也就是说可以把按钮管理器中的方法通过委托的形式添加到这里面。
					onPushAction?.Invoke();
				}else
				{
					onPopAction?.Invoke();
				}
			}
		}
	}
	private void Awake()
    {
        _originColor = backGround.GetComponent<Renderer>().material.color;
        _hoverColor = pushColor;
    }
    public void ButtonClick()
    {
		IsPushed =true;
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        var material = backGround.GetComponent<Renderer>().material;
        material.color = _hoverColor;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
    	//这里的判断是想让按下的按钮不收这个影响。
        if (!_isPushed) 
        {
            //要判断该按钮是不是点击了
            var material = backGround.GetComponent<Renderer>().material;
            material.color = _originColor;
        }
    }
}

我这里是通过按钮管理器来控制多个按钮,其实也可以吧写在按钮脚本里面。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PushButtonManager : MonoBehaviour
{
	//这里通过在编辑器模式下拖拽的形式把按钮添加
    public PushButton[] pushButtons;

    public void PopAllPushButtonInthisGroup(PushButton button)
    {
        foreach (PushButton pushButton in pushButtons)
        {
            if (pushButton != button)
            {
                pushButton.IsPushed = false;
            }
        }
    }
}

上面就是实现多个按钮之间互斥的效果。单个按钮互斥的话就是在按钮中加标识来达到互斥的效果。当一组按钮中即出现多个互斥又出现单个互斥的时候就要在脚本中加多个标识,来达到这个效果。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class PushButton : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    public string buttonId;
    public Color pushColor;
    private Color _hoverColor;
    private Color _originColor;
    public GameObject backGround;
    
    private bool isPopBySelf = false;        // 当前的 pop 行为由自己触发
    public bool allowPopSelf = false;       // 是否允许通过点击自己弹出自己

    public UnityEvent onPushAction;
    public UnityEvent onPopAction;
    
    private bool _isPushed;

    public bool IsPushed
    {
        get { return _isPushed; }
        set
        {
            if (_isPushed != value)
            {
                _isPushed = value;
                var material = backGround.GetComponent<Renderer>().material;
                material.color = _isPushed ? pushColor : _originColor;
                Debug.Log($".............{_isPushed}");
                if (_isPushed)
                {
                    onPushAction?.Invoke();
                }
                else
                {
                    if (isPopBySelf)
                    {
                        onPopAction?.Invoke();
                        isPopBySelf = false;
                    }
                }
            }
        }
    }
    private void Awake()
    {
        _originColor = backGround.GetComponent<Renderer>().material.color;
        _hoverColor = pushColor;
    }
    public void ButtonClick()
    {
        if (_isPushed && allowPopSelf)
        {
            isPopBySelf = true;
            IsPushed = false;
        }
        else if (!IsPushed)
        {
            IsPushed = true;
        }
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        var material = backGround.GetComponent<Renderer>().material;
        material.color = _hoverColor;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (!_isPushed)
        {
            //要判断该按钮是不是点击了
            var material = backGround.GetComponent<Renderer>().material;
            material.color = _originColor;
        }
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值