示例#1
0
        /// <summary>
        /// Try to access to a BlobAssetReference from its key
        /// </summary>
        /// <param name="key">The key associated with the BlobAssetReference when it was added to the cache</param>
        /// <param name="blobAssetReference">The corresponding BlobAssetReference or default if none was found</param>
        /// <typeparam name="T">The type of BlobAsset</typeparam>
        /// <returns></returns>
        public bool TryGet <T>(Hash128 key, out BlobAssetReference <T> blobAssetReference) where T : struct
        {
            var typedHash = ComputeTypedHash(key, typeof(T));

            var res = m_BlobAssets.TryGetValue(typedHash, out var blobData);

            if (res)
            {
                m_CacheHit++;

                blobAssetReference = BlobAssetReference <T> .Create(blobData);

                return(true);
            }

            m_CacheMiss++;
            blobAssetReference = default;
            return(false);
        }
示例#2
0
        /// <summary>
        /// Adds a blob asset where the key that makes it unique is based on the BlobAsset contents itself. If the contents of the generated blob asset is the same as a previously inserted blob asset,
        /// then the passed blobAsset will be disposed and the reference to the blob asset will be replaced with the previously added blob asset
        /// </summary>
        /// <param name="blobAsset">The blob asset that will be inserted or replaced</param>
        /// <typeparam name="T"></typeparam>
        /// <returns>Returns true if the blob asset was added, returns false if the blob asset was disposed and replaced with the previous blob.</returns>
        unsafe public bool AddUniqueBlobAsset <T>(ref BlobAssetReference <T> blobAsset) where T : struct
        {
            Hash128 key  = default;
            ulong   hash = blobAsset.m_data.Header->Hash;

            UnsafeUtility.MemCpy(&key, &blobAsset.m_data.Header->Hash, sizeof(ulong));
            key.Value.w = ComputeTypeHash(typeof(T));

            if (m_BlobAssets.TryGetValue(key, out var previousBlob))
            {
                blobAsset.Dispose();
                blobAsset = BlobAssetReference <T> .Create(previousBlob);

                return(false);
            }

            m_BlobAssets.Add(key, blobAsset.m_data);
            return(true);
        }