个人主页:@元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏:unityUI专题篇
️
解析:
1.采用单例模式,原因: 单例模式无法被纂改基类,适合当管理者
2.基类提取了子类所有的共性: 即面板的显示和隐藏
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
// 项目:
// 功能: 开始面板的基类(单例模式)
// 创建者:秩沅
//_____________________________________
//-------------------------------------
public class BeginFather<T>: MonoBehaviour where T : class
{
static private T planeFather;
static public T PlaneFather => planeFather;
private void Awake()
{
planeFather = this as T ; //到时被继承时的类型会不同,需加上泛型约束class
}
public virtual void Hidden()
{
this.gameObject.SetActive(false);
}
public virtual void Show()
{
this.gameObject.SetActive(true);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 开始面板类
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BeginPlane : BeginFather<BeginPlane>
{
//获取开始面板中的各个按钮控件
public Button but1;
public Button but2;
public Button but3;
public Button but4;
private void Start()
{
but1.triggerEvent += () => { SceneManager.LoadScene("Start"); }; //点击“开始游戏”
but2.triggerEvent += () => { Hidden(); }; //点击“游戏设置”
but3.triggerEvent += () => { Application.Quit(); }; //点击“退出游戏”
but4.triggerEvent += () => { Hidden(); }; //点击“排行榜”
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 设置面板功能
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class SetPlane : BeginFather<SetPlane>
{
//将功能控件拖拽对应的成员变量中
public Button closeButt;
public Slider sliderMusic;
public Slider sliderSound;
public ToggleM toggleMusic;
public ToggleM toggleSound;
private float toggle1;
private float toggle2;
private void Start()
{
this.Hidden();
//按钮中事件的添加
closeButt.triggerEvent += () => {
this.gameObject.SetActive(false);
BeginPlane.SingleInstance.Show();
};
//滑条中事件的添加
sliderMusic.triggerEvent += (value) =>
{
};
sliderSound.triggerEvent += (value) =>
{
};
//多选框中事件的添加
toggleMusic.triggerEvent += (value) =>
{
};
toggleSound .triggerEvent += (value) =>
{
};
}
}
----------------------
此时开始面板脚本也需要修改
----------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 开始面板类
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BeginPlane : BeginFather<BeginPlane>
{
// 获取开始面板中的各个按钮控件
public Button but1;
public Button but2;
public Button but3;
public Button but4;
private void Start()
{
but1.triggerEvent += () => { SceneManager.LoadScene("Start"); }; //点击“开始游戏”
but2.triggerEvent += () => { Hidden(); SetPlane.SingleInstance.Show(); }; //点击“游戏设置”
but3.triggerEvent += () => { Application.Quit(); }; //点击“退出游戏”
but4.triggerEvent += () => { Hidden(); }; //点击“排行榜”
}
}
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-6.关卡设计
⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!