游戏制作之路(36)退出游戏循环返回开始菜单

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caimouse/article/details/85542221

在前面已经学习了从游戏开始菜单进入游戏主循环,当玩家玩累了,想退出游戏,或者想重新开始游戏,应该怎么样实现呢?下面就来解决这个问题。

 

首先要在游戏主循环的界面上创建一个退出的按钮,这样玩家才可以随时退出游戏。按前面方法,在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

猜你喜欢

转载自blog.csdn.net/caimouse/article/details/85542221