#if UNITY_EDITOR using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceLocations; using UnityEngine.ResourceManagement.Util; namespace UnityEngine.ResourceManagement.ResourceProviders.Simulation { /// /// Custom version of AssetBundleRequestOptions used to compute needed download size while bypassing the cache. In the future a virtual cache may be implemented. /// [Serializable] public class VirtualAssetBundleRequestOptions : AssetBundleRequestOptions { /// public override long ComputeSize(IResourceLocation location, ResourceManager resourceManager) { var id = resourceManager == null ? location.InternalId : resourceManager.TransformInternalId(location); if (!ResourceManagerConfig.IsPathRemote(id)) return 0; return BundleSize; } } /// /// Provides assets from virtual asset bundles. Actual loads are done through the AssetDatabase API. /// [DisplayName("Assets from Virtual Bundles")] public class VirtualBundledAssetProvider : ResourceProviderBase { /// /// Default copnstructor. /// public VirtualBundledAssetProvider() { m_ProviderId = typeof(BundledAssetProvider).FullName; } class InternalOp { VBAsyncOperation m_RequestOperation; ProvideHandle m_PI; public void Start(ProvideHandle provideHandle, VirtualAssetBundle bundle) { m_PI = provideHandle; provideHandle.SetWaitForCompletionCallback(WaitForCompletionHandler); m_RequestOperation = bundle.LoadAssetAsync(m_PI, m_PI.Location); m_RequestOperation.Completed += RequestOperation_Completed; } private bool WaitForCompletionHandler() { return m_RequestOperation.WaitForCompletion(); } private void RequestOperation_Completed(VBAsyncOperation obj) { bool success = (obj.Result != null && m_PI.Type.IsAssignableFrom(obj.Result.GetType())) && obj.OperationException == null; m_PI.Complete(obj.Result, success, obj.OperationException); } public float GetPercentComplete() { return m_RequestOperation != null ? m_RequestOperation.PercentComplete : 0.0f; } } public override void Provide(ProvideHandle provideHandle) { List deps = new List(); // TODO: garbage. need to pass actual count and reuse the list provideHandle.GetDependencies(deps); VirtualAssetBundle bundle = deps[0] as VirtualAssetBundle; if (bundle == null) { provideHandle.Complete(null, false, new Exception($"Unable to load asset of type {provideHandle.Type} from location {provideHandle.Location}.")); } else { new InternalOp().Start(provideHandle, bundle); } } } } #endif