一,脚本的生命周期
脚本的生命周期大致有如下几个,在不同时刻系统会自动执行对应的生命周期函数,把一下脚本添加到某个游戏体上,在执行程序,即可看到各函数的执行顺序。
using UnityEngine;
using System.Collections;
public class life : MonoBehaviour {
// Use this for initialization
void Start () { //will be called after Awake , only once
Debug.Log ("onstart");
}
// Update is called once per frame
void Update () {//each frame
Debug.Log ("Update");
}
void LateUpdate(){//after update
Debug.Log ("LateUpdate");
}
void FixedUpdate(){//in a fixed time ,can set in the setting
Debug.Log ("FixedUpdate");
}
void Awake(){// first be called, only once
Debug.Log ("Awake");
}
void OnGUI(){//each frame.draw sth;
Debug.Log ("OnGUI");
}
void OnDestroy(){ //last ,when the script need to be destroyed;
Debug.Log ("OnDestroy");
}
}
其中,Awake()在脚本唤醒时就会执行,只执行一次,紧接着会执行Start ()在这里可以进行一些初始化操作, Update (), LateUpdate(),OnGUI()每帧都会执行,在这里绘制一些UI界面,如按钮,FixedUpdate()会在固定时间间隔执行,该时间间隔可在软件中设置,OnDestroy()会在脚本摧毁时调用。
二,输入和运动控制
获取输入是实现交换的基础,Unity中的输入都与Input类有关,通过查阅Input类可以了解Unity的输入。Unity针对pc平台常用的输入有按键输入,和鼠标输入,针对移动平台,则还有触摸输入,利用传感器值的变化作为输入等。
这里主要记录如何获取按键输入,和鼠标输入。
1. 获取按键输入
按键输入有3种状态:按下(GetKeyDown()),长按(GetKey()),抬起(GetKeyUp()),传进去对应按键的值。则可根据返回值判断对应状态是否触发,如Input.GetKeyDown (KeyCode.A),当A按下时,该函数就会返回true。
2.获取鼠标输入
鼠标输入和按键输入类似,也有Input.GetMouseButtonDown(),GetMouseButton(),Input.GetMouseButtonUp()3种方法,分别对应着按下,长按,抬起3种状态。输入参数可以是0,1,2分别对应着鼠标左键,鼠标右键,鼠标滑轮。此外,还可以通过Input.mousePosition获取鼠标所在的位置。
下面是一个简单例子,当某种状态出现时,就打印出相应日志。
using UnityEngine;
using System.Collections;
public class InputTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.A)) {
Debug.Log ("A-down");
;
}
if (Input.GetKey (KeyCode.A)) {
Debug.Log ("A-keep");
}
if (Input.GetKeyUp (KeyCode.A)) {
Debug.Log ("A-up");
}
if (Input.GetMouseButtonDown (2)) {
Debug.Log ("MOUSE-DOWN"+Input.mousePosition);
}
if (Input.GetMouseButton (2)) {
Debug.Log ("MOUSE-KEEP"+Input.mousePosition);
}
if (Input.GetMouseButtonUp (2)) {
Debug.Log ("MOUSE-UP"+Input.mousePosition);
}
}
}
进一步,获取输入信息之后,我们就可以控制场景中的物体做一些变换,如简单的移动,旋转物体等,接下来,就讲述一下如何控制场景中的某个物体移动:
物体的运动状态的改变本质应该就是它的Transform组件的改变,如Transform中Position值的改变会改变物体的空间位置,Rotation的变化会使物体在空间旋转,Scale的改变会改变物体的大小。所以,只要更改物体Transform组件中各属性的值即可改变物体的运动状态。如:
mCube.transform.Rotate (0.0f, Time.deltaTime * 200.0f, 0.0f);就是使物体绕y轴旋转一定角度(mCube自定义的是一个立方体)
mCube.transform.Translate (new Vector3 (0, 0.1f, 0));是使物体沿y轴移动0.1,
比如,游戏中常见的通过W,S,A,D控制物体上下左右移动:
using UnityEngine;
using System.Collections;
public class InputTest : MonoBehaviour {
GameObject mCube;
int scale=1;
// Use this for initialization
void Start () {
mCube=GameObject.Find("Cube");
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
mCube.transform.Translate (new Vector3 (0, Time.deltaTime*100.0f, 0));
}
if (Input.GetKey (KeyCode.S)) {
mCube.transform.Translate (new Vector3 (0, -Time.deltaTime*100.0f, 0));
}
if (Input.GetKey (KeyCode.A)) {
mCube.transform.Translate (new Vector3 (0, 0, Time.deltaTime*100.0f));
}
if (Input.GetKey (KeyCode.D)) {
mCube.transform.Translate (new Vector3 (0, 0,-Time.deltaTime*100.0f));
}
}
}