Search Unity

  1. We have added a sub-forum for DOTS Graphics. Please use the new space for related discussions.
    Dismiss Notice
  2. The 2021.2 beta is now available. To find out what's new, have a look at our 2021.2 beta blog post.
    Dismiss Notice
  3. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  4. Dismiss Notice

How to use UnsafeList AddRange

Discussion in 'Data Oriented Technology Stack' started by TheGabelle, Apr 28, 2021.

  1. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    229
    I'm having difficulty understanding how to do this properly. I get an error on line 16 which reads:
    "Pointers and fixed size buffers may only be used in an unsafe context [Assembly-CSharp]csharp(CS0214)"

    Code (CSharp):
    1. public struct Poly : IDisposable
    2. {
    3.     public bool isClosed;
    4.     public AABB AABB;
    5.     UnsafeList<float3> points;
    6.  
    7.     public Poly (bool closed)
    8.     {
    9.         isClosed = closed;
    10.         AABB = new AABB();
    11.         points = new UnsafeList<float3>(3, Allocator.Persistent);
    12.     }
    13.     // ...
    14.     public void AddPoints(ref NativeList<float3> pts)
    15.     {
    16.         points.AddRange(pts.GetUnsafeReadOnlyPtr(), pts.Length);
    17.         UpdateBounds();
    18.     }
    19.     // ...
    20. }
    Somewhat off topic, but if I have a NativeList<Poly> and Dispose it, will each Poly item have its Dispose method invoked automatically?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    2,932
    GetUnsafeReadOnlyPtr() returns a pointer

    You need to use the unsafe keyword and to mark your asmdef as allowing unsafe code to use

    No, you need to call them yourself.
     
    Last edited: Apr 28, 2021
    TheGabelle likes this.
  3. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    229
unityunity