一,腳本的生命週期
腳本的生命週期大致有如下幾個,在不同時刻系統會自動執行對應的生命週期函數,把一下腳本添加到某個遊戲體上,在執行程序,即可看到各函數的執行順序。
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));
}
}
}