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

Question Correctly adding a Managed Shared component

Discussion in 'Entity Component System' started by CynicalBusiness, Aug 21, 2023.

  1. CynicalBusiness

    CynicalBusiness

    Joined:
    May 3, 2020
    Posts:
    10
    So, I've come upon what sounds like a use case for a managed shared component. However, after digging through a lot of documentation, I cannot discern how to correctly add the component.

    In my case, I have a managed object, we'll call it MyObject, that multiple (and often many) entities need to point to and are grouped by, so I created MySharedComponent which holds a reference to this type. Querying and/or accessing this data is very rare, so I'm not concerned about the potential performance drawbacks of doing it this way over engineering some trickery to point to this object.

    I read through the documentation on both shared and managed components, but it did not specify how to add the component to an entity. So, I dug around and so far I've discovered these:
    • EntityManager.AddComponentObject claims to be for Unity Component objects, of which my object is not, and it doesn't sound like it's for shared components anyway
    • EntityManager.AddComponentData expects an IComponentData, not an ISharedComponentData, and doesn't have reference-type overloads anyway
    • EntityManager.AddSharedComponentManaged has no overloads which takes a reference type
    • EntityManagerManagedComponentExtensions has no extension which accepts ISharedComponentData
    How do I actually add this component to an entity?
     
  2. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    825
    Seems like you already found it?
    EntityManager.AddSharedComponentManaged(entity, yourManagedSharedComponent)
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    Shared components are always structs. Store your class as a field inside the struct to make your shared component managed.
     
    apkdev likes this.
  4. CynicalBusiness

    CynicalBusiness

    Joined:
    May 3, 2020
    Posts:
    10
    AddSharedComponentManaged does not accept reference types, only structs, so that's why I wasn't using it. The documentation says to use a class for managed shared components and I cannot pass a class to this. Confusing still, AddSharedComponent is actually marked as obsolete for me, saying to use the prior instead, even for unmanaged components.

    I thought that might work, but wasn't sure if that was actually going to function correctly or was even remotely intended, given that conflicts with what the docs say. However, I think I'll just settle for this unless there's some other more intended way that presents itself. Thanks!

    Edit: It would appear I misunderstood the docs and this is actually the intended way to approach this, my bad.
     
    Last edited: Aug 23, 2023