復(fù)制上一篇文章unity ECS 學(xué)習(xí)筆記 一 子場景以及發(fā)布1 - 簡書 (jianshu.com)
最后的工程開始.
-
添加ENABLE_CONTENT_DELIVERY編譯條件開啟內(nèi)容可下載功能;
ENABLE_CONTENT_DELIVERY -
構(gòu)建一次content
構(gòu)建Content - 部署到http服務(wù)器上;
- 把Test01場景設(shè)置為啟動場景并出包.
- 啟動exe竟然沒有《SUB01》里的內(nèi)容I飞觥!习绢!
無sub01
經(jīng)過一番goggle后發(fā)現(xiàn)開啟ENABLE_CONTENT_DELIVERY需要額外加一行代碼.
Question - How to actually use ENABLE_CONTENT_DELIVERY in build ? - Unity Forum
似乎是官方團(tuán)隊(duì)忘記加到文檔里面說明了
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("下載內(nèi)容"))
{
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>場景內(nèi)的Cube分配一個(gè)綠色的材質(zhì);給<sub02>子場景內(nèi)的球分配紅色的材質(zhì);
- 把<test02>作為啟動場景;重新Build并發(fā)布Content;
-
啟動EXE,默認(rèn)是沒有sub02的;
無Sub02 -
點(diǎn)擊[下載內(nèi)容]按鈕后出現(xiàn)sub02的內(nèi)容:
出現(xiàn)Sub02 -
點(diǎn)擊[加載Sub03]按鈕后出現(xiàn)Sub03的內(nèi)容:
Sub03
到這里,開啟ENABLE_CONTENT_DELIVERY并從網(wǎng)絡(luò)下載內(nèi)容到本地后再加載進(jìn)游戲的流程跑通了.同時(shí)可以觀察到SubScene <Sub02>需要下載完成更新Content后才會顯示.
- 進(jìn)步測試更新content,發(fā)現(xiàn):
- 如果給<Sub02>內(nèi)的球添加新材質(zhì)會報(bào)錯(cuò).
-
而給<sub03>內(nèi)的球添加新材質(zhì)則會成功更新.
Sub03內(nèi)的球變成藍(lán)色了