1. 程式人生 > >遊戲製作之路(36)退出遊戲迴圈返回開始選單

遊戲製作之路(36)退出遊戲迴圈返回開始選單

在前面已經學習了從遊戲開始選單進入遊戲主迴圈,當玩家玩累了,想退出遊戲,或者想重新開始遊戲,應該怎麼樣實現呢?下面就來解決這個問題。

 

首先要在遊戲主迴圈的介面上建立一個退出的按鈕,這樣玩家才可以隨時退出遊戲。按前面方法,在StarGame.cs腳本里新增一個OnGUI()函式,如下:

    void OnGUI()

    {

 

}

然後在腳本里新增一個退出按鈕的樣式變數,如下:

public GUISkin skin;

再新增一個按鈕的位置:

private Rect btnEndRect;

 

再按下面的程式碼來修改指令碼,結果如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarGame : MonoBehaviour
{
    public GameObject starPrefab;
    public GUISkin skin;

    private Rect btnEndRect;

    // Use this for initialization
    void Start()
    {
        btnEndRect = new Rect();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //
            Vector3 pos = Input.mousePosition;
            pos.x /= Screen.width;
            pos.y /= Screen.height;

            //
            GameObject g = (GameObject)Instantiate(starPrefab, pos, Quaternion.identity);            
        }
    }

    void OnGUI()
    {
        btnEndRect.x = 0;
        btnEndRect.y = 0;
        btnEndRect.width = 100;
        btnEndRect.height = 45;
        //
        if (GUI.Button(btnEndRect, "結束", skin.button))
        {
           
        }

    }
}

接著下來要設定腳本里GUISkin skin變數,也就是把GameGUISkin拖到相應的變數裡,如下圖:

如果沒有設定這個變數,就會執行出錯,看不到顯示的“結束”按鈕,正確設定之後,執行如下圖:

這時候可以看到結束按鈕了,但點選之後並沒有響應結束,因為我們還沒有在點選響應的大括號裡新增退出的程式碼,因此把這個響應程式碼修改如下:

        //

        if (GUI.Button(btnEndRect, "結束", skin.button))

        {

            StartMenu menu = gameObject.GetComponent<StartMenu>();

            menu.enabled = true;

            this.enabled = false;

        }

再接著執行之後,可以點選結束按鈕:

上面是返回到開始選單了,但是遊戲裡的內容並沒有清空,還是看到在背景上顯示很多星星,這顯然不是我們希望的結果,那麼怎麼辦呢?怎麼樣才可以恢復到遊戲的初始化狀態呢?好吧,得發揮思考能力了,其實問題就是要清空星星,如果我們可以把所有點選建立的星星都記錄下來,在點選退出時,再把所有星星刪除,不就是恢復到開始狀態了嗎?就按著這個思路來修改指令碼程式碼,首先要新增一個容器來儲存所有星星,但是我們使用什麼容器來儲存呢?使用陣列可以嗎?是不可以的,因為星星有多少個是不確定的,因此要使用動態變長的容器,那麼最簡單的容器就是列表了,如List<T>,最後可以遊戲指令碼程式碼修改如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarGame : MonoBehaviour
{
    public GameObject starPrefab;
    public GUISkin skin;

    private Rect btnEndRect;
    private List<GameObject> starList;

    // Use this for initialization
    void Start()
    {
        btnEndRect = new Rect();
        starList = new List<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //
            Vector3 pos = Input.mousePosition;
            pos.x /= Screen.width;
            pos.y /= Screen.height;

            //
            GameObject g = (GameObject)Instantiate(starPrefab, pos, Quaternion.identity);
            starList.Add(g);
        }
    }

    void OnGUI()
    {
        btnEndRect.x = 0;
        btnEndRect.y = 0;
        btnEndRect.width = 100;
        btnEndRect.height = 45;
        //
        if (GUI.Button(btnEndRect, "結束", skin.button))
        {
            foreach(GameObject g in starList)
            {
                Destroy(g);
            }
            starList.Clear();
            //
            StartMenu menu = gameObject.GetComponent<StartMenu>();
            menu.enabled = true;
            this.enabled = false;
        }

    }
}

經過上面的修改,結束遊戲之後返回開始選單,就可以看到跟開始的狀態是一樣的了,如下圖:

到這裡,就可以實現從遊戲主迴圈裡退出來,並恢復到遊戲開始的狀態。

https://blog.csdn.net/caimouse/article/details/51749579