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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't see 2D prefab spawn, but it shows entities spawn.

Discussion in 'Entity Component System' started by mbennett5, Nov 27, 2019.

  1. mbennett5

    mbennett5

    Joined:
    Oct 9, 2018
    Posts:
    4
    Entities show in the entity debugger, but won't show in the game.

    Code (CSharp):
    1. public struct GunData : IComponentData
    2. {
    3. //    public float GunPivotX;
    4. //    public float GunPivotY;
    5.     public Entity Prefab;
    6. //    public float secondsBetweenSpawns;
    7. //    public float secondsToNextSpawn;
    8. }
    9.  
    Code (CSharp):
    1. public class PlayerAuthoring : MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
    2. {
    3.     public Entity playerEntity;
    4.     public Transform GunPivot;
    5.     public GameObject Bullet;
    6.     private EntityManager em;
    7.     [SerializeField] private float spawnRate;
    8.  
    9.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    10.     {
    11.         em = dstManager;
    12.         em.AddComponentData(entity, new PlayerInputData { Move = new float2(0, 0), Shoot = 0 });
    13.         em.AddComponentData(entity, new PlayerData { IsJumping = false });
    14.         em.AddComponentData(entity, new GunData
    15.         {
    16.             Prefab = conversionSystem.GetPrimaryEntity(Bullet),
    17. //            GunPivotX = GunPivot.position.x,
    18. //            GunPivotY = GunPivot.position.y,
    19. //            secondsBetweenSpawns = 1/spawnRate,
    20. //            secondsBetweenSpawns = 1,
    21. //            secondsToNextSpawn = 0f
    22.         });
    23.      
    24.         playerEntity = entity;
    25.     }
    26.  
    27.     private void OnCollisionEnter2D(Collision2D other)
    28.     {
    29.         if (other.gameObject.name == "Floor")
    30.         {
    31.             em.SetComponentData(playerEntity, new PlayerData { IsJumping = false });
    32.         }
    33.     }
    34.  
    35.     public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    36.     {
    37.         referencedPrefabs.Add(Bullet);
    38.     }
    39. }
    40.  
    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Mathematics;
    6. using Unity.Transforms;
    7. using UnityEngine;
    8. using UnityEngine.EventSystems;
    9. public class PlayerShootingSystem : JobComponentSystem
    10. {
    11.     private EndInitializationEntityCommandBufferSystem _bufferSystem;
    12.     protected override void OnCreate()
    13.     {
    14.         _bufferSystem = World.GetOrCreateSystem<EndInitializationEntityCommandBufferSystem>();
    15.     }
    16.     private struct PlayerShootingJob : IJobForEachWithEntity<GunData, PlayerInputData, LocalToWorld>
    17.     {
    18.         private EntityCommandBuffer.Concurrent _concurrent;
    19.         private readonly float _deltaTime;
    20.         public PlayerShootingJob(EntityCommandBuffer.Concurrent concurrent, float deltaTime)
    21.         {
    22.             _concurrent = concurrent;
    23.             _deltaTime = deltaTime;
    24.         }
    25.          
    26.         public void Execute(Entity entity, int index, ref GunData gunData, ref PlayerInputData playerInputData, ref LocalToWorld localToWorld)
    27.         {
    28.             if (playerInputData.Shoot > 0)
    29.             {
    30.                 Debug.Log("taco");
    31.                 gunData.secondsToNextSpawn -= _deltaTime;
    32.                 Entity instance = _concurrent.Instantiate(index, gunData.Prefab);
    33.              
    34.                 _concurrent.SetComponent(index, instance, new Translation
    35.                 {
    36.                     Value = localToWorld.Position
    37.                 });
    38.                  
    39.             }
    40.         }
    41.     }
    42.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    43.     {
    44.         var playerShootingJob = new PlayerShootingJob(
    45.             _bufferSystem.CreateCommandBuffer().ToConcurrent(),
    46.             Time.deltaTime
    47.         );
    48.         JobHandle jobHandle = playerShootingJob.Schedule(this, inputDeps);
    49.         _bufferSystem.AddJobHandleForProducer(jobHandle);
    50.         return jobHandle;
    51.     }
    52. }
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    Hybrid Renderer package installed?
     
    siggigg likes this.
  3. mbennett5

    mbennett5

    Joined:
    Oct 9, 2018
    Posts:
    4
    Yes it is.
     
  4. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    When you inspect the created entity, do you see a mesh ref in the components? Maybe its something simple like the quad normal facing the wrong way?
     
  5. mbennett5

    mbennett5

    Joined:
    Oct 9, 2018
    Posts:
    4
    This is what I see in the chunk info.
     

    Attached Files:

  6. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Seems like you are converting and inject the data for a sprite?
    If you are doing that in a root gameObject there is a bug on Entities 0.2.0