搭建UnityGameFramework框架最低需求项目

参考 https://gitee.com/jiangyin/StarForce

1、下载 Game Framework 包

进入官网的下载页面下载2021.05.31版本 https://gameframework.cn/download/

2、新建Unity项目,然后把包导入

3、新建Editor文件夹,并创建 GameFrameworkConfigs.cs 脚本

GameFrameworkConfigs.cs 这是指定xml配置文件的脚本,可以修改路径

using GameFramework;
using System.IO;
using UnityEngine;
using UnityGameFramework.Editor;
using UnityGameFramework.Editor.ResourceTools;

public static class GameFrameworkConfigs
{
    [BuildSettingsConfigPath]
    public static string BuildSettingsConfig = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameMain/Configs/BuildSettings.xml"));

    [ResourceCollectionConfigPath]
    public static string ResourceCollectionConfig = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameMain/Configs/ResourceCollection.xml"));

    [ResourceEditorConfigPath]
    public static string ResourceEditorConfig = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameMain/Configs/ResourceEditor.xml"));

    [ResourceBuilderConfigPath]
    public static string ResourceBuilderConfig = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameMain/Configs/ResourceBuilder.xml"));
}

4、创建 GameFrameworkConfigs.cs 脚本中提到的4个 xml 文件

BuildSettings.xml DefaultScene是启动场景根据需要修改

<?xml version="1.0" encoding="UTF-8"?>
<UnityGameFramework>
  <BuildSettings>
    <DefaultScenes>
      <DefaultScene Name="Assets/Launcher.unity" />
    </DefaultScenes>
    <SearchScenePaths>
      <SearchScenePath Path="Assets" />
    </SearchScenePaths>
  </BuildSettings>
</UnityGameFramework>

ResourceCollection.xml 是配置AB包要打包的资源。 使用UGF自带的编辑器编辑可能会出现错误,可以将 Resources 和 Assets 中的内容全部删除重新编辑

<?xml version="1.0" encoding="UTF-8"?>
<UnityGameFramework>
  <ResourceCollection>
    <Resources>
      <Resource Name="Prefabs/Cube" LoadType="0" Packed="False" />
      <Resource Name="Prefabs/Panel" LoadType="0" Packed="False" />
      <Resource Name="Scenes/Menu" LoadType="0" Packed="False" />
    </Resources>
    <Assets>
      <Asset Guid="4001e38f08c97ba47a17ee5abcb6a388" ResourceName="Prefabs/Cube" />
      <Asset Guid="80381cb4a7962eb4089d41001ca2218c" ResourceName="Prefabs/Panel" />
      <Asset Guid="c56d6ccf379a228439a15bc3c45518b6" ResourceName="Scenes/Menu" />
    </Assets>
  </ResourceCollection>
</UnityGameFramework>

ResourceEditor.xml 配置资源文件的根目录和哪些文件会出现在 打包的资源编辑器中

<?xml version="1.0" encoding="UTF-8"?>
<UnityGameFramework>
  <ResourceEditor>
    <Settings>
      <SourceAssetRootPath>Assets/GameMain</SourceAssetRootPath>
      <SourceAssetSearchPaths>
        <SourceAssetSearchPath RelativePath="" />
      </SourceAssetSearchPaths>
      <SourceAssetUnionTypeFilter>t:Scene t:Prefab t:Shader t:Model t:Material t:Texture t:AudioClip t:AnimationClip t:AnimatorController t:Font t:TextAsset t:ScriptableObject</SourceAssetUnionTypeFilter>
      <SourceAssetUnionLabelFilter>l:ResourceInclusive</SourceAssetUnionLabelFilter>
      <SourceAssetExceptTypeFilter>t:Script</SourceAssetExceptTypeFilter>
      <SourceAssetExceptLabelFilter>l:ResourceExclusive</SourceAssetExceptLabelFilter>
      <AssetSorter>Path</AssetSorter>
    </Settings>
  </ResourceEditor>
</UnityGameFramework>

ResourceBuilder.xml 配置打包选项,如打包输出目录,一般不需要手动修改

<?xml version="1.0" encoding="UTF-8"?>
<UnityGameFramework>
  <ResourceBuilder>
    <Settings>
      <InternalResourceVersion>22</InternalResourceVersion>
      <Platforms>1</Platforms>
      <AssetBundleCompression>1</AssetBundleCompression>
      <CompressionHelperTypeName>UnityGameFramework.Runtime.DefaultCompressionHelper</CompressionHelperTypeName>
      <AdditionalCompressionSelected>True</AdditionalCompressionSelected>
      <ForceRebuildAssetBundleSelected>False</ForceRebuildAssetBundleSelected>
      <BuildEventHandlerTypeName>
      </BuildEventHandlerTypeName>
      <OutputDirectory>D:/ProjectUnity/UgfPackageTemplate/AB</OutputDirectory>
      <OutputPackageSelected>True</OutputPackageSelected>
      <OutputFullSelected>False</OutputFullSelected>
      <OutputPackedSelected>False</OutputPackedSelected>
    </Settings>
  </ResourceBuilder>
</UnityGameFramework>

5、创建启动场景 Launcher.unity ,将框架的预制体GameFramework拖入场景中,然后编写代码

参考 https://gameframework.cn/tutorial/tutorial-003/ 搭建可以快速访问UGF组件的工具和自定义组件

6、单机模式

单机模式需要调用 GameEntry.Resource.InitResources(); 并成功后才能使用加载资源的方法(如显示UI、实体等)

using UnityEngine;
using GameFramework;
using GameFramework.Event;
using GameFramework.Resource;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

public class ProcedureStart : GameFramework.Procedure.ProcedureBase
{
    bool m_InitResourcesComplete = false;
    protected override void OnEnter(ProcedureOwner procedureOwner)
    {
        base.OnEnter(procedureOwner);
        if (GameEntry.Base.EditorResourceMode)
        {
            // 编辑器模式
            m_InitResourcesComplete = true;
        }
        else if (GameEntry.Resource.ResourceMode == ResourceMode.Package)
        {
            // 单机模式 调用InitResources方法
            GameEntry.Resource.InitResources(OnInitResourcesComplete);
        }
    }
    private void OnInitResourcesComplete()
    {
        m_InitResourcesComplete = true;
    }
    protected override void OnUpdate(ProcedureOwner procedureOwner,float elapseSeconds,float realElapseSeconds)
    {
        base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
        if (m_InitResourcesComplete)
        {
            ChangeState<ProcedureMenu>(procedureOwner);// 资源加载成功后切换流程
        }
    }
}
posted @ 2022-08-17 15:04  溟天有雨  阅读(993)  评论(0)    收藏  举报