这是本栏目的开篇文章,从一开始就了解游戏开发的编程。
流程
创建一个Scene并添加一个Cube,在Cube上挂载ExampleBehaviourScript.cs
这个文件,然后启动游戏。通过键盘输入 R 或 G 或 B 来改变Cube的颜色。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| using UnityEngine; using System.Collections;
public class ExampleBehaviourScript : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.R)) { GetComponent<Renderer> ().material.color = Color.red; } if (Input.GetKeyDown(KeyCode.G)) { GetComponent<Renderer>().material.color = Color.green; } if (Input.GetKeyDown(KeyCode.B)) { GetComponent<Renderer>().material.color = Color.blue; } } }
|