Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Converting to dots 1.0.16

Discussion in 'Entity Component System' started by kacperszkola365, Jan 24, 2024.

  1. kacperszkola365

    kacperszkola365

    Joined:
    Oct 26, 2021
    Posts:
    2
    So hey i have the following depreciated dots code and im not certain how do i convert it to the new dots system.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5. using static Terrain;
    6. using System.Diagnostics;
    7.  
    8. public class OnEntityAssign : MonoBehaviour ,IConvertGameObjectToEntity
    9. {
    10.     private Entity entity;
    11.     private EntityManager entityManager;
    12.    
    13.     public PathIndex pathIndex;
    14.  
    15.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    16.     {
    17.         this.entity = entity;
    18.         this.entityManager = dstManager;
    19.         dstManager.AddBuffer<PathBuffer>(entity);
    20.     }
    21.  
    22.     public Entity GetEntity()
    23.     {
    24.         return entity;
    25.     }
    26.  
    27.     public EntityManager GetEntityManager()
    28.     {
    29.         return entityManager;
    30.     }
    31.  
    32.     public PathIndex GetPathIndex()
    33.     {
    34.         return entityManager.GetComponentData<PathIndex>(entity);
    35.     }
    36.  
    37.     public DynamicBuffer<PathBuffer> GetPathBuffer()
    38.     {
    39.         return entityManager.GetBuffer<PathBuffer>(entity);
    40.  
    41.     }
    42.  
    43.     public void SetComponentData(PathIndex pathIndex)
    44.     {
    45.         entityManager.SetComponentData(entity, pathIndex);
    46.     }
    47.  
    48.     public void EnityConvert()
    49.     {
    50.         //this.gameObject.AddComponent<ConvertToEntity>();
    51.     }
    52. }
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,316
    First things first, everything that is public needs to be made nonpublic (except for Convert). Bakers don't really have a way to communicate with each other. And MonoBehaviours need to be treated as readonly data.
     
    kacperszkola365 likes this.
  3. MildlyInfuriating

    MildlyInfuriating

    Joined:
    Oct 20, 2015
    Posts:
    73
    kacperszkola365 and WAYNGames like this.
  4. kacperszkola365

    kacperszkola365

    Joined:
    Oct 26, 2021
    Posts:
    2
    So if anybody is curious here is how i rewrote and it works now so neat :)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5. using static Terrain;
    6. using System.Diagnostics;
    7.  
    8. public struct OnEntityAssign : IComponentData
    9. {
    10.     public Entity entity;
    11.  
    12.     public Entity GetEntity()
    13.     {
    14.         return entity;
    15.     }
    16.  
    17.     public EntityManager GetEntityManager()
    18.     {
    19.         return World.DefaultGameObjectInjectionWorld.EntityManager;
    20.     }
    21.  
    22.     public PathIndex GetPathIndex()
    23.     {
    24.         return GetEntityManager().GetComponentData<PathIndex>(GetEntity());
    25.     }
    26.  
    27.     public void InitBuffer()
    28.     {
    29.         GetEntityManager().AddBuffer<PathBuffer>(GetEntity());
    30.     }
    31.  
    32.     public DynamicBuffer<PathBuffer> GetPathBuffer()
    33.     {
    34.         return GetEntityManager().GetBuffer<PathBuffer>(GetEntity());
    35.     }
    36.  
    37.     public void SetComponentData(PathIndex pathIndex)
    38.     {
    39.         GetEntityManager().SetComponentData(GetEntity(), pathIndex);
    40.     }
    41. }
    and the baker:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5.  
    6. public class OnEntityAuthoring : MonoBehaviour
    7. {
    8.     public int id;
    9. }
    10.  
    11. public class OnEntityAssignBaker : Baker<OnEntityAuthoring>
    12. {
    13.     public override void Bake(OnEntityAuthoring authoring)
    14.     {
    15.         var entityy = GetEntity(TransformUsageFlags.Dynamic);
    16.         AddComponent(entityy, new OnEntityAssign
    17.         {
    18.             entity = entityy,
    19.         });
    20.     }
    21. }