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 3 minutesLast updated 2 days ago
Definition
- Type: Struct
- Namespace: UnityEngine
- Assembly: UnityEngine.AnimationModule
- Implements: IDisposable
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. Safety checks are always enabled in the Unity Editor and never in a built player.
You can't store the reference in serialized data. Reference the BlobObject itself instead.
The following example uses the , , , and namespaces. It stores a custom blob in a , reads the data back through a , and then disposes the reference and destroys the object.
Unity.AnimationsUnity.CollectionsUnity.EntitiesUnityEngineBlobObjectBlobObjectReferenceAdditional 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 and copy it into a BlobObjectReference. SetBlobAsset copies the data again, so you // can dispose the blob asset and the reference 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)) using (BlobObjectReference<MarkerTable> blobReference = blobAsset.ToBlobObjectReference()) { blobObject.SetBlobAsset(blobReference, 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. Value is a reference to the stored data, not a copy, so keep it in a ref local. float sum = 0f; ref MarkerTable table = ref reference.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 |
|---|---|
| IsValid | Whether the reference points to blob data. |
| Value | A reference to the root of the blob data. |
Static Properties
Property | Description |
|---|---|
| TypeHash | Hash that identifies the type of the blob root, used to check that a BlobObject stores the type you expect. |