Sometimes in the editor, dynamic buffers can be added normally, but during runtime, the dynamic buffer of the entity baked from the Subscene is empty.
I don’t know where the problem lies
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
using System.IO;
using System;
using Unity.Entities.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Animism.Prefab
{
[ExecuteInEditMode]
class A_AllScene : MonoBehaviour
{
public List<SceneAsset> Prefabs = new();
#if UNITY_EDITOR
public void Update()
{
DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath + "/Scenes/");
Prefabs.Clear();
foreach (var info in directoryInfo.GetFiles("*", SearchOption.AllDirectories))
{
if (info.Extension == ".unity")
{
var span = info.FullName.AsSpan(Application.dataPath.Length);
var obj = AssetDatabase.LoadAssetAtPath("Assets/" + span.ToString(), typeof(SceneAsset));
var sceneAsset = obj as SceneAsset;
if (sceneAsset)
{
Prefabs.Add(sceneAsset);
}
}
}
}
#endif
}
class A_AllSceneBaker : Baker<A_AllScene>
{
public override void Bake(A_AllScene authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
var buffer = AddBuffer<C_AllScene>(entity);
buffer.Clear();
foreach (var scene in authoring.Prefabs)
{
if (scene)
{
Debug.Log(scene.name);
C_AllScene allScene = new()
{
Name = scene.name,
Scene = new EntitySceneReference(scene),
};
buffer.Add(allScene);
}
}
}
}
}
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Serialization;
namespace Animism.Prefab
{
[InternalBufferCapacity(0)]
public struct C_AllScene : IBufferElementData
{
public FixedString64Bytes Name;
public EntitySceneReference Scene;
}
}