复制上一篇文章unity ECS 学习笔记 一 子场景以及发布1 - 简书 (jianshu.com)
最后的工程开始.
-
添加ENABLE_CONTENT_DELIVERY编译条件开启内容可下载功能;
ENABLE_CONTENT_DELIVERY -
构建一次content
构建Content - 部署到http服务器上;
- 把Test01场景设置为启动场景并出包.
- 启动exe竟然没有《SUB01》里的内容!!!
无sub01
经过一番goggle后发现开启ENABLE_CONTENT_DELIVERY需要额外加一行代码.
Question - How to actually use ENABLE_CONTENT_DELIVERY in build ? - Unity Forum
似乎是官方团队忘记加到文档里面说明了
Changelog | Entities | 1.0.16 (unity3d.com)
RuntimeContentSystem.LoadContentCatalog(remoteUrlRoot, Application.persistentDataPath + "/content-cache",
initialContentSet);
以Test02作为完整演示:
- 修改Test02的代码为如下:
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Content;
using Unity.Scenes;
using UnityEngine;
namespace Assets.Scripts.Test02
{
/// <summary>
/// 测试使用SceneSystem.LoadSceneAsync加载EntitySceneReference弱引用的场景(Entity场景)
/// </summary>
public class Test02 : MonoBehaviour
{
public string remoteUrlRoot;
public string initialContentSet;
string _msg = string.Empty;
private void OnGUI()
{
GUILayout.Label(_msg);
GUILayout.Space(30);
if (GUILayout.Button("下载内容"))
{
DownloadContent();
}
if (GUILayout.Button("加载Sub03"))
{
LoadSub03();
}
}
void LoadSub03()
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var builder = new EntityQueryBuilder(Allocator.Temp);
builder.WithAll<SimpleSceneRefCom>();
var query = builder.Build(entityManager);
foreach (var entity in query.ToEntityArray(Allocator.Temp))
{
Debug.Log($"==LoadSub03=={entity}");
var com = entityManager.GetComponentData<SimpleSceneRefCom>(entity);
SceneSystem.LoadSceneAsync(World.DefaultGameObjectInjectionWorld.Unmanaged, com.SceneRef);
}
}
void DownloadContent()
{
_msg = "下载......";
//ContentDeliveryGlobalState.LogFunc = (log)=>_msg = log;
RuntimeContentSystem.LoadContentCatalog(remoteUrlRoot, Application.persistentDataPath + "/content-cache",
initialContentSet);
ContentDeliveryGlobalState.Initialize(remoteUrlRoot, Application.persistentDataPath + "/content-cache", initialContentSet, s =>
{
if (s >= ContentDeliveryGlobalState.ContentUpdateState.ContentReady)
{
DownloadCompleted();
}
else
{
_msg = s.ToString();
}
});
}
void DownloadCompleted()
{
_msg = "下载完成了";
}
}
}
- 为了使演示看起来更清晰,把<test02>场景内的Cube分配一个绿色的材质;给<sub02>子场景内的球分配红色的材质;
- 把<test02>作为启动场景;重新Build并发布Content;
-
启动EXE,默认是没有sub02的;
无Sub02 -
点击[下载内容]按钮后出现sub02的内容:
出现Sub02 -
点击[加载Sub03]按钮后出现Sub03的内容:
Sub03
到这里,开启ENABLE_CONTENT_DELIVERY并从网络下载内容到本地后再加载进游戏的流程跑通了.同时可以观察到SubScene <Sub02>需要下载完成更新Content后才会显示.
- 进步测试更新content,发现:
- 如果给<Sub02>内的球添加新材质会报错.
-
而给<sub03>内的球添加新材质则会成功更新.
Sub03内的球变成蓝色了