在 GF 中,调用框架中组件有两种写法,比如在这里我们获取 UI 组件:
csharpUIComponent uiComponent = UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>();
uiComponent.OpenUIForm("",this);
csharpGameEntry.UI.OpenUIForm("",this);
两种方式都可以得到,但明显后者更快捷,GameEntry 的主要作用就是为了更方便的全局调用。
分别创建三个脚本,命名为 GameEntry
,GameEntry.Builtin
,GameEntry.Custom
三个脚本分别代表 GameEntry 类的三个部分,下面来依次讲解他们的作用:
GameEntry
在这里调用组件初始化方法,将内置组件和自定义组件初始化,使用 partial
关键字将 GameEntry 类进行拆分为三个部分
c#using UnityEngine;
using UnityGameFramework.Runtime;
/// <summary>
/// 游戏入口。
/// </summary>
public partial class GameEntry : MonoBehaviour
{
private void Start()
{
//初始化内置组件
InitBuiltinComponents();
//初始化自定义组件
InitCustomComponents();
}
}
GameEntry.Builtin
用于初始化内部组件,将各个组件封装为静态的属性供外部调用,并提供一个初始化方法用于 GameEntry
csharpusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
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>();
Debug.Log(">>>>>>初始化内置组件成功");
}
}
GameEntry.Custom 我们可以在这里扩展实现自己的组件并初始化
csharpusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public partial class GameEntry : MonoBehaviour
{
//自定义组件
//>>>
//初始化自定义组件
private void InitCustomComponents()
{
Debug.Log(">>>>>>初始化自定义组件成功");
}
}
完成脚本的准备后,为 GameFramework 创建一个父物体 GF
,并将 GameEntry
挂载在物体上,启动 Unity
Consolo 窗口输出 log,所有组件初始化成功,至此准备工作完毕,进入游戏流程
本文作者:xuxuxuJS
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!