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

ECS Camera and serialization

Discussion in 'Entity Component System' started by mr-gmg, Feb 2, 2020.

  1. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    Hey guys, I have a little problem,
    I did an ECS camera system, and it works perfectly.

    I didn't find how to use a pure ECS camera(probably nohow for now) and I put on the camera gameobject ConvertToEntity component with conversion mode ConvertAndInjectGameObject. Without ConvertAndInjectGameObject mode camera simply destroys and nothing shows, but in this mode, I receive an exception for every monobehaviour on the camera during the serialization
    Code (CSharp):
    1. if (cType.Category == TypeManager.TypeCategory.Class)
    2.     throw new ArgumentException("Serialization of GameObject components is not supported for pure entity scenes");
    for some reason, it tries to serialize all of them and I don't know how to ignore this serialization.
    How can I figure it out?
     
  2. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    Code (CSharp):
    1. public static void SaveGame(string fileName) {
    2.     EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    3.     using (var writer = new StreamBinaryWriter(GetFullPathByName(fileName))) {
    4.         SerializeUtility.SerializeWorld(entityManager, writer, out object[] objectReferences);
    5.     }
    6. }
    7.  
    Screenshot_129.png
    Screenshot_128.png
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Why do you have to serialize a Camera? I have worked for 10 years in Unity and barely Serialized anything and it always worked fine. Just make the variables public and they are serialized.
     
  4. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    I don't really want to serialize a camera, I want to use a camera with an ECS system.
    For that, I marked a camera gameobject with ConvertToEntity component in ConvertAndInjectGameObject mode. it creates a camera entity with a Translation, LocalToWorld and Rotation components.
    Screenshot_15.png
    Also, I put CameraInjection monobehaviour on the camera game object. It adds several ECS components to the camera entity.
    Screenshot_18.png
    So, my final ECS camera entity looks like that
    Screenshot_16.png
    and my CameraSystem controls camera translation, rotation etc.

    So, after that, I want to save my ECS world, for that I use this function:
    Code (CSharp):
    1. public static void SaveGame(string fileName) {
    2.     EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    3.     using (var writer = new StreamBinaryWriter(GetFullPathByName(fileName))) {
    4.         SerializeUtility.SerializeWorld(entityManager, writer, out object[] objectReferences);
    5.     }
    6. }
    it serializes and saves every entity with every component in the world. But, for some reason, it also tries to serialize every MonoBehaviour component on the Camera gameobject, I think because I add ConvertToEntity component on that. And I receive an exception for every monobehaviour on the camera gameobject
    Code (CSharp):
    1. if (cType.Category == TypeManager.TypeCategory.Class)
    2.     throw new ArgumentException("Serialization of GameObject components is not supported for pure entity scenes");
    I simply don't want to serialize anything related to camera. But I don't want how to do it. That was my question :)
     
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Maybe just serialize position and rotation at SaveGame under another guise and apply back to the camera Archetype when reloading?
     
    mr-gmg likes this.
  6. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    You mean do not convert the camera to ECS? I think it might work. I may do not touch the camera, and create a new entity with all necessary components, and one more system to apply the changes to the camera transform, right? Anyway, thank you! You helped a lot!

    But does it mean that I cannot use ConvertToEntity and serialize the world after it?
     
  7. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    May I ask..as I will be seeing if I can keep my cameras..Are you following entities around or monobehaviour controlled gameObject/transforms around?

    I seem to be at the same point of understanding as you with DOTS. I can read and write MonoBehaviours C# fluently and can get frameworks going for any game mechanic fairly rapidly..but I continually run into walls with DOTS physics accessing components and where i can do queries etc. I am seriously trying to figure out how to save all the work I put into the mono systems I wrote and simply use DOTS physics for the collisions, scoring pickups and subatomic particle simulations.

    I would have written four more levels in the time it took me to write one ParticleProjectile emitter. A typical emitter was taking me about ten minutes to author prior. But that sucker spit out 5000 rigidbody entities with the AtomicSpeciesData (all the math data about protons, neutrons, electrons and others) and they bounced around the arena at 120FPS and disappeared at the end of the specified LifetimeData.lifetime. My biggest issue is how to figure out what used to be simple physics transactions like OnTriggerEntered. I am struggling with the syntax for getting collisions between the rigidbody vehicle..the main method of exploring, colliding with and scoring protons, neutrons and electrons to the VehicleInventoryController in the MonoBehavior world. I will be damned if I am gonna rewrite all the logic that transfers the scores to umpteen other systems for creating element atoms. So my conundrum is finding the syntax bridges that will let me take the output of a DOTS query and use it immediately in my MonoBehaviour components. I also have 183 ScriptableObjects that have all the details of all atoms and interesting isotopes and I need full access to them and many fields are not blittable.
     
  8. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    The only one gameobject(except UI) that I'm going to use is a camera gameobject, because as I understood, for now, there is no pure ECS camera. All my environment is on entities. And I save only the ECS world, no other data.
    I'm not sure if I can help you with other issues, because I did not work with ECS physics yet, but I will try to explain how I synchronized my entity data with UI monobehaviours.
    For example, I have a very simple PauseSystem and it looks like that
    Code (CSharp):
    1. public class PauseSystem : JobComponentSystem {
    2.     [BurstCompile]
    3.     private struct PauseJob : IJobForEach<GameSystem.GameState> {
    4.         public bool spacePressed;
    5.         public void Execute(ref GameSystem.GameState gameState) {
    6.             if (spacePressed) {
    7.                 gameState.paused = !gameState.paused;
    8.             }
    9.         }
    10.     }
    11.  
    12.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    13.         PauseJob pauseJob = new PauseJob {spacePressed = Input.GetKeyDown(KeyCode.Space)};
    14.         JobHandle jobHandle = pauseJob.Schedule(this, inputDeps);
    15.         return jobHandle;
    16.     }
    17.  
    18.     public static bool isPaused;
    19.  
    20.     [UpdateAfter(typeof(PauseSystem))]
    21.     private class CollectDataSystem: ComponentSystem {
    22.         protected override void OnUpdate() {
    23.             Entities.WithAll<GameSystem.GameState>().ForEach((Entity entity, ref GameSystem.GameState gameState) => {
    24.                 isPaused = gameState.paused;
    25.             });
    26.         }
    27.     }
    28. }
    Here I have a system and a job to change the pause state, and also I have an internal private system which just collects data and puts it to some variables in a classic way. In my UI controller update pause code looks like that:
    Code (CSharp):
    1. private void UpdatePause() {
    2.    m_pause.gameObject.SetActive(PauseSystem.isPaused);
    3. }
    I think for your purposes you may do the same, just write a system that will be collecting all calculated data and put it into your monobehaviour.
    I'm not sure if it is a good way, but it is the only one for now that I came up with.
     
    Last edited: Feb 3, 2020
    ippdev likes this.
  9. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Will look into this. I tried making a simple trigger using Unity example boilerplate with some alterations and my framerate was cut in half. Not sure where so I ended up tossing 4 scripts to get it back and starting from scratch again.
     
  10. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    Let me know please, what is the solution you come up with.
     
  11. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Will do. I came up with a rigidbody emitter script that is mono based but emits entities based on an Archetype. You interested in that?
    https://forum.unity.com/threads/using-ecs-with-nested-gameobjects.813540/
    I get 5000 rigidbodies bouncing around at 120 fps. Nothing to sneeze at and worth the pain so far. Been poking the community for simple patterns for a trigger that is only triggered by one object..the vehicle..so it takes like less than ten lines to deal with in mono in two scripts. It eludes me entirely in ECS DOTS Physics..so far.
     
  12. mr-gmg

    mr-gmg

    Joined:
    Aug 31, 2015
    Posts:
    62
    I'm interested in everything that related to ECS, thank you! :)
     
  13. shyamarama

    shyamarama

    Joined:
    Dec 13, 2019
    Posts:
    10
    I think Cinemachine is the answer

     
    digitaliliad likes this.