GameFramework02_GameEntry
GameEntry类的作用
在GF中,在任意一处调用框架中的某个组件有两种写法,比如在这里我们获取UI组件:
- 通过Runtime获取,不封装
UIComponent uiComponent = UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>();
uiComponent.OpenUIForm("",this);
- 封装为静态类,全局调用,无需间接获取
GameEntry.UI.OpenUIForm("",this);
虽然两种方式都可以使用到GF的组件,通过对比,很明显后者更方便快捷,我们可以在代码任意一处使用组件,并查看定位到调用的具体位置。如何实现?可以将GameEntry作为一个静态类,将GF中的各个组件封装为静态的属性,以实现第二种方式的调用,这是作者的推荐写法。
代码实现
- 创建一个脚本,命名为
GameEntry
,并将下内容复制粘贴,替换掉Unity生成的默认模板
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>();
}
}
- 组件初始化
在代码的末尾处提供了一个方法用InitBuiltinComponents()
来初始化这些组件,我们在Start方法中调用,写在脚本上方
private void Start()
{
// 初始化内置组件
InitBuiltinComponents();
}
- 将
GameEntry
脚本挂载在GameFramework预制体上,完成组件初始化的步骤,这样我们就可以在其他脚本中以GameEntry.xxx(组件).xxx(组件方法)
这样的方式来使用了
本章节到此结束 😋😋😋
下一章节内容 : Procedure流程