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 Call mono method from system

Discussion in 'Entity Component System' started by xindexer2, Jul 27, 2023.

  1. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    I'm sure this is super simple but I can't get the right combination to call a monobehaviour method from a system.

    Code (CSharp):
    1. public class SomeMonoBehaviourUI : MonoBehaviour
    2.     {
    3.         public void DoSomething()
    4.         {
    5.             Debug.Log("DoSomething");
    6.         }
    7.     }
    8.  
    9.     public class Baker : Baker<SomeMonoBehaviourUI>
    10.       {
    11.           public override void Bake(SomeMonoBehaviourUI authoring)
    12.           {
    13.               var entity = GetEntity(TransformUsageFlags.None);
    14.               // How do I add the SomeMonoBehaviourUI to the entity?
    15.           }
    16.       }
    17.     public partial class SomeUISystem : SystemBase
    18.     {
    19.         protected override void OnUpdate()
    20.         {
    21.             Entities.ForEach((in SomeMonoBehaviourUI ui) =>
    22.             {
    23.                 ui.DoSomething();
    24.             }).WithoutBurst().Run();
    25.         }
    26.     }
    I have tried AddComponentObject(entity, ----) with a bunch of different items with no luck.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    You can't directly attach a MonoBehaviour to an entity in a baker.
     
  3. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    Ok so what’s the bridge between the system and mono behavior,,I feel like this is stupid simple but I haven’t been able to connect them
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    Hacks.

    I'm being honest with you here. In my opinion, there's no clean way to do it. I've done it a couple of different ways, and I know others here have their favorite hacks. But really, they are all just hacks because there is no proper solution.
     
  5. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    Interesting, they provide you a way to easily connect a mono to a system through the EntityManager but everybody (including Unity) says that you want the data to flow the other way but don't provide an easy way to do it.
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    This is just as hacky honestly.
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Simplest (and most robust) solution is to avoid baking for this case.

    If you want MonoBehaviour (or any UnityEngine.Object) attached to the entity - reverse control it.
    Let MonoBehaviour author entity & keep it in the scene (not subscene).

    For better non-baking authoring you could take a peek at EntitiesExt.