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

Using managed objects with ECS

Discussion in 'Entity Component System' started by LaphicetCrowe, Mar 18, 2021.

  1. LaphicetCrowe

    LaphicetCrowe

    Joined:
    Sep 30, 2020
    Posts:
    17
    ECS/DOTS has been fun for me, but I understand not all of the good stuff is currently supported, and if it is, it's by some third party that makes it really convoluted. I know the C# job system can work with traditional Unity, but there some aspects which nearly short circuit my brain.

    For example, when it comes to an object having an array I need to use in a job, NativeArray can't be multidimensional, and doing a job one by one rather than in a batch ruins the point. For a waypoint system, I tried doing a GameManager-scope waypoint NativeArray, then in a job, grab that array, allocate another array for each NPC's waypoint IDs (index into the waypoint array GameManager has), and allocate yet another array for the offsets of the ID array. In the end, it bore no fruit and my brain was burnt to a crisp.

    So to make life easier and still utilise ECS for other things, I decided to take a performance hit for navigation after reading https://www.reddit.com/r/Unity3D/comments/bfcqqf/navmeshagent_using_hybrid_inject_ecs/. It seems to kind of (?) work, except I have no way of copying a NavMeshAgent from a game object to an entity. If I grab the component and add it via EntityManager.AddComponentObject, it only grabs a reference, which of course gets deleted with the parent object. My last attempt was to Object.Instantiate the NavMeshAgent component, but Unity instead clones the entire game object instead of just the component.

    Given the above, I'm out of ideas. I want to use ECS and am willing to take a performance hit for navigation until ECS gets an official nav mesh system of its own. However, I feel forced to choose traditional or ECS in their entirety.
     
  2. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    791
    Last edited: Mar 18, 2021
    LaphicetCrowe likes this.
  3. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Krajca and LaphicetCrowe like this.
  4. LaphicetCrowe

    LaphicetCrowe

    Joined:
    Sep 30, 2020
    Posts:
    17
    Appreciate the tips, guys. I looked over the docs you mentioned and tried to implement a GOCS-derived class, but my entity just sits there with no components shown in the entity debugger inspector.

    For context, AIMovement is an IComponentData with some waypoint data in it. I decided to use it as a filter seeing as all AI movement will use nav meshes.

    using UnityEngine.AI;

    /// <summary>
    /// Because Unity ECS lacks a nav mesh system, use the classic one with our entities for now.
    /// </summary>
    public class NavMeshAgentTransferSystem : GameObjectConversionSystem
    {
    protected override void OnUpdate()
    {
    Entities.WithAll<AIMovement>().ForEach((NavMeshAgent agent) =>
    {
    AddHybridComponent(agent);
    });
    }
    }
     
    Last edited: Mar 19, 2021
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
  6. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    For this to work as written you'd have to ensure the AIMovement component was added by some other conversion method before this system runs.
     
  7. LaphicetCrowe

    LaphicetCrowe

    Joined:
    Sep 30, 2020
    Posts:
    17
    I did, via IConvertGameObjectToEntity. I'm guessing an UpdateAfter attribute might work?
     
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,068
    if you are good with unsafe code and pointers, you can use a native array of an unsafe struct which holds your data pointer, count, allocator ect.. you can easly manage it using unsafeUtity and NativeArrayUnsafeUtility.
    i use this approach in extreme cases where i need to Create /Pass an unknow amount of native arrays within / to a job.
     
  9. LaphicetCrowe

    LaphicetCrowe

    Joined:
    Sep 30, 2020
    Posts:
    17
    I'd rather not. I mean I use pointers and stuff in C++ but in C# it seems like an ugly mess. I also gave DotNav a try and it seems very limited right now. I remember an ECS sample where an Animation object was made a blob asset, but it required a new struct and stuff and there's no explanation how it works.