解决UI和3D物体同时响应的问题。
在ColliderUI代码里面加上:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ColliderUI : MonoBehaviour ,IPointerClickHandler{
private int _index;
// Use this for initialization
void Start () {
}
public void ChangeColor()
{
if (_index == 0)
{
GetComponent<Image>().color = Color.blue;
}
else {
GetComponent<Image>().color = Color.white;
}
_index = _index == 0 ? 1 : 0;
}
public void OnPointerClick(PointerEventData eventData)
{
ChangeColor();
// ExecuteAll(eventData);
}
public void ExecuteAll(PointerEventData eventData) {
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
foreach (RaycastResult result in results) {
if (result.gameObject != gameObject)
{
ExecuteEvents.Execute(result.gameObject, eventData, ExecuteEvents.pointerClickHandler);
}
}
}
}