BlobObject
A Unity object that stores a blob of contiguous bytes that jobs can read without copying the data.
Read time 6 minutesLast updated 3 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.AnimationModule
- Inherits from: Object
public class BlobObject : Object
Remarks
Jobs that the Burst compiler compiles can read the blob data in parallel through a BlobObjectReference<T>. Reading the data doesn't copy it, so reading is cheap enough for performance-critical code.
A is a Object, so it also benefits from the object management that Unity provides. The management includes garbage collection, deduplication of the instances that the same MonoBehaviour references, and authoring in the Unity Editor.
BlobObjectBlobObjectTo store data, call BlobObject.SetBlobAsset with a BlobObjectReference<T>. To read the data, call BlobObject.AcquireReference to get a reference, read the data through BlobObjectReference<T>.Value, and dispose the reference when you finish with it.
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: BlobObjectReference<T>
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;}
Constructors
Constructor | Description |
|---|---|
| BlobObject() | Creates an empty BlobObject. |
Methods
Method | Description |
|---|---|
| AcquireReference | Acquires a reference that keeps the blob data stored in this object alive, even if the object is destroyed or its data is replaced. |
| SetBlobAsset | Replaces the data stored in this object with a copy of the blob data of a reference. |
| SetUntypedBlobData | Replaces the data stored in this object with a copy of the given data, type hash, and version. |
| TryGetUntypedBlobData | Gets the data stored in this object without checking its type or version, for example to migrate data written with an older layout. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |