美文网首页
unity ECS 学习笔记 一 子场景以及发布2

unity ECS 学习笔记 一 子场景以及发布2

作者: 灰的狼 | 来源:发表于2023-12-26 12:02 被阅读0次

复制上一篇文章unity ECS 学习笔记 一 子场景以及发布1 - 简书 (jianshu.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内的球变成蓝色了

相关文章

网友评论

      本文标题:unity ECS 学习笔记 一 子场景以及发布2

      本文链接:https://www.haomeiwen.com/subject/egyzgdtx.html