Search Unity

  1. Want to see 2020.1b in action? Sign up for our Beta 2020.1 Overview Webinar on April 20th for a live presentation from our evangelists and a Q&A session with guests from R&D.
    Dismiss Notice
  2. Interested in giving us feedback? Join our online research interviews on a broad range of topics and share your insights with us.
    Dismiss Notice
  3. We're hosting a webinar for the new Input System where you'll be able to get in touch with the devs. Sign up now and share your questions with us in preparation for the session on April 15.
    Dismiss Notice
  4. Dismiss Notice

Hybrid - Accessing Entity data from Monobehaviour

Discussion in 'Data Oriented Technology Stack' started by matthew-holtzem, Dec 10, 2019.

  1. matthew-holtzem

    matthew-holtzem

    Joined:
    Sep 1, 2016
    Posts:
    24
    Does anyone have any good advice or examples for accessing entity data from an associated monobehaviour? Trying to break out certain calculations from monobehaviours into systems and access the results from those monobehaviours.

    I am creating the entity from the gameobject so I have a reference to that entity and have been able to get the componentdata from EntityManager.GetComponentData but am concerned that that might not be a performant approach.

    Any tips would be greatly appreciated :D
     
  2. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    269
    Either that, it's not that big of a deal when it's for example your player and just a single entity but it surely is not that performant and runs only on the main thread.
    Other option is to use a ComponentSystem and add MBs with AddComponentObject to the entity so you can access it in a ForEach. Data accessing will have less overhead but it's still running only in the main thread.
    If you're not having main thread issues I wouldn't bother to be honest, otherwise you'd have to write jobs and those don't play nice with MBs.
     
    Tony_Max and matthew-holtzem like this.
  3. matthew-holtzem

    matthew-holtzem

    Joined:
    Sep 1, 2016
    Posts:
    24
    Ok thanks good to know. In my case there are potentially a lot of these items so I'm going to steer clear of GetComponentData. Here is what I have come up with.

    I have 3 systems
    1: Component System to copy position from gameobject to componentData
    2: Job System to do my calculation
    3: Component System to copy calculation result from componentData to gameobject

    For my specific use-case there's probably not much point to this because my calculation is not really very expensive, but in the interest of learning figuring out useful hybrid patterns this was a useful endeavor.

    I would still love to hear any better alternatives that people find if they come across this thread
     
  4. matthew-holtzem

    matthew-holtzem

    Joined:
    Sep 1, 2016
    Posts:
    24
    On second thought when I look at my profiler it looks like my main thread component systems just sit there waiting until the JobSystem is finished anyway so perhaps there isn't really a good way to do this kind of thing afterall
     
  5. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    269
    It's probably okay if you don't have too many gameobjects you copy from. When it's going up to 100-1000+ you might want to look for faster solutions.
    One way for transforms specifically (because this is mostly the bottleneck) is to use:
    var transforms = transformEntityQuery.GetTransformAccessArray();
    and an IJobParallelForTransform.

    When you have only 1 entry/exit way where redundant data is copied around it's still okay. Rule of thumb, the more you stay in ECS space the more performant and scalable it will be.

    Also, have you written those systems yourself? Because there is already a copy transform from/to gameobject component you can use.
    GameObjectEntity has been deprecated, it is still available but I would not recommend it to anyone. Some systems are still in place though that are useful, like copying transforms.

    Right now I'm going for a HybridEntity MB component that either is pure ECS when it has a ConvertToEntity script on it or creates the entity and components itself in Awake. I've yet to find any issues with it, unlike GameObjectEntity where I had to comment out the OnDisable event because it was interfering with the hybrid workflow.
    Hybrid is in a weird place, so I'd also recommend you phase out any gameobjects and monobehaviours as soon as possible.
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    1,052
    It doesn't matter if the systems are waiting for the jobs to finish if the jobs execute significantly faster than had you done everything in MonoBehaviours. If you are asking for help optimizing, post profiler timeline pics and code, and we can definitely help.
     
  7. matthew-holtzem

    matthew-holtzem

    Joined:
    Sep 1, 2016
    Posts:
    24
    That's a good point. In this case I would probably still be getting some gains from the best compiler and running logic in the system rather than a bunch of updates
     
unityunity