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 Instantiate Prefab in Entities 1.0.0

Discussion in 'Entity Component System' started by Zimaell, Mar 6, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    I'm studying Entities 1.0.0 and stopped at the spawn of an object on the stage, I just can't understand even from examples, I get confused when I try to write it myself.
    i created prefab and spawner components, i made prefabs out of them
    Prefab
    Code (CSharp):
    1.  
    2. public struct Character_DataTransform : IComponentData{
    3.     public float3 Position;
    4.     public float3 Rotation;
    5.     public float3 Scale;
    6.     }
    7.  
    8. public class CharacterPrefab : MonoBehaviour{
    9.     public float3 Position;
    10.     public float3 Rotation;
    11.     public float3 Scale;
    12.     }
    13.  
    14. public class CharacterPrefab_Baker : Baker<CharacterPrefab>{
    15.     public override void Bake(CharacterPrefab authoring){
    16.         AddComponent<Character_DataTransform>();
    17.         Debug.Log("CharacterPrefab_Baker is called");
    18.         }
    19.     }
    Spawner
    Code (CSharp):
    1. public struct CharacterSpawner_Component : IComponentData{
    2.     public Entity SpawnPrefab;
    3.     public float3 SpawnPosition;
    4.     }
    5.  
    6. public class CharacterSpawner : MonoBehaviour{
    7.     public GameObject SpawnPrefab;
    8.     public float3 SpawnPosition;
    9.     }
    10.  
    11. public class CharacterSpawner_Baker : Baker<CharacterSpawner>{
    12.     public override void Bake(CharacterSpawner authoring){
    13.         CharacterSpawner_Component sc = default;
    14.         sc.SpawnPrefab = GetEntity(authoring.SpawnPrefab);
    15.         sc.SpawnPosition = authoring.SpawnPosition;
    16.         AddComponent(sc);
    17.         }
    18.     }
    System?
    How do I spawn an object at the point I need?
    (confused with examples and can't recreate...)
     
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Have look at my tutorial.
    And let me know if you still can't make it.

     
    erenaydin, Neiist and Zimaell like this.
  3. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    I watched this video and tried the script itself from this video, the script works, but I got confused in it while studying it...
    I need to understand the principle of the object spawn itself, I still don’t understand why it is created in the update, it is that each frame is created and deleted. can't find a clear description about it...
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    You should look into helo cube samples of Unity ECS Samples at github.
    There is tons of resources to study for someone who is just starting off.
     
    Zimaell likes this.
  5. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Your have a spawner entity that holds a prefab entity reference in a component.

    The system loops over every spawner and spawns the prefab entity.
    The it destroys the spawner, or remove the component that contains the prefab reference, so that in the next frame that same spawner is no longer spawn a new prefab instance.

    Does that make sens ?
     
    Zimaell likes this.
  6. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    I seem to do everything as in the example, but I get an error
    Code (CSharp):
    1.  
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Mathematics;
    6. using Unity.Transforms;
    7. [BurstCompile]
    8. public partial struct SystemSpawn : ISystem{
    9.  
    10.         EntityQuery SpawnQuery;
    11.         uint counter;
    12.  
    13.         [BurstCompile]
    14.         public void OnCreate(ref SystemState state){
    15.             state.RequireForUpdate<Execute>();
    16.             state.RequireForUpdate<CharacterSpawner>();
    17.             var q = new EntityQueryBuilder(Allocator.Temp);
    18.             q.WithAll<CharacterSpawner_Component>();
    19.             SpawnQuery = state.GetEntityQuery(q);
    20.             }
    21.  
    22.         [BurstCompile]
    23.         public void OnDestroy(ref SystemState state){}
    24.  
    25.         [BurstCompile]
    26.         public void OnUpdate(ref SystemState state){
    27.             if(SpawnQuery.IsEmpty){
    28.                 var spawnPrefab = SystemAPI.GetSingleton<CharacterSpawner>().SpawnPrefab;
    29. ^^^^^^ here error

    Code (CSharp):
    1. Assets\Scripts\SystemSpawn.cs(28,45): error CS8377: The type 'CharacterSpawner' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'SystemAPI.GetSingleton<T>()'
    I don't understand what's wrong?
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    IsbspawnerPrefab an Entity?
    Do you have any nested classes in the CharacterSpawner component?
    Can you show us CharacterSpawner?

    Also, did you study Unity samples?
     
    FederalRazer89 and Zimaell like this.
  8. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Shouldn't your singleton be CharacterSpawner_Component ?

    The error message tells you that the type you pass in your getsingleton is not a struct.
    Your CharacterSpawner is a class that you use to author the data in the editor.

    But at runtime you should use you baked entity data so the CharacterSpawner_Component.
     
    Zimaell likes this.
  9. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    Component... I understand, I'm still confused in these concepts ... thanks
     
  10. OUTTAHERE

    OUTTAHERE

    Joined:
    Sep 23, 2013
    Posts:
    656
    I have a related problem to this - what do I need to do with the Entities I get as prefabs? They have the wrong numbers. (like, 20:1, 21:1, but at runtime, the actual prefab entity is 180:1, etc.)

    How do I translate these "baking time" entity numbers to the runtime ones?

    I was using a FixedList<Entity> to store a short list of prefab entities on my spawner component.

    EDIT: Solved, seems like you HAVE to put them into a DynamicBuffer or a naked Entity in an IComponentData.
     
    Last edited: Mar 8, 2023
  11. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Yes entity remapping is only supported by IComponentData and IBufferElementData
     
  12. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Sid enote: You can have Entities inside struct fields inside struct fields inside ICDs. The rule is that it has to be an Entity field directly within the struct. FixedLists are actually just a bunch of byte fields which get type-punned which is why FixedList<Entity> doesn't work.
     
    bb8_1 likes this.
  13. Simon-O

    Simon-O

    Joined:
    Jan 22, 2014
    Posts:
    51
    I appreciate the amount of effort that goes into these, so please don't think I'm criticising, but to offer a perspective... I'd pay real money for the same information in a simple html format somewhere, with screenshots and snippets where appropriate.

    Scrubbing backwards and forward through a video (where it's nigh impossible to "skim" audio) is time consuming. It's a bad medium for random access, especially when all you're looking for is a particular technique or code snippet and don't particularly care about the preamble.
     
  14. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Thanks for the feedback.
    Glad to hear you would be willing to financially support my efforts.
    In the mean time, all the code is available in git hub. Each video has 2 links, one to the starting repo and one to the ending repo.
    So you can have a look through the code or download the project for yourself.
    Some of the code as also been update to reflect changes to the package or in some case include some minor fixes.

    I have been considering working on a readme page for the main repo with description of each episode, link to the video and correspondent branches and few key words to help searching for future reference.

    Video and text format both have benefits and drawback. Videos are a pain to keep up to date but they provide a much wider reach (lot of people start searching on YouTube before they search on git). On the other hand having the code in git allows for update.

    I'm trying to provide the best of both worlds while keeping it sustainable for me.

    Hope you'll still enjoy the other episodes of the series and that the availability of the code in git will be enough to compensate the pain of watching the video several times or having to scrub through it. Let me know if you have other feedback.
     
    Neiist likes this.