1.另外新建一個場景:GameScene, 主要是用來製做遊戲內容
經過點擊遊戲菜單中的「進入遊戲」按鈕轉到到這裏。
2.要實現場景鍵的切換,修改SceneController中的update方法,
void Update () {
if (currentGameState == preGameState) {
return;
}
//賦值遊戲狀態
preGameState = currentGameState;
switch(currentGameState)
{
case GameState.GAME_MENU:
break;
case GameState.GAME_START:
Application.LoadLevel("GameScene");
break;
case GameState.GAME_END:
break;
case GameState.GAME_QUIT:
break;
default:
Debug.Log("未知狀態錯誤 state="+currentGameState);
break;
}
}
3.在點擊按鈕「進入遊戲」後,SceneController的currentGameState值就
會修改成GameState.GAME_START,在下次執行update方法的以後,就會
執行到 Application.LoadLevel("gameScene"),這句話是用來切場景。
4.若是這時候執行程序,會出現如下錯誤:
這是由於若是咱們要切換場景,必須先把全部的場景放在build settings裏面:
打開,這時候,雙擊打開主場景:MainScene,而後點擊Add Current,把當前場景添加到正在構建的場景裏面。
5.而後在啓動程序,點擊「進入遊戲」就能夠切換到GameScene場景了。
6.不過這時候的遊戲場景仍是一片空白,接下來就給場景GameScene繪製遊戲地圖。
7.在Hierarchy面板點擊Create,選擇Terrain建立地形:
8.導入Unity3d附帶的地圖資源包:
9.給場景場景太暗,給場景添加一個方向燈:
方向燈:顧名思義,只須要給修改方向,整個場景的光亮就會改變。
10.修改一下地形:
11.給地形增長貼圖:
12.同理,給地形增長一些花草樹木:
這裏要說明一點,若是是的Unity給遊戲渲染作了一些優化,若是鏡頭距離目標較遠的時候給
地形增長草,那麼極可能在場景裏面出現不了,此時把鏡頭拉近的時候,就能夠看到了。
13.接下來是實現以第一人稱來觀看整個場景:
新建一個類:FirstPersonView用於綁定攝像機,實現第一人稱視覺
/**
* 鼠標左鍵GetMouseButton(0)
鼠標右鍵GetMouseButton(1)
鼠標中鍵GetMouseButton(2)
**/
public const int MOUSE_BTN_RIGHT = 1;
//射擊準心貼圖
public Texture2D tex_fire;
// Use this for initialization
void Start () {
//影藏鼠標指針
Screen.showCursor = false;
}
// Update is called once per frame
void Update () {
//按住鼠標右鍵旋轉
if (Input.GetMouseButton(MOUSE_BTN_RIGHT)) {
transform.Rotate (0, Input.GetAxis ("Mouse X"), 0);
}
}
/**
* 把鼠標的指針改爲射擊準星,並設置準心的位置爲鼠標的指針點
*/
void OnGUI()
{
if (tex_fire) {
float x = Input.mousePosition.x - (20 >> 1);
float y = Input.mousePosition.y + 10;
GUI.DrawTexture (new Rect (x, Screen.height - y, 20, 20), tex_fire);
}
} 優化
14.一樣的新建一個RoleMoveController的類綁定在攝像機,這個類主要是用來控制主角的移動:
public class RoleMoveController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.A)) {
this.transform.Translate(Vector3.left*Time.deltaTime*Common.MOVE_SPEED);
}
else if (Input.GetKey (KeyCode.D)) {
this.transform.Translate(Vector3.right*Time.deltaTime*Common.MOVE_SPEED);
}
else if (Input.GetKey (KeyCode.W)) {
this.transform.Translate(Vector3.forward*Time.deltaTime*Common.MOVE_SPEED);
}
else if (Input.GetKey (KeyCode.S)) {
this.transform.Translate(Vector3.back*Time.deltaTime*Common.MOVE_SPEED);
}
}
}
Common類主要是存放一些公共的常量:
public class Common{
//移動速度
public const int MOVE_SPEED = 5;
//旋轉速度
public const int ROTATE_SPEED = 20;
}
運行項目~!