目录
前言:
很久以前就已经听说过这个框架了(Game Framework | 基于 Unity 引擎的游戏框架),看过官方的文档和一丢丢教程(就硬不更呗),也看过很多 视频教程、博客等,但大多数都只停留在代码分析,很少有详细的上手实践教学。于是经常会这样,看完后发现自己看的时候好像理解了,但是上手用的时候却一片茫然。
所以我决定以实践促学,上手用GameFrameWork框架实现一个简单的较为完整的项目,并且把这个过程给记录下来。
本次实践的主要内容是将框架应用到我们团队之前做的一个游戏《汝影》中去(Unity版本:2021.3.1f1c1)。整个过程的主要参照的是官方的案例《StarForce》(GitHub - EllanJiang/StarForce: This is a demo made with Game Framework.)中间遇到的问题我都会通过阅读源码、查询各种文档和教程等方法解决。由于个人能力有限,有错误敬请指出。
那么,游戏开始!
一、创建项目,引入框架
这部分内容很简单,官网和各种教程中都有详细的方案。这里简单记录一下。
首先在官网下载最新的框架插件:
创建项目,创建文件夹GameMain用来装游戏内容然后将下载的框架拖进来:
至此游戏框架的安装就好了:
运行Example场景测试:
没有问题:
二、游戏入口
框架安装好后,就是创建游戏入口GameEntry(学的StarForce的做法)。
它的作用实际上是存储管理所有的框架模块(包括内置的和自定义的)组件的引用,方便全局调用使用。(实际上框架内部有一个GameEntry和GameFrameworkEntry,个人感觉这里自己创建的GameEntry是在框架和游戏主体之间的一步解耦)。
创建Scripts文件夹:
在其中创建Base文件夹:
在其中分别创建GameEntry,GameEntry.Builtin,GameEntry.Custom三个脚本,分别管理GameEntry类的三个部分(字面识意):
三个脚本的代码如下:
//GameEntry.cs
using UnityEngine;
using UnityGameFramework.Runtime;
namespace ShadowU
{
public partial class GameEntry : MonoBehaviour
{
private void Start()
{
InitBuiltinComponents();
InitCustomComponents();
}
}
}
//GameEntry.Builtin.cs
using UnityEngine;
using UnityGameFramework.Runtime;
namespace ShadowU
{
/// <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>();
}
}
}
//GameEntry.Custom.cs
using UnityEngine;
using UnityGameFramework.Runtime;
namespace ShadowU
{
/// <summary>
/// 用户自定义模块
/// </summary>
public partial class GameEntry : MonoBehaviour
{
private void InitCustomComponents()
{
}
}
}
(Builtin的部分的代码完全可以copy因为基本上都是死的,然后Custom里面的自己对照着扩展就行)
然后创建场景文件夹和创建ShadowULauncher场景 作为启动场景,并且添加空物体FrameWork并将GameEntry挂在上面,同时将GameFrameWork预制体设为其子物体:
至此,基本准备已经完毕,下面开始创建流程。