GameFrameWork 搭建Space Shooter(二)

这一张我们主要介绍程序初始入口

首先我们模仿GF的范例项目来构建我们需要的文件目录如下所示:
image.png

其中GameFramework文件是导入插件自带的不需要管理:GameMain是我们自己项目需要的各个文件模块,按照这种方式可以让我们的项目一目了然,这个还是比较推荐的,至于StreamingAssets是不适用Editor模式通过AssertsBundle打包后将所有的文件复制到该文件下的运行方式,public是我们打包指定的目录这个其实不建议放在这里,因为产生的文件guid会冲突,建议放在Asserts外,这个是演示项目就不太care了。

其实我们第一步肯定是创建一个场景了,然后将GameFramework自带的Prefabs引入到插件中去:


image.png

可以看到这里有基本上所有的GF相关的配置信息,我们只需要手动配置并将相关脚本与其关联就可以了:
首先是程序入口


image.png

手表点击GemeFramework在


image.png

这是第一个入口

可以看到定义了一个Game Entry脚本的入口,这里EJ大神通过c# partial的方式定义了一个类三个文件夹


image.png

我们可以看下代码,

GameEntry.Bultin.cs 初始化了GF本身所有相关的组件

{


    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 是我们游戏中自己定义的组件也是需要

/// <summary>
/// 游戏入口。
/// </summary>
public partial class GameEntry : MonoBehaviour
{
    public static BuiltinDataComponent BuiltinData
    {
        get;
        private set;
    }

    public static HPBarComponent HPBar
    {
        get;
        private set;
    }

    private static void InitCustomComponents()
    {
        BuiltinData = UnityGameFramework.Runtime.GameEntry.GetComponent<BuiltinDataComponent>();
        HPBar = UnityGameFramework.Runtime.GameEntry.GetComponent<HPBarComponent>();
    }
}

GameEntry.cs 自然就是调用初始化了:

public partial class GameEntry : MonoBehaviour
{
    // Start is called before the first frame update
    private void Start()
    {
        InitBuiltinComponents();
        InitCustomComponents();
    }
}

这三个类我觉得还是比较好的,等于是按需分类这样代码结构清晰明了,不过自定义的组件GameEntry.Custom.cs 是需要先定义的,其实这一步应该等我们把相关的组件全部写好再来做这一步,不过我们先不管了,按照代码结构提前讲了。

第二个入口就是 GameMain-》Script-> ProcedureLaunch

image.png

由于MonoBehaviour 是没有加载顺序的,所以需要在配置中指定执行顺序,否则如果第二个入口先执行,很多组件都没有初始化,会报错的,我们看下Unity如何配置执行顺序


image.png

从Edit->project setting->Script Excute Order配置你需要执行的顺序,我们只要把GameEntry优先级调高就可以了。因为gf只定义了两个入口。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容