List of prefabs with access in editor (with GenerateAuthoringComponent?)

Hey all!

Im trying to make an list (or array) of prefabs in editor so i can acces the enitities in runtime with DOTS/ECS.

It worked with one enitity but now im trying to make an list of all the prefabs I have.

Any solutions?

This is my working solution now with one entity:

6163146--674115--upload_2020-8-3_14-39-46.png

using Unity.Entities;

[GenerateAuthoringComponent]
public struct EntityGamePrefabs : IComponentData
{
    public Entity EntitiesPrefabs;
}

6163146--674115--upload_2020-8-3_14-39-46.png

    using Unity.Entities;
  
    [GenerateAuthoringComponent]
    public struct EntityGamePrefab : IBufferElementData
    {
        public Entity EntityPrefab;
    }
1 Like

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?

6167669--674792--upload_2020-8-4_15-55-12.png

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

Found an solution from this talk:

timestamp 23:28

Monoclass script :

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;
}

Acces buffer:

EntityQuery m_Query = GetEntityQuery(typeof(EntityGamePrefabs));
entityWithPrefabs = m_Query.GetSingletonEntity();
DynamicBuffer<EntityGamePrefabs> GamePrefabs = entityManager.GetBuffer<EntityGamePrefabs>(entityWithPrefabs);

This is an working solution but i hope someday GenerateAuthoringComponent will work with dynamic buffers…

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.

Ok thanks! Where can I report an bug?

The usual Bug Reporter from Unity

We don’t currently support this but we should. Added a ticket to add it.

4 Likes

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 **