(一)解决UI和3D物体层叠时,只响应UI的问题
创建一个Cube和一个Image
对Cube加入脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeOnClick : MonoBehaviour,IPointerClickHandler
{
int index;
private void OnMouseDown()
{
ChangeColor();
}
void Start()
{
index = 0;
}
void ChangeColor()
{
if (index == 0)
{
GetComponent<MeshRenderer>().material.SetColor("_Color", Color.black);
}
else
{
GetComponent<MeshRenderer>().material.SetColor("_Color", Color.white);
}
index = index == 0 ? 1 : 0;
}
}
发现在image上点击也能使得Cube变色
在Camera中添加Physics Raycsst:
对Cube 和 Image分别添加脚本,使用 IPointerClickHandler 接口 :
Cube:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CubeOnClick : MonoBehaviour,I