/// <summary>
        /// Sets a BlobPtr to point to the given object inside the blob.
        /// </summary>
        /// <param name="ptr">A reference to a blob pointer field in a blob asset.</param>
        /// <param name="obj">The struct that exists in the blob that you want to point to.</param>
        /// <returns>A reference to obj.</returns>
        public ref T SetPointer <T>(ref BlobPtr <T> ptr, ref T obj) where T : struct
        {
            var offsetPtr    = (int *)UnsafeUtility.AddressOf(ref ptr.m_OffsetPtr);
            var objTargetPtr = UnsafeUtility.AddressOf(ref obj);

            ValidateAllocation(offsetPtr);

            if (GetPatchTarget(objTargetPtr, out var target))
            {
                var patch = new OffsetPtrPatch
                {
                    offsetPtr = offsetPtr,
                    target    = target,
                    length    = 0
                };

                m_patches.Add(patch);
            }
            else
            {
                throw new System.ArgumentException("Reference object is not a part of the blob; can only create BlobPtr to objects inside a blob");
            }

            return(ref obj);
        }
        /// <summary>
        /// Allocates enough memory to store a struct of type <typeparamref name="T"/>.
        /// </summary>
        /// <param name="ptr">A reference to a blob pointer field in a blob asset.</param>
        /// <typeparam name="T">The struct data type.</typeparam>
        /// <returns>A reference to the newly allocated struct.</returns>
        public ref T Allocate <T>(ref BlobPtr <T> ptr) where T : struct
        {
            var offsetPtr = (int *)UnsafeUtility.AddressOf(ref ptr.m_OffsetPtr);

            ValidateAllocation(offsetPtr);

            var allocation = Allocate(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>());

            var patch = new OffsetPtrPatch
            {
                offsetPtr = offsetPtr,
                target    = allocation,
                length    = 0
            };

            m_patches.Add(patch);
            return(ref UnsafeUtility.AsRef <T>(AllocationToPointer(allocation)));
        }
Ejemplo n.º 3
0
 public void Allocate <T>(ref BlobPtr <T> ptr) where T : struct
 {
     ptr.m_OffsetPtr = Allocate(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AddressOf(ref ptr));
 }