【Unity3D】鼠标经过物体时变颜色,离开时恢复

2020-03-10 14:29:08 浏览数 (1)

代码语言:txt复制
usingUnityEngine;

usingSystem.Collections;

public class MouseEvent_Color : MonoBehaviour 

{

    //物体初始颜色

    private Color CubeColor;

    private Texture CubeTexture;

    private GameObject ObjCube;

    voidStart () 

    {

        ObjCube = GameObject.Find("Cube");

        ObjCube.renderer.material.shader = Shader.Find("Diffuse");

        CubeColor = ObjCube.renderer.material.GetColor("_Color");

        CubeTexture = renderer.material.GetTexture("Cube");       

    }

    /// <summary>

    /// 鼠标经过

    /// </summary>

    void OnMouseOver()

    {

        renderer.material.color = Color.red;

    }

    /// <summary>

    /// 鼠标进入

    /// </summary>

    voidOnMouseEnter()

    {

        renderer.material.color = Color.red;

    }

    //鼠标离开

    voidOnMouseExit()

    {

        renderer.material.color = CubeColor;

    }

}

0 人点赞