您的位置:首页 > 移动开发 > Unity3D

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

2017-11-10 20:22 411 查看
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<MeshRenderer>().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<MeshRenderer>())
{
//随机一种颜色
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 color 射线