Search Unity

Changing Camera.orthographicSize in ComponentSystem

Discussion in 'Entity Component System' started by sand_lantern, Mar 11, 2020.

  1. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    I am working on prototyping something with an orthographic view that I would like to be able to "zoom" in and out. Now with an orthographic camera style, you need to change the orthographicSize attribute on the camera in order to do this. Since (AFAIK) the camera still isn't really implemented in ECS, I was trying to devise some way to get the camera on Entity creation.

    This is what I attempted to do:
    Code (CSharp):
    1.  
    2. public static Entity Build (EntityManager manager, Entity source)
    3. {
    4.     var instance = manager.Instantiate (source);
    5.  
    6.     GameObject MyCamera = manager.GetComponentObject<Transform> (instance).gameObject;
    7.  
    8.     return instance;
    9. }
    Hoewever, the GetComponentObject allways returns null. I tried adding the CopyTransformToGameObjectProxy script to my player prefab, but that didn't work either. Since I am in need of multiple cameras, I can't just access the main camera and call it a day.

    Obviously I am going about this wrong. Any thoughts?
     
    Last edited: Mar 11, 2020
  2. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    Some clarifications. The actual error that I'm getting is
    ArgumentException: A component with type:Transform has not been added to the entity.



    The gist of the containing code is this. Note that I heavily simplified things in order to show example code, but I think this is everything that's relevant. Assume that myPlayerArchetype has been initialized correctly from the prefab (because as far as I can tell, it has).

    Code (CSharp):
    1. [UpdateBefore (typeof (PlayerInputHandler))]
    2. public class PlayerInputReader : ComponentSystem
    3. {
    4.     Entity myPlayerArchetype;
    5.     protected override void OnCreate ()
    6.     {
    7.         GameDatabase.Build (World.DefaultGameObjectInjectionWorld.EntityManager, myPlayerArchetype);
    8.     }
    9.  
    10.     protected override void OnUpdate ()
    11.     {
    12.         // Some handling logic
    13.     }
    14. }
     
    Last edited: Mar 12, 2020
  3. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    Also, this is how my prefab looks:
    upload_2020-3-12_12-34-53.png

    And in the debugger I end up with:
    upload_2020-3-12_12-35-30.png

    Nothing too interesting here.
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Convert And Destroy doesn't keep any UnityEngine.Component after the conversion, if you want to keep the Transform you need to use Convert And Inject instead
     
    sand_lantern likes this.
  5. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    Thanks! That's probably the missing piece. I am also looking at the suggestions in this thread
    https://forum.unity.com/threads/how-do-you-combine-a-hybrid-ecs-with-a-pure-ecs.575545/

    Seems like this may be a better designed way to go anyways for me.
     
  6. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    So, I'm still having troubles with this. Does Convert And Inject work when dealing with creating an archetype? As far as I can tell, the convert script happens when I generate that, but not when I actually create an new player instance. Still getting component objects with no monobehaviour.
     
  7. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    Ok, the problem was that I was instancing my object from an archetype rather than dealing with the prefab directly.

    What I was doing was something like this:
    Code (CSharp):
    1. GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld (World.DefaultGameObjectInjectionWorld, null);
    2. playerArchetype = GameObjectConversionUtility.ConvertGameObjectHierarchy (playerPrefab.gameObject, settings);
    3. EntityManager manager = world.EntityManager;
    4. manager.Instantiate(playerArchetype);
    What I actually needed was something like this:
    Code (CSharp):
    1. EntityManager manager = world.EntityManager;
    2. manager.Instantiate(playerPrefab);
    By converting the prefab to an archetype for creation, that was bypassing the create and inject setting so that the monobehaviours never existed in scene. If this is documented somewhere I didn't see it. It would be nice if this behavior was a little more transparent.