游戏中GameOver菜单是必不可少的游戏部分,今天以游戏SunnyLand来教教大伙怎么制作菜单
游戏地址Sunny Land | 2D Characters | Unity Asset StoreElevate your workflow with the Sunny Land asset from Ansimuz. Find this & more Characters on the Unity Asset Store.https://assetstore.unity.com/packages/2d/characters/sunny-land-103349我的上一篇文章
CSDNhttps://mp.csdn.net/mp_blog/creation/editor/122727732
简简单单做一下GAMEOVER菜单,首先需要在Canvas中创建一张铺满整个屏幕的Image,在它的Sprite组件添加一张你想要的照片,然后创建一个Text两个Button,并在Button下添加Text-MeshPro
接着在Canvas下添加一个C#脚本就叫GameOverMenu
内容如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class GameOverMenu : MonoBehaviour { public GameObject GameOverImage; public void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); GameOverImage.SetActive(false); } public void MainMenu() { SceneManager.LoadScene(0); GameOverImage.SetActive(false); } } 我们要引用命名空间叫 using UnityEngine.SceneManagement;
还有SceneManager.LoadScene()方法,参数是场景名或者编号,编号可以在界面左上角File——BuildSettings查看,
SceneManager.GetActiveScene().name是获取当前场景,接着再给两个Button添加点击事件,一个
顺便我讲一下如何设置角色死亡,这里以掉下场景外为例。
在Grid表格中创建一个TileMap叫BackGround。用Tile Pattle铺设好后添加组件TileMap Collider2D并勾选isTrigger,表明是触发器。接着创建一个空对象叫DeadLine,添加组件BoxCollider2D 勾选isTrigger,并创建一个叫DeadLine的脚本
内容如下
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DeadLine : MonoBehaviour { public GameObject GameOverImage; private void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag("Player")) { GameOverImage.SetActive(true); } } } 别忘了把GameOVER的图像拖进来
效果:
点击Restart以后,MainMenu我下一期文章再展示。
学习产出:
学会SceneManager来加载游戏场景,用触发器判断玩家是否死亡