Search Unity

Question How to set an Entity SubScene (collider) component, with Main Scene (Camera) data?

Discussion in 'Entity Component System' started by Tudor, Mar 22, 2023.

  1. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    What I ask might not be very directly possible, but it illustrates I want to get data from a unity Main Scene gameObject (in this case actually from the Camera), and send it over to a component in my Entities SubScene.

    I want to manipulate a physics enabled Entity's collider, based on let's say the Camera bounds, and/or Camera FoV, also Camera position.

    Problem 1: Best way to communicate cross-scene-subscene
    Problem 2: while also communicating cross-system (mono to ecs)

    PS: Also not finding any up-to-date info on how to do the opposite: move a mono gameObject from an entity (also cross-scene). E.g. make the mono Camera, follow the player entity.

    -------

    ..I probably could just set some static data from the mono.. ..and read it from a system/aspect.. but that's hacky af..
     
    Last edited: Mar 22, 2023
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
  3. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    The ECS samples is a great repo, but kind of gave up on HelloCube specifically, because it's too basic / suboptimal. E.g. it doesn't use command buffers, the Aspect demo shows a Transform aspect which we already know about, badly simplified practices etc. a little unhelpful.

    BUT, what particular part of HelloCube (or ecs samples) do you think tackles the kind of data transfer I am describing?

    What keywords do I look for?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    There is syncing demo in Hello Cube.
    Isn't this one what you néed?

    It is good these are simple examples.
    Since when you write ECS based systems, you focus on small subset of your application. For usual cases, each system and its jobs are doing only dedicated task. Narrowing the problem down.

    Alternatively you could always try Mega City demo. But I doubt it will be much helpful for beginning learning, since its design is based on older DOTS. Still it may be good study case.

    If you try to think about system which doing too many things and you are not comfortable to work with such paradigm yet, it can easily become overwelming.

    Some of DOTS devs, due to large count of jobs and as results delay caused by scheduling jobs, specially before ISystem, did use fewer systems with many jobs inside.

    But for you as you learning DOTS, I advise to focus one job per system.
     
  5. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    1. No, that basic 101 syncing example is not what I asked for. That example instantiates a game object already in the entities subscene, and references it in a managed IComponentData class (as opposed to the unmanaged struct).
    I am asking to access the CAMERA, from the MAIN SCENE, in the ENTITIES SUBSCENE.

    2. You keep making a lot of assumptions. I am not a beginner just starting to look at dots, and there's many great projects in between hello cube and mega city. Nor were my comments meant to be interpreted as "I just want bloated systems".
     
    Last edited: Mar 22, 2023
  6. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    This is not really possible if you want to do it during the baking process.

    But you can do it during runtime. I usually add a `RequiresSetup` or similar named component tag to an entity and an ISystem (or SystemBase if you need managed data) will setup the required data for later processing.

    If you need to reference a managed monobehaviour, you can store it in a class component (instead of a struct), or you have one system which extracts the necessary information of the managed data into a blitable data structure on a component for afterward faster processing.

    For references to mono behaviours which have a singleton-like character, unity currently recommends to use static variables (as they did in the netcode racing example).

    Does that answer your question?
     
  7. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    855
    There are many different ways to do this but what I've found to be useful is:

    - A MainCameraSystem queries for a MainCamera singleton, if one isnt found it finds the main camera(inside of OnCreate), creates an entity and adds that managed UnityEngine.Camera component to it, as well as the unmanaged MainCamera component.
    - During update copy any relevant camera data to the MainCamera component and/or copy the camera's transform data.
    - Inside of other systems, query for that MainCamera singleton, and you can stuff that data inside of jobs for processing.
     
  8. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150

    What I am still struggling with, maybe I am dumb, is the crux of the matter: how do you get a reference to the camera (which is in another scene), from your entities scene?

    Thanks Occuros for pointing this out. "For references to mono behaviours which have a singleton-like character, unity currently recommends to use static variables (as they did in the netcode racing example)."
    This is exactly the kind of practices recommendations that provide valuable context
     
    Occuros likes this.