/// <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)));
        }
        /// <summary>
        /// Allocates enough memory to store <paramref name="length"/> elements of struct <typeparamref name="T"/>.
        /// </summary>
        /// <param name="ptr">A reference to a BlobArray field in a blob asset.</param>
        /// <param name="length">The number of elements to allocate.</param>
        /// <typeparam name="T">The struct data type.</typeparam>
        /// <returns>A reference to the newly allocated array as a mutable BlobBuilderArray instance.</returns>
        public BlobBuilderArray <T> Allocate <T>(ref BlobArray <T> ptr, int length) where T : struct
        {
            if (length <= 0)
            {
                return(new BlobBuilderArray <T>(null, 0));
            }

            var offsetPtr = (int *)UnsafeUtility.AddressOf(ref ptr.m_OffsetPtr);

            ValidateAllocation(offsetPtr);

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

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

            m_patches.Add(patch);
            return(new BlobBuilderArray <T>(AllocationToPointer(allocation), length));
        }
Esempio n. 4
0
        public BlobBuilderArray <T> Allocate <T>(int length, ref BlobArray <T> ptr) where T : struct
        {
            if (length <= 0)
            {
                throw new ArgumentException("BlobArray length must be greater than 0");
            }

            var offsetPtr = (int *)UnsafeUtility.AddressOf(ref ptr.m_OffsetPtr);

            ValidateAllocation(offsetPtr, "The BlobArray passed to Allocate was not allocated by this BlobBuilder");

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

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

            m_patches.Add(patch);
            return(new BlobBuilderArray <T>(AllocationToPointer(allocation), length));
        }