GameObject

Unity中的最基本構成−遊戲物件。

// 取得在Hierachy中名稱叫Enemy的物件
GameObject enemy = GameObject.Find("Enemy");

GetComponent()

取得遊戲物件底下的元件。

// 取得遊戲物件身上的Animator元件,並把它放到anim變數容器中。
Animator anim = GetComponent<Animator>();
// 取得遊戲物件身上的BoxCollider2D元件,並把它放到box變數容器中。
BoxCollider2D box = GetComponent<BoxCollider2D>();
// 停用該元件
box.enabled = false;

Instantiate()

用於生成物件。

GameObject prefab;
Vector3 position = Vector3.zero;
Quaternion rotation = Quaternion.identity;
// 生成一個物件在原點且角度沒有任何變動。
Instantiate(prefab, position, rotation);

SendMessage()

呼叫指定物件上腳本中任何一個符合特定字串的方法。

// 呼叫自身物件上腳本中任一叫做Walk的方法
gameObject.SendMessage("Walk");
// 呼叫Enemy物件上腳本中任一叫做Die的方法
GameObject.Find("Enemy").SendMessage("Die");

DontDestroyOnLoad()

讓目標物件在新場景不會自動被清除。

DontDestroyOnLoad(transform.gameObject);

Destroy()

從Hierarchy中移除遊戲物件。

Destroy(gameObject); // 將綁定此腳本的遊戲物件移除

Debug

.Log()

在console窗格印出訊息

Debug.Log("Hello Indie Game! XD");

.DrawRay()

從物件本身出發往(0, 0, 1)方向繪製出一條長10單位的射線

void Update () {
    Debug.DrawRay (transform.position, Vector3.forward, 10, Color.green);
}

Ref: stackoverflow

Input

Input類別掌管所有輸入行為,包括觸控、滑鼠、鍵盤以及搖桿等。

Input.GetKey(KeyCode.UpArrow) // 當按下十字鍵上時會回傳true 反之則false

Time

Time類別管理所有時間相關的資訊。

float timeLeft = 30f;

void Update () 
    // 減去每個影格所花費的時間
    timeLeft -= Time.deltaTime;
}

Mathf

Mathf類別可取用內建的許多數學相關函式。

Debug.Log(Mathf.RoundToInt(4.5)); // 四捨五入輸出5
Debug.Log(Mathf.Cos((Mathf.PI / 180) * 90)); // cos90°

Random

被用來產生隨機資料的類別。

// 隨機產生0-99的整數 (不包含100)
Debug.Log(Random.Range(0, 100));
// 隨機產生(0.0-100.0之間的浮點數)
Debug.Log(Random.Range(0.0f, 100.0f));

Quaternion

被用來代表旋轉角度。

// 沒旋轉
Debug.Log(Quaternion.identity); // 輸出(0, 0, 0, 1)=(x, y, z, w)
// 以Z軸為軸心向右旋轉30度
Quaternion r = Quaternion.identity;
r.eularAngles = new Vector3(0, 0, 30);

.Euler()

// 將旋轉的角度轉為向量
// 此案例為向右轉45度的向量
Debug.Log(Quaternion.Euler(0, 45, 0) * Vector3.forward);

Ref: unity3d-forum

.Lerp() & .LookRotation()

// 將方向向量回傳成Quaternion Type後利用線性內插去變化值
// 此案例為將物件向右轉45度
transform.rotation = Quaternion.Lerp (
    transform.rotation,
    Quaternion.LookRotation(Quaternion.Euler (0, 45, 0) * Vector3.forward),
    Time.time
);

Ref: unity3d-forum

Physics

涵蓋物理相關數值及方法。

.RayCastAll()

回傳射線擊中的所有物理實體及訊息。

using System.Linq;
...
Ray ray = new Ray (startPosition, direction);
RaycastHit[] hits = Physics.RaycastAll(ray, rayLength).OrderBy(h=>h.distance).ToArray();

foreach (RaycastHit hit in hits) {
    // 取出的hit訊息將會由近至遠排列
}

Ref: RaycastAll-Returning-Results-In-Reverse-Order

PlayerPrefs

存取使用者自行命名的變數資訊在本地端,可跨場景取用。

// 儲存分數
PlayerPrefs.SetInt("top1", 689);
// 取得分數
Debug.Log(PlayerPrefs.GetInt("top1"));

Resources

被用來存取assets資料夾下的資源。

.LoadAll()

UnityScript

// 把Resources/Skin資料夾下所有的Sprite物件讀出存到skin變數中
var skin = (Sprite)Resources.LoadAll("Skin/");

C Sharp

// 亦可將Multiple Sprite的所有內容存入sprites變數中
Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites");

Ref: tiled-map-tool-with-unity

Application

提供許多靜態變數及函數供開發者存取/控制應用程式。

if(Application.isMobilePlatform); // 在行動裝置下執行
    Applicaiton.LoadLevel("Level1"); // 讀取關卡1
if(Application.isEditor) // 在編輯器下執行
    Application.LoadLevelAdditive("Level1_Advanced"); // 加載物件至關卡1

利用Slider物件展示關卡讀取進度。

Slider loadingBar;
AsyncOperation async;

void LoadLevel (string levelName) {
    StartCoroutine(LoadLevelWithBar(levelName));
}

IEnumerator LoadLevelWithProgressBar (string levelName) {
    async = Application.LoadLevelAsync(levelName);
    while(!async.isDone) {
        loadingBar.value = async.progress;
        yield return null;
    }
}

WWW

透過Web請求存取URL提供的內容資訊。

void Start () {
    StartCoroutine ("Request");
}

IEnumerator Request () {
    var www = new WWW("http://www.google.com.tw");
    while (!www.isDone && www.error == null) { yield return null; }
    Debug.Log (www.text);
}

Yield Instruction

WaitForSeconds()

依據此語法後方所接續的敘述,來決定等待時間。

UnityScript

// 先印出Hello之後過一秒才印出Unity
Debug.Log("Hello, ");
yield WaitForSeconds(1);
Debug.og("Unity!");

C#

// 先印出Hello之後過一秒才印出Unity
void Start() {
    StartCoroutine(HelloUnity());
}

IEnumerator HelloUnity () {
    Debug.Log("Hello, ");
    yield return new WaitForSeconds(1);
    Debug.og("Unity!");
}

MonoBehaviour

InvokeRepeating

在t1秒後執行方法,同時每t2秒後重複執行該方法。

void Start () {
    InvokeRepeating("Test", t1, t2);
}
void Test () {
    Debug.Log("Test!");
}

results matching ""

    No results matching ""