Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

Effect:

Code:

using UnityEngine;
using System.Collections;
public class MouseEvent_Color : MonoBehaviour
{
    ///The mouse is suspended
    void OnMouseOver()
    {
        transform.Rotate(Vector3.up * 25 * Time.deltaTime);
    }

    // Mouse click
    void OnMouseDown()
    {
        transform.GetComponent<MeshRenderer>().material.color = Color.green;
    }


    // Mouse away
    void OnMouseUp(){ transform.GetComponent<MeshRenderer>().material.color = Color.blue; }}Copy the code

Just hang the script on the object that needs to change color

Note that OnMouseOver(), OnMouseDown(), and OnMouseUp() only trigger events for 3D objects