栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

[2022.3.15]<呆头熊的开发日记>游戏初始化Game类

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[2022.3.15]<呆头熊的开发日记>游戏初始化Game类

游戏内基本都采用MVC模式实现,主要通过SendEvent来接收时间信息,决定下一步要怎么做。 Game类继承于ApplicationBase,搭在游戏第一个场景,启动后自动跳到下一场景,并且在之后场景一直存在。

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

[RequireComponent(typeof(ObjectPool))]
[RequireComponent(typeof(Sound))]
[RequireComponent(typeof(StaticData))]

public class Game : ApplicationBase
{
    //全局访问功能
    [HideInInspector]
    public ObjectPool ObjectPool = null;
    [HideInInspector]
    public Sound Sound = null;
    [HideInInspector]
    public StaticData StaticData = null;  //静态数据


    //全局方法
    public void LoadScene(int level)
    {
        //-----退出前一场景
        //事件参数
        SceneArgs e = new SceneArgs();
        e.Level = SceneManager.GetActiveScene().buildIndex;
        //发送事件
        SendEvent(Const.E_ExitScene, e);

        //-----新场景
        SceneManager.LoadScene(level, LoadSceneMode.Single);
    }

    //当前场景加载完后触发
    void OnLevelWasLoaded(int level)
    {
        Debug.Log("OnLevelWasLoaded" + level);

        SceneArgs e = new SceneArgs();
        e.Level = level;

        SendEvent(Const.E_EnterScene, e);
    }


    //游戏入口
    void Start()
    {
        Object.DontDestroyOnLoad(this.gameObject);
        //全局单例赋值
        ObjectPool = ObjectPool.Instance;
        Sound = Sound.Instance;
        StaticData = StaticData.Instance;

        //注册启动命令
        RegisterController(Const.E_StartUp, typeof(StartUpCommand));

        //启动游戏
        SendEvent(Const.E_StartUp);

        
    }

    
}
其中,启动命令StartUpCommand类继承于Controller类:
public class StartUpCommand : Controller
{
    public override void Execute(object data)
    {
        //注册模型

        //注册命令
        RegisterController(Const.E_EnterScene, typeof(EnterSceneCommand));
        RegisterController(Const.E_ExitScene, typeof(ExitSceneCommand));


        //进入开始界面
        //UnityEngine.SceneManagement.SceneManager.LoadScene(1);
        Game.Instance.LoadScene(1);
        
    }
    
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901950.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号