Unity3d中如何实现选中物体高亮的效果
首先到unity商城搜索 Quick Outline 安装免费得高亮插件
Quick Outline | Particles/Effects | Unity Asset Store
也可以直接上面链接安装
然后找到路径QuickOutline\Scripts
把outline 绑定到物体上,其实绑定后就是直接可以显示高亮,那如何实现选中物体就高亮呢?
首先把这个outline组件 先不要开启
然后再物体脚本加入下面代码
private GameObject SelectObj;//选中得物体
void Update() {
if (Input.GetMouseButtonDown(0)) {
if(SelectObj==null)
{
RaycastHit hit=CastRay();
if(!hit.collider.gameObject ){
return;
}else{
if(hit.collider.tag=="Player"){
SelectObj=hit.collider.gameObject;//将射线碰撞物体gamneobj赋值给选中物体selectedobj
SelectObj.GetComponent<Outline>().enabled=true;
//Cursor.visible=false;
}
}
} else{
SelectObj.GetComponent<Outline>().enabled=false;
SelectObj=null;
//Cursor.visible=true;
}
}
}
private RaycastHit CastRay(){
Vector3 screenFar=new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.farClipPlane);
Vector3 screenNear=new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.nearClipPlane);
Vector3 far=Camera.main.ScreenToWorldPoint(screenFar);
Vector3 near=Camera.main.ScreenToWorldPoint(screenNear);
RaycastHit hit;
Physics.Raycast(near,far-near,out hit);
return hit;
}
加入后再运行测试 ,发现点击物体就可以 显示高亮,再点空白地方就不显示