using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace UnityEngine.XR.ARSubsystems
{
///
/// Holds arrays of changes to trackables (added, updated, and removed).
/// This is typically used by subsystems to report changes each frame.
///
/// The that can be added and updated.
public struct TrackableChanges : IDisposable where T : struct, ITrackable
{
NativeArray m_Added;
NativeArray m_Updated;
NativeArray m_Removed;
///
/// An array of added trackables.
///
public NativeArray added => m_Added;
///
/// An array of updated trackables.
///
public NativeArray updated => m_Updated;
///
/// An array of s that have been removed.
///
public NativeArray removed => m_Removed;
///
/// Whether the NativeArrays have been created.
///
public bool isCreated { get; private set; }
///
/// Constructs a from the number of added, updated, and removed trackables.
/// This creates the NativeArrays , , and
/// and should be disposed by calling on this object.
///
/// The number of added trackables.
/// The number of updated trackables.
/// The number of removed trackables.
///
/// An allocator to use for each of
/// , , and arrays.
///
public TrackableChanges(
int addedCount,
int updatedCount,
int removedCount,
Allocator allocator)
{
m_Added = new NativeArray(addedCount, allocator);
m_Updated = new NativeArray(updatedCount, allocator);
m_Removed = new NativeArray(removedCount, allocator);
isCreated = true;
}
///
/// Constructor which creates all three arrays and fills the
/// and arrays with repeated copies of
/// .
///
/// The number of added trackables.
/// The number of updated trackables.
/// The number of removed trackables.
/// The allocator to use for each of , , and arrays.
/// The value with which to fill the and arrays.
public TrackableChanges(
int addedCount,
int updatedCount,
int removedCount,
Allocator allocator,
T defaultValue)
{
m_Added = NativeCopyUtility.CreateArrayFilledWithValue(defaultValue, addedCount, allocator);
m_Updated = NativeCopyUtility.CreateArrayFilledWithValue(defaultValue, updatedCount, allocator);
m_Removed = new NativeArray(removedCount, allocator);
isCreated = true;
}
///
/// Constructs a from native memory.
///
///
/// Because native code might be using an older version of ,
/// this constructor first fills the and
/// arrays with copies of before copying the data from
/// the and pointers.
/// This ensures that the addition of new fields to will have
/// appropriate default values.
///
/// A pointer to a block of memory containing elements each of size .
/// The number of added elements.
/// A pointer to a block of memory containing elements each of size .
/// The number of updated elements.
/// A pointer to a block of memory containing s.
/// The number of removed elements.
/// A default which should be used to fill the and
/// arrays before copying data from and , respectively.
/// The number of bytes for each element in the and arrays.
/// An allocator to use when creating the , , and arrays.
public unsafe TrackableChanges(
void* addedPtr, int addedCount,
void* updatedPtr, int updatedCount,
void* removedPtr, int removedCount,
T defaultT, int stride,
Allocator allocator)
{
m_Added = NativeCopyUtility.PtrToNativeArrayWithDefault(defaultT, addedPtr, stride, addedCount, allocator);
m_Updated = NativeCopyUtility.PtrToNativeArrayWithDefault(defaultT, updatedPtr, stride, updatedCount, allocator);
m_Removed = new NativeArray(removedCount, allocator);
if (removedCount > 0)
{
UnsafeUtility.MemCpy(m_Removed.GetUnsafePtr(), removedPtr, removedCount * sizeof(TrackableId));
}
isCreated = true;
}
///
/// Creates a new from existing NativeArrays.
///
/// An array of added elements.
/// An array of updated elements.
/// An array of removed elements.
/// The allocator to use for each of the , , and arrays.
/// A new from existing NativeArrays. The caller must
/// the returned to avoid memory leaks.
public static TrackableChanges CopyFrom(
NativeArray added,
NativeArray updated,
NativeArray removed,
Allocator allocator)
{
var addedCopy = new NativeArray(added.Length, allocator);
addedCopy.CopyFrom(added);
var updatedCopy = new NativeArray(updated.Length, allocator);
updatedCopy.CopyFrom(updated);
var removedCopy = new NativeArray(removed.Length, allocator);
removedCopy.CopyFrom(removed);
return new TrackableChanges(addedCopy, updatedCopy, removedCopy);
}
///
/// Disposes the , , and arrays.
/// Safe to call on a default-constructed .
///
public void Dispose()
{
if (isCreated)
{
m_Added.Dispose();
m_Updated.Dispose();
m_Removed.Dispose();
}
isCreated = false;
}
TrackableChanges(
NativeArray added,
NativeArray updated,
NativeArray removed)
{
m_Added = added;
m_Updated = updated;
m_Removed = removed;
isCreated = true;
}
}
}