首先 ,给你的游戏 新建一个scence
然后新建一个ui并且在里面新建一个panel并将它的透明度调为最低
并且再它里面新建 按钮 play 和 quit
然后新建一个脚本 mainmenu 挂到canvas里面
然后新建两个 public函数 playegame 和 quitgame 如图
(其中 用到using UnityEngine.SceneManagement;)
并且在文件 -》 生成设置-》
然后将 ui 放进 runtime 下面的小框里面
这样就能在右面的功能里面找到 之前做好的的游戏开始脚本 和 退出脚本
然后两个按钮分别 选择开始功能 和 退出 功能即可
如图 选择以后就能实现按按钮开始和结束游戏了
效果如视频
按钮实验
这时只能用鼠标来控制按钮的触发但是键盘并不可以所以
点击事件系统(当你建立UI的panel后自动产生的) 将首个选择项 设置为 游戏开始按钮。
这个时候虽然可以用键盘 控制了 但刚开始必须拿鼠标碰一下
所以我们在添加一个脚本。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class initButton : MonoBehaviour { // Start is called before the first frame update private GameObject lastSelect; void Start() { lastSelect = new GameObject(); } // Update is called once per frame void Update() { if (EventSystem.current.currentSelectedGameObject == null) { EventSystem.current.SetSelectedGameObject(lastSelect); } else { lastSelect = EventSystem.current.currentSelectedGameObject; } }
如图 : 它的意思是 这个panel的事件系统当前选择的物体(就是指之前建立的按钮)
如果什么都没选中那么 当前选中的就为上一次选中的。
如果当前有选中那么就储存本次选中的内容等下次什么也没选中的时候使用
这样就能实现即使鼠标移开了依然可以选择 开始游戏还是结束游戏。
以后可能还增加其他功能到时候再写