標籤:als 一個 code 還需要 getc pre ... 檔案夾 play
一、介紹
目的:通過一個簡單的例子(滑鼠點擊,使立方體旋轉和變色)熟悉Unity中C#指令碼的編寫。
軟體環境:Unity 2017.3.0f3 、 VS2013。
二、C#指令碼實現1,啟動Unity,建立遊戲情境。【關於Unity基本操作請點擊 Unity入門教程(上)進行瞭解】
2,在Assets目錄下建立檔案夾,用於存放遊戲的各種資源。3,建立一個名為CubeRotate的C#指令碼並拖放到情境的方塊上,調整好相機位置。4,雙擊開啟指令碼,在指令碼中加入滑鼠相關函數
5,設定一個功能:當滑鼠游標移動到物體上時,物體材質色彩變為黃色。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CubeRotate : MonoBehaviour { private bool bCube1 = false; private bool bCube2 = false; private Color OldColor; // Use this for initialization void Start () { OldColor = this.gameObject.GetComponent<Renderer>().material.color; //擷取物體的原始顏色 } // Update is called once per frame void Update () { } void OnMouseOver() { this.gameObject.GetComponent<Renderer>().material.color = Color.yellow; //立方體變為黃色 bCube1 = true; } void OnMouseExit() { this.gameObject.GetComponent<Renderer>().material.color = OldColor; //立方體變為原始顏色 bCube1 = false; } void OnMouseDown() { if(bCube1) { bCube2 = true; } }}
代碼解讀:當滑鼠游標移動到物體上時,物體變為黃色,同時將一個初始值為false的bCube1的值變為true;當滑鼠游標離開後,物體材質色彩還原,bCube1為false;當按下滑鼠左鍵,且bCube1的值為true,bCube2的值為真。
註:OnMouse函數都是執行一次的函數,因此不能將與動畫有關的控制函數放於其內執行,所以通常會用布爾值開關來控制Update函數中的動畫函數。
6,在Update函數裡實現Cube轉動
void Update () { if(bCube2) //當Cube為真時 { this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200); //Cube轉動 } }
因為Cube轉動是持久性的,所以把旋轉指令碼寫在Update函數裡面實現Cube轉動。
7,更改Spotlight的強度
// Use this for initialization void Start () { OldColor = this.gameObject.GetComponent<Renderer>().material.color; //擷取物體的原始顏色 GameObject.Find("Spotlight").GetComponent<Light>().intensity = 1.0F; //擷取Spotlight的強度屬性 } // Update is called once per frame void Update () { if(bCube2) //當Cube為真時 { this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200); //Cube轉動 if(GameObject.Find("Spotlight").GetComponent<Light>().intensity < 8.0F) { GameObject.Find("Spotlight").GetComponent<Light>().intensity += 0.05F; } } }
8,UGUI的使用->添加Text組件
9,添加控制Text顯示的指令碼
使用UGUI組件必須在C#指令碼中添加UI的命名空間,這樣我們才能引用。當bCube2的值為真時,Text組件顯示“Cube正在旋轉中...”,所以在Update函數的if語句裡面應添加以下指令碼
GameObject.Find("Text").GetComponent<Text>().text = "Cube正在旋轉...";
10,點擊“Play”按鈕,運行遊戲
滑鼠點擊前:
滑鼠點擊後:
11,完整指令碼代碼
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; //添加UI的命名空間public class CubeRotate : MonoBehaviour { private bool bCube1 = false; private bool bCube2 = false; private Color OldColor; // Use this for initialization void Start () { OldColor = this.gameObject.GetComponent<Renderer>().material.color; //擷取物體的原始顏色 GameObject.Find("Spotlight").GetComponent<Light>().intensity = 1.0F; //擷取Spotlight的強度屬性 } // Update is called once per frame void Update () { if(bCube2) //當Cube為真時 { this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200); //Cube轉動 GameObject.Find("Text").GetComponent<Text>().text = "Cube正在旋轉..."; if(GameObject.Find("Spotlight").GetComponent<Light>().intensity < 8.0F) { GameObject.Find("Spotlight").GetComponent<Light>().intensity += 0.05F; } } } void OnMouseOver() { this.gameObject.GetComponent<Renderer>().material.color = Color.yellow; //立方體變為黃色 bCube1 = true; } void OnMouseExit() { this.gameObject.GetComponent<Renderer>().material.color = OldColor; //立方體變為原始顏色 bCube1 = false; } void OnMouseDown() { if(bCube1) { bCube2 = true; } }}
三、總結
通過學習我們瞭解了C#指令碼對於遊戲對象的作用,中間還學習了UGUI的使用。
Unity指令碼語言的綜合應用並不是通過一個執行個體就能夠達到熟練的程度,還需要自己不斷地練習和探索,不斷的嘗試bug和及時總結。
通過C#指令碼實現旋轉的立方體