task.cs 982 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using System.Collections;
  3. public class task : MonoBehaviour
  4. {
  5. Ray ray;
  6. RaycastHit _hit;
  7. GameObject cube;
  8. Material color1;
  9. // Use this for initialization
  10. void Start()
  11. {
  12. //创建一个方块
  13. cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  14. color1 = cube.GetComponent<MeshRenderer>().material;
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. //获取鼠标射线
  20. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  21. if (Physics.Raycast(ray, out _hit))
  22. {
  23. if (_hit.transform.gameObject.GetComponent<MeshRenderer>())
  24. {
  25. //随机一种颜色
  26. 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);
  27. //给方块添加上颜色
  28. color1.color = color;
  29. }
  30. }
  31. }
  32. }