首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >GameFramework教程✨二、GameEntry,游戏入口

GameFramework教程✨二、GameEntry,游戏入口

作者头像
星河造梦坊官方
发布2024-08-15 19:22:24
发布2024-08-15 19:22:24
35600
代码可运行
举报
运行总次数:0
代码可运行

🟥 GameEntry 的作用

若你看过E大写的《StarForce》工程,应该看到它在调用框架时,是写的 GameEntry.xxx

比如:

代码语言:javascript
代码运行次数:0
运行
复制
GameEntry.UI.OpenUIForm("","", this);

但我们如果在 导入了框架插件的工程 中这样写,会发现这样写会报错。

这是因为E大对框架 API 进行了封装。

若我们用不封装的写法,应该是:

代码语言:javascript
代码运行次数:0
运行
复制
UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>()GameEntry.UI.OpenUIForm("","", this);

那下面我们就来看看E大在《StarForce》中是怎样封装的吧!

我们可以直接把这部分封装的代码,用在我们自己的项目中,便于调用。

🟧 GameEntry 的封装原理

在《StarForce》中,GameEntry分为两部分:

  • 框架自带的GameEntry脚本
  • 我们自己写的两个局部类型GameEntry脚本

局部类型(partial):允许我们将一个类、结构或接口分成几个部分,分别实现在几个不同的.cs文件中。

封装后有你可以:

  • 轻松的书写代码
  • 轻松查找 GameEntry.xxx 的引用,知道谁在哪儿调用了它
  • 代码规范。我们也可以添加自己写的系统组件,将其封装到入口供后续轻松调用。例如《StarForce》的 BuiltinDataComponent 组件,便是继承自 GameFrameworkComponent 自己写的系统组件。

🟨 GameEntry 编写过程

下方脚本建议都放在 Assets/GameMain/Scripts/GameEntry 文件夹下。

1️⃣ GameEntry.Builtin.cs

这个脚本提供了静态的封装API,供我们调用。

直接将下方代码复制到你的工程即可。

代码语言:javascript
代码运行次数:0
运行
复制
using UnityEngine;
using UnityGameFramework.Runtime;

/// <summary>
/// 游戏入口。
/// </summary>
public partial class GameEntry : MonoBehaviour
{
    /// <summary>
    /// 获取游戏基础组件。
    /// </summary>
    public static BaseComponent Base { get; private set; }

    /// <summary>
    /// 获取配置组件。
    /// </summary>
    public static ConfigComponent Config { get; private set; }

    /// <summary>
    /// 获取数据结点组件。
    /// </summary>
    public static DataNodeComponent DataNode { get; private set; }

    /// <summary>
    /// 获取数据表组件。
    /// </summary>
    public static DataTableComponent DataTable { get; private set; }

    /// <summary>
    /// 获取调试组件。
    /// </summary>
    public static DebuggerComponent Debugger { get; private set; }

    /// <summary>
    /// 获取下载组件。
    /// </summary>
    public static DownloadComponent Download { get; private set; }

    /// <summary>
    /// 获取实体组件。
    /// </summary>
    public static EntityComponent Entity { get; private set; }

    /// <summary>
    /// 获取事件组件。
    /// </summary>
    public static EventComponent Event { get; private set; }

    /// <summary>
    /// 获取文件系统组件。
    /// </summary>
    public static FileSystemComponent FileSystem { get; private set; }

    /// <summary>
    /// 获取有限状态机组件。
    /// </summary>
    public static FsmComponent Fsm { get; private set; }

    /// <summary>
    /// 获取本地化组件。
    /// </summary>
    public static LocalizationComponent Localization { get; private set; }

    /// <summary>
    /// 获取网络组件。
    /// </summary>
    public static NetworkComponent Network { get; private set; }

    /// <summary>
    /// 获取对象池组件。
    /// </summary>
    public static ObjectPoolComponent ObjectPool { get; private set; }

    /// <summary>
    /// 获取流程组件。
    /// </summary>
    public static ProcedureComponent Procedure { get; private set; }

    /// <summary>
    /// 获取资源组件。
    /// </summary>
    public static ResourceComponent Resource { get; private set; }

    /// <summary>
    /// 获取场景组件。
    /// </summary>
    public static SceneComponent Scene { get; private set; }

    /// <summary>
    /// 获取配置组件。
    /// </summary>
    public static SettingComponent Setting { get; private set; }

    /// <summary>
    /// 获取声音组件。
    /// </summary>
    public static SoundComponent Sound { get; private set; }

    /// <summary>
    /// 获取界面组件。
    /// </summary>
    public static UIComponent UI { get; private set; }

    /// <summary>
    /// 获取网络组件。
    /// </summary>
    public static WebRequestComponent WebRequest { get; private set; }

    private static void InitBuiltinComponents()
    {
        Base = UnityGameFramework.Runtime.GameEntry.GetComponent<BaseComponent>();
        Config = UnityGameFramework.Runtime.GameEntry.GetComponent<ConfigComponent>();
        DataNode = UnityGameFramework.Runtime.GameEntry.GetComponent<DataNodeComponent>();
        DataTable = UnityGameFramework.Runtime.GameEntry.GetComponent<DataTableComponent>();
        Debugger = UnityGameFramework.Runtime.GameEntry.GetComponent<DebuggerComponent>();
        Download = UnityGameFramework.Runtime.GameEntry.GetComponent<DownloadComponent>();
        Entity = UnityGameFramework.Runtime.GameEntry.GetComponent<EntityComponent>();
        Event = UnityGameFramework.Runtime.GameEntry.GetComponent<EventComponent>();
        FileSystem = UnityGameFramework.Runtime.GameEntry.GetComponent<FileSystemComponent>();
        Fsm = UnityGameFramework.Runtime.GameEntry.GetComponent<FsmComponent>();
        Localization = UnityGameFramework.Runtime.GameEntry.GetComponent<LocalizationComponent>();
        Network = UnityGameFramework.Runtime.GameEntry.GetComponent<NetworkComponent>();
        ObjectPool = UnityGameFramework.Runtime.GameEntry.GetComponent<ObjectPoolComponent>();
        Procedure = UnityGameFramework.Runtime.GameEntry.GetComponent<ProcedureComponent>();
        Resource = UnityGameFramework.Runtime.GameEntry.GetComponent<ResourceComponent>();
        Scene = UnityGameFramework.Runtime.GameEntry.GetComponent<SceneComponent>();
        Setting = UnityGameFramework.Runtime.GameEntry.GetComponent<SettingComponent>();
        Sound = UnityGameFramework.Runtime.GameEntry.GetComponent<SoundComponent>();
        UI = UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>();
        WebRequest = UnityGameFramework.Runtime.GameEntry.GetComponent<WebRequestComponent>();
    }
}
2️⃣ GameEntry.cs

这个脚本挂载在 GameFramework 预制体上,

InitBuiltinComponents 完成了初始化上面API的功能。

代码语言:javascript
代码运行次数:0
运行
复制
using UnityEngine;
 
/// <summary>
/// 游戏入口。
/// </summary>
public partial class GameEntry : MonoBehaviour
{
    private void Awake()
    {
        InitBuiltinComponents();
    }
}

大家还有什么问题,欢迎在下方留言!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-12-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🟥 GameEntry 的作用
  • 🟧 GameEntry 的封装原理
  • 🟨 GameEntry 编写过程
    • 1️⃣ GameEntry.Builtin.cs
    • 2️⃣ GameEntry.cs
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档