Documentation

Unity Engine

Download Unity

Unity Engine

BlobObjectReference<T>

A reference that keeps the blob data stored in a BlobObject alive, so that jobs and Burst-compiled code can read it.
Read time 2 minutesLast updated 7 hours ago

Definition

public struct BlobObjectReference<T> : IDisposable where T : unmanaged

Remarks

Use this struct to read the blob data of a BlobObject from jobs and Burst-compiled code without copying it. Get a reference from BlobObject.AcquireReference, then check BlobObjectReference<T>.IsValid before you use it.
The data stays alive until you call BlobObjectReference<T>.Dispose, even if the object is destroyed or its data is replaced, so always dispose the reference when you finish with it. Use BlobObjectReference<T>.Clone to get another reference that you dispose separately, for example to give another thread its own reference.
You can't store the reference in serialized data. Reference the BlobObject itself instead.
The following example uses the
Unity.Animations
,
Unity.Collections
,
Unity.Entities
, and
UnityEngine
namespaces. It stores a custom blob in a
BlobObject
, reads the data back through a
BlobObjectReference
, and then disposes the reference and destroys the object.
Additional Resources: BlobObject

Examples

// This example stores marker times in a BlobObject, reads them back through a reference, and sums them.// A custom blob type can hold only unmanaged data, such as values and BlobArray fields.public struct MarkerTable{ public BlobArray<float> Times;}public static float StoreAndSumMarkerTimes(float[] markerTimes){ const int version = 1; // Build the blob asset. SetBlobAsset copies it, so you can dispose the original afterwards. BlobObject blobObject = new BlobObject(); using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp)) { ref MarkerTable root = ref blobBuilder.ConstructRoot<MarkerTable>(); BlobBuilderArray<float> times = blobBuilder.Allocate(ref root.Times, markerTimes.Length); for (int i = 0; i < markerTimes.Length; ++i) { times[i] = markerTimes[i]; } using (BlobAssetReference<MarkerTable> blobAsset = blobBuilder.CreateBlobAssetReference<MarkerTable>(Allocator.Temp)) { blobObject.SetBlobAsset(blobAsset, version); } } // Acquire a reference with the same version. The data stays alive until you dispose the reference. BlobObjectReference<MarkerTable> reference = blobObject.AcquireReference<MarkerTable>(version); // Read the data. The returned blob asset reference is a view of the stored data, so don't dispose this blob asset reference. BlobAssetReference<MarkerTable> markerTable = reference.AsBlobAssetReference(); float sum = 0f; ref MarkerTable table = ref markerTable.Value; for (int i = 0; i < table.Times.Length; ++i) { sum += table.Times[i]; } // Dispose the reference, then destroy the object. reference.Dispose(); Object.DestroyImmediate(blobObject); return sum;}

Properties

Property

Description

IsValidWhether the reference points to blob data.

Static Properties

Property

Description

TypeHashHash that identifies the type of the blob root, used to check that a BlobObject stores the type you expect.

Methods

Method

Description

AsBlobAssetReferenceReturns a blob asset reference to the data that this reference points to.
CloneAcquires another reference to the same data. Both references must be disposed.
DisposeReleases this reference to the blob data.