Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Generic Components

Discussion in 'Project Tiny' started by GilCat, Dec 24, 2020.

  1. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Hello
    I'm trying to use generic components alongside with tiny (IBufferElementData) which i use all the time in regular DOTS. But in tiny it immediately crashes the editor when i play and in the build the system using that generic component doesn't work at all.
    I haven't found anything about it in the forum or documentation so i assumed it should work.
    I will look further into the issue but let me know if i'm missing something here.
    My component:
    Code (CSharp):
    1.   public struct KeyFrame<T> : IBufferElementData
    2.     where T : struct {
    3.  
    4.     public T Value;
    5.     public float NormalizedTime { get; set; }
    6.   }
    Registering it with :
    Code (CSharp):
    1. [assembly: RegisterGenericComponentType(typeof(KeyFrame<Color>))]
    Thanks
     
  2. BlaiseRoth

    BlaiseRoth

    Joined:
    Oct 11, 2016
    Posts:
    8
  3. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    My problem was that I was trying to use generics in the Foreach lambda of systemBase and that was never supported. The odd thing is that no warning error was thrown, just a plain crash :(.
    My way to do it right now is to use IJobChunk that supports those but it is a lot of boilerplate code.
    Remember that when using generics they must be know at compile time in order to use Burst.
     
  4. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Using generics with Tiny still shows lots problems that are not seen in regular DOTS.
    I don't know if anyone is using generics but for me it's a must have because I don't want to be writing every single system and jobs over and over when it can be written with one single system using generics.
    I hope next versions of Tiny will address such these issues and others.
     
  5. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    To put some meaning into the problems i've mentioned here is an example that does will show it:

    Code (CSharp):
    1.  
    2. /// Concrete version of the generic system
    3. public class ConcreteSystem : GenericSystem<Translation> { }
    4.  
    5. public class GenericSystem<TComp1> : SystemBase
    6.   where TComp1 : struct, IComponentData {
    7.  
    8.   EntityQuery m_query;
    9.  
    10.   protected override void OnCreate() {
    11.     base.OnCreate();
    12.     m_query = GetEntityQuery(ComponentType.ReadWrite<TComp1>());
    13.   }
    14.   protected override void OnUpdate() {
    15.     Dependency = new MyJob {
    16.       Comp1TypeHandle = GetComponentTypeHandle<TComp1>(false)
    17.     }.Schedule(m_query, Dependency);
    18.   }
    19.  
    20.   struct MyJob : IJobChunk {
    21.     public ComponentTypeHandle<TComp1> Comp1TypeHandle;
    22.  
    23.     public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) {
    24.       // Error here on tiny only
    25.       // InvalidOperationException: You are not allowed to read this native container or resource.
    26.       var comps = chunk.GetNativeArray(Comp1TypeHandle);
    27.       // Do something with the components
    28.       // In real case scenarios the component will extend from a interface in
    29.       // in order to do something meaningful
    30.     }
    31.   }
    32. }
    33.  
    When building with Tiny i get InvalidOperationException: You are not allowed to read this native container or resource.
    Also attached a project reproducing the problem
     

    Attached Files:

  6. NagaChiang

    NagaChiang

    Joined:
    Sep 17, 2020
    Posts:
    13
    Seems we encountered the same problem. I posted awhile ago but no luck.
     
    GilCat likes this.