thanks for the suggestion it seems I cant acces it in the editor so i cant put my Prefabs in there… See the picture.
Any suggestion?
And it gives an error :
NullReferenceException: Object reference not set to an instance of an object
Unity.Entities.Hybrid.BufferElementAuthoring`2[TBufferElementData,TWrappedValue].Convert (Unity.Entities.Entity entity, Unity.Entities.EntityManager destinationManager, GameObjectConversionSystem _) (at
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
public class PrefabsHolder : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
{
public GameObject[] prefabs;
public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
{
referencedPrefabs.AddRange(prefabs);
}
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var buffer = dstManager.AddBuffer<EntityGamePrefabs>(entity);
foreach (var prefab in prefabs)
{
buffer.Add(new EntityGamePrefabs { EntitiesPrefabs = conversionSystem.GetPrimaryEntity(prefab) });
}
}
}
Buffer:
using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
[GenerateAuthoringComponent]
public struct EntityGamePrefabs : IBufferElementData
{
public Entity EntitiesPrefabs;
}
Hmm… seems like a bug with when using “Entity” as the field, if you replace it with other serializable type (like int or float) it works just fine.
This solution no longer works unfortunately. Trying to get a singleton by IBufferElementData elements no longer works.
I instead added a “PrefabCollectionTag” to the Gameobject and .GetSingleton()
EDIT: The buffer never gets added to the entity anymore. Using this approach in a Sub Scene does not work. "ArgumentException: A component with type:EntityGamePrefabs has not been added to the entity." - when trying to access the buffer on the entity
**EDIT 2: For anyone else trying to figure out how to spawn prefabs programmatically in ECS and are shocked to find there are 0 examples of how to do this with Sub Scenes here is a working example: https://forum.unity.com/threads/prefab-workflow-and-managing.968299/#post-6675886 **