天天看點

Unity基礎知識總結

 一,腳本的生命周期

      腳本的生命周期大緻有如下幾個,在不同時刻系統會自動執行對應的生命周期函數,把一下腳本添加到某個遊戲體上,在執行程式,即可看到各函數的執行順序。

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));
		}
           
}
           
}