方法介绍
【unity学习笔记】OnMouseEnter、OnMouseOver、OnMouseExit_unity onmouseover_一白梦人的博客-CSDN博客https://blog.csdn.net/a1208498468/article/details/117856445
GetComponent()详解_getcomponet<> 动态名称-CSDN博客https://blog.csdn.net/kaixindragon/article/details/44776451
实现代码
public class color : MonoBehaviour
{
public Color oldcolor;
// Start is called before the first frame update
void Start()
{
oldcolor = GetComponent<Renderer>().material.color; //获取颜色
}
void OnMouseOver()
{
GetComponent<Renderer>().material.color = Color.red; //变成红色
}
void OnMouseExit()
{
GetComponent<Renderer>().material.color = oldcolor; //变回原来的颜色
}
鼠标不在物体上 鼠标放在物体上