注意: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;
}
}
}