Search Unity

How to use UnsafeList AddRange

Discussion in 'Entity Component System' started by TheGabelle, Apr 28, 2021.

  1. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    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:
    3,759
    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:
    242