若你看过E大写的《StarForce》工程,应该看到它在调用框架时,是写的 GameEntry.xxx
比如:
GameEntry.UI.OpenUIForm("","", this);
但我们如果在 导入了框架插件的工程 中这样写,会发现这样写会报错。
这是因为E大对框架 API 进行了封装。
若我们用不封装的写法,应该是:
UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>()GameEntry.UI.OpenUIForm("","", this);
那下面我们就来看看E大在《StarForce》中是怎样封装的吧!
我们可以直接把这部分封装的代码,用在我们自己的项目中,便于调用。
在《StarForce》中,GameEntry分为两部分:
局部类型(partial):允许我们将一个类、结构或接口分成几个部分,分别实现在几个不同的.cs文件中。
封装后有你可以:
下方脚本建议都放在 Assets/GameMain/Scripts/GameEntry 文件夹下。
这个脚本提供了静态的封装API,供我们调用。
直接将下方代码复制到你的工程即可。
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>();
}
}
这个脚本挂载在 GameFramework 预制体上,
InitBuiltinComponents 完成了初始化上面API的功能。
using UnityEngine;
/// <summary>
/// 游戏入口。
/// </summary>
public partial class GameEntry : MonoBehaviour
{
private void Awake()
{
InitBuiltinComponents();
}
}
大家还有什么问题,欢迎在下方留言!