最近制作一个VR的DEMO
UGUI 的Button需要加高亮,为了避免两个Button同时高亮,所有写了一个类似于Tougle Group的东西
==ButtonGroup============
public delegate void HighLightOffHandle(GameObject sender);
public class ButtonGroup : MonoBehaviour {
public event HighLightOffHandle HighLightOffEvent;
public void Notify(GameObject sender)
{
if (HighLightOffEvent != null)
{
HighLightOffEvent(sender);
}
}
}
==ButtonSingle=============
public class ButtonSingle : MonoBehaviour {
public ButtonGroup buttonGroup;
private bool m_IsOn;
private void Awake()
{
m_IsOn = false;
}
private void OnEnable()
{
//将按钮注册进按钮组
if (buttonGroup == null)
return;
buttonGroup.HighLightOffEvent += new HighLightOffHandle(HighLightOff);
}
private void OnDisable()
{
//将按钮移出按钮组
if (buttonGroup == null)
return;
buttonGroup.HighLightOffEvent -= new HighLightOffHandle(HighLightOff);
}
//set方法用于设置是否高亮,并发送事件
public void Set(bool value)
{
if (m_IsOn == value)
return;
m_IsOn = value;
if (buttonGroup != null)
{
if (m_IsOn)
{
buttonGroup.Notify(gameObject);
}
}
ChangeHighlight(true, Color.yellow);
Debug.Log("lezi");
}
private void HighLightOff(GameObject sender)
{
m_IsOn = false;
ChangeHighlight(false, Color.yellow);
}
public void ChangeHighlight(bool isHighlight,Color highlightColor)
{
Highlighter highlighter;
if (GetComponent<Highlighter>())
highlighter = GetComponent<Highlighter>();
else
highlighter = gameObject.AddComponent<Highlighter>();
if (isHighlight)
highlighter.ConstantOnImmediate(highlightColor);
else
highlighter.ConstantOffImmediate();
}
}