Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Entity Component System' started by uknowunity, Aug 3, 2020.

  1. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    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:

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


    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. [GenerateAuthoringComponent]
    4. public struct EntityGamePrefabs : IComponentData
    5. {
    6.     public Entity EntitiesPrefabs;
    7. }
     

    Attached Files:

  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    Code (CSharp):
    1.     using Unity.Entities;
    2.  
    3.     [GenerateAuthoringComponent]
    4.     public struct EntityGamePrefab : IBufferElementData
    5.     {
    6.         public Entity EntityPrefab;
    7.     }
    8.  
     
    florianhanke likes this.
  3. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    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?



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

    And it gives an error :


    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Unity.Entities.Hybrid.BufferElementAuthoring`2[TBufferElementData,TWrappedValue].Convert (Unity.Entities.Entity entity, Unity.Entities.EntityManager destinationManager, GameObjectConversionSystem _) (at
     
  4. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    Found an solution from this talk:
    timestamp 23:28

    Monoclass script :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Entities;
    4. using UnityEngine;
    5.  
    6. public class PrefabsHolder : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
    7. {
    8.     public GameObject[] prefabs;
    9.  
    10.     public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    11.     {
    12.         referencedPrefabs.AddRange(prefabs);
    13.     }
    14.  
    15.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    16.     {
    17.         var buffer = dstManager.AddBuffer<EntityGamePrefabs>(entity);
    18.  
    19.         foreach (var prefab in prefabs)
    20.         {
    21.             buffer.Add(new EntityGamePrefabs { EntitiesPrefabs = conversionSystem.GetPrimaryEntity(prefab) });
    22.         }
    23.     }
    24. }
    25.  
    26.  
    27.  
    Buffer:

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using Unity.Entities;
    5. using UnityEngine;
    6.  
    7. [GenerateAuthoringComponent]
    8. public struct EntityGamePrefabs : IBufferElementData
    9. {
    10.     public Entity EntitiesPrefabs;
    11. }
    12.  
    Acces buffer:

    Code (CSharp):
    1. EntityQuery m_Query = GetEntityQuery(typeof(EntityGamePrefabs));
    2. entityWithPrefabs = m_Query.GetSingletonEntity();
    3. DynamicBuffer<EntityGamePrefabs> GamePrefabs = entityManager.GetBuffer<EntityGamePrefabs>(entityWithPrefabs);
    4.  
    This is an working solution but i hope someday GenerateAuthoringComponent will work with dynamic buffers..
     
  5. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    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.
     
  6. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    Ok thanks! Where can I report an bug?
     
  7. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    The usual Bug Reporter from Unity
     
  8. joepl

    joepl

    Unity Technologies

    Joined:
    Jul 6, 2017
    Posts:
    85
    We don't currently support this but we should. Added a ticket to add it.
     
  9. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    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<PrefabCollectionTag>()

    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
     
    Last edited: Dec 31, 2020