Search Unity

Question Spawning Entities from a MonoBehaviour - no longer supported??

Discussion in 'Graphics for ECS' started by lclemens, Jan 11, 2023.

  1. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    In my AnimationCooker project I had a MonoBehaviour spawner in 0.51 that worked just fine. When I converted to 1.0, it stopped working. It uses World.DefaultGameObjectInjectionWorld.EntityManager.Instantiate() to instantiate a new entity based on an existing one. I'm using URP 14 and 2022.2.

    It instantiates the entity just fine, and they can be seen in the Entity Hierarchy. Each spawned entity looks like it has all the right components and values as far as I can tell. The problem is that the entities aren't rendered. I don't think it's a problem with the vertex animation because the same problem happens if I instruct the spawner to spawn a bunch of primitive cube entities.

    Whenever I spawn the entities using a System with ecb.Instantiate(), everything works fine. So it has something to do with EntityManager and the MonoBehaviour. I don't mind spawning from a System in this case, but in other parts of my game it makes sense to be able to spawn in an entity once in a while from a MonoBehaviour script when a button is pressed by the player or something. So I'd like to understand this issue which broke after upgrading to 1.0. If there is a problem with rendering entities created via EntityManager.Instantiate(Entity), I did not see a mention of it in the documentation.

    These little tiny dots are shadows (I think) of the entities that were supposed to be spawned (the horse, spider, and cube). These shadows are much smaller than they are supposed to be, and of course the entities themselves are not being rendered. The horse and spider are the original entities that are being cloned. One thing I did notice is that the shadows only display for the entities that have vertex animation - the cube is just a standard cube and its shadows are not rendered.

    upload_2023-1-11_14-36-46.png

    So any ideas what is going on? If anyone wants to see the full source code it is here: https://gitlab.com/lclemens/animationcooker (and the specific spawner code in question is here https://gitlab.com/lclemens/animationcooker/-/blob/master/Assets/ExampleScene/Scripts/GridSpawner.cs ). Thanks for your help.
     
    Last edited: Jan 11, 2023
  2. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    351
    What do the instantiated entities look like in the Entities hierarchy? Visible shadows suggests that the entities are rendering, but there is something going wrong with it. Perhaps their scale is incorrect and they are just scaled to be very small?
     
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Yes that was it! Thanks 1000x!

    So yeah... this is embarrassing. Still getting used to the new transforms - I forgot pos, rot, and scale are all rolled into one. I needed to change the line:

    Code (CSharp):
    1. m_em.AddComponentData<LocalTransform>(entity, new LocalTransform { Position = m_pos });
    to

    Code (CSharp):
    1.  
    2. LocalTransform xForm = m_em.GetComponentData<LocalTransform>(entity);
    3. xForm.Position = m_pos;
    4. m_em.SetComponentData<LocalTransform>(entity, xForm);
    5.  
     
    JussiKnuuttila likes this.