Unity通过射线快速变化物体颜色

using UnityEngine;
using System.Collections;


public class Task : MonoBehaviour {

    Ray ray;
    RaycastHit _hit;

    GameObject cube;
    Material color1;

    // Use this for initialization
    void Start () {
        //创建一个方块
        cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

        color1= GameObject.Find("Cube").GetComponent().material;
    }

    // Update is called once per frame
    void Update () {

       //获取鼠标射线
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out _hit))
        {
            if (_hit.transform.gameObject.GetComponent())
            {
               //随机一种颜色
                Color color = new Color(Random.Range(0f, 1f) * Time.deltaTime * 30f, Random.Range(0f, 1f) * Time.deltaTime * 30f, Random.Range(0f, 1f) * Time.deltaTime * 30f);
                //给方块添加上颜色
                color1.color = color;
            }
        }

    }


}

你可能感兴趣的:(Unity-物体操作)