protected virtual void SendWebRequest(string path) { UnityWebRequest request = new UnityWebRequest(path, UnityWebRequest.kHttpVerbGET, new DownloadHandlerBuffer(), null); if (m_Timeout > 0) { request.timeout = m_Timeout; } m_PI.ResourceManager.WebRequestOverride?.Invoke(request); m_RequestQueueOperation = WebRequestQueue.QueueRequest(request); if (m_RequestQueueOperation.IsDone) { m_RequestOperation = m_RequestQueueOperation.Result; if (m_RequestOperation.isDone) { RequestOperation_completed(m_RequestOperation); } else { m_RequestOperation.completed += RequestOperation_completed; } } else { m_RequestQueueOperation.OnComplete += asyncOperation => { m_RequestOperation = asyncOperation; m_RequestOperation.completed += RequestOperation_completed; }; } }
public IEnumerator WebRequestQueue_CompletesAllOperations() { int numberOfCompletedOperations = 0; int totalOperations = 5000; for (int i = 0; i < totalOperations; i++) { UnityWebRequest uwr = new UnityWebRequest(); var requestOp = WebRequestQueue.QueueRequest(uwr); if (requestOp.IsDone) { numberOfCompletedOperations++; } else { requestOp.OnComplete += op => { numberOfCompletedOperations++; } }; } while (WebRequestQueue.s_QueuedOperations.Count > 0) { yield return(null); } Assert.AreEqual(totalOperations, numberOfCompletedOperations); }
private void BeginOperation() { string path = m_ProvideHandle.Location.InternalId; if (File.Exists(path)) { m_RequestOperation = AssetBundle.LoadFromFileAsync(path, m_Options == null ? 0 : m_Options.Crc); m_RequestOperation.completed += LocalRequestOperationCompleted; } else if (ResourceManagerConfig.ShouldPathUseWebRequest(path)) { var req = CreateWebRequest(m_ProvideHandle.Location); req.disposeDownloadHandlerOnDispose = false; m_WebRequestQueueOperation = WebRequestQueue.QueueRequest(req); if (m_WebRequestQueueOperation.IsDone) { m_RequestOperation = m_WebRequestQueueOperation.Result; m_RequestOperation.completed += WebRequestOperationCompleted; } else { m_WebRequestQueueOperation.OnComplete += asyncOp => { m_RequestOperation = asyncOp; m_RequestOperation.completed += WebRequestOperationCompleted; }; } } else { m_RequestOperation = null; m_ProvideHandle.Complete <AssetBundleResource>(null, false, new Exception(string.Format("Invalid path in AssetBundleProvider: '{0}'.", path))); } }
protected override void Execute() { var db = GetBuilderOfType <BuildScriptFastMode>(m_settings); if (db == null) { UnityEngine.Debug.Log($"Unable to find {nameof(BuildScriptFastMode)} builder in settings assets. Using default Instance and Scene Providers."); } var locator = new AddressableAssetSettingsLocator(m_settings); m_addressables.AddResourceLocator(locator); m_addressables.AddResourceLocator(new DynamicResourceLocator(m_addressables)); m_addressables.ResourceManager.postProfilerEvents = ProjectConfigData.PostProfilerEvents; if (!m_addressables.ResourceManager.postProfilerEvents) { m_Diagnostics.Dispose(); m_Diagnostics = null; m_addressables.ResourceManager.ClearDiagnosticCallbacks(); } //NOTE: for some reason, the data builders can get lost from the settings asset during a domain reload - this only happens in tests and custom instance and scene providers are not needed m_addressables.InstanceProvider = db == null ? new InstanceProvider() : ObjectInitializationData.CreateSerializedInitializationData(db.instanceProviderType.Value).CreateInstance <IInstanceProvider>(); m_addressables.SceneProvider = db == null ? new SceneProvider() : ObjectInitializationData.CreateSerializedInitializationData(db.sceneProviderType.Value).CreateInstance <ISceneProvider>(); m_addressables.ResourceManager.ResourceProviders.Add(new AssetDatabaseProvider()); m_addressables.ResourceManager.ResourceProviders.Add(new TextDataProvider()); m_addressables.ResourceManager.ResourceProviders.Add(new JsonAssetProvider()); m_addressables.ResourceManager.ResourceProviders.Add(new LegacyResourcesProvider()); m_addressables.ResourceManager.ResourceProviders.Add(new AtlasSpriteProvider()); m_addressables.ResourceManager.ResourceProviders.Add(new ContentCatalogProvider(m_addressables.ResourceManager)); WebRequestQueue.SetMaxConcurrentRequests(m_settings.MaxConcurrentWebRequests); if (m_settings.InitializationObjects.Count == 0) { Complete(locator, true, null); } else { List <AsyncOperationHandle> initOperations = new List <AsyncOperationHandle>(); foreach (var io in m_settings.InitializationObjects) { if (io is IObjectInitializationDataProvider) { var ioData = (io as IObjectInitializationDataProvider).CreateObjectInitializationData(); var h = ioData.GetAsyncInitHandle(m_addressables.ResourceManager); initOperations.Add(h); } } groupOp = m_addressables.ResourceManager.CreateGenericGroupOperation(initOperations, true); groupOp.Completed += op => { bool success = op.Status == AsyncOperationStatus.Succeeded; Complete(locator, success, success ? "" : $"{op.DebugName}, status={op.Status}, result={op.Result} failed initialization."); m_addressables.Release(op); }; } }
public void Start(ProvideHandle provideHandle, TextDataProvider rawProvider, bool ignoreFailures) { m_PI = provideHandle; provideHandle.SetProgressCallback(GetPercentComplete); m_Provider = rawProvider; m_IgnoreFailures = ignoreFailures; var path = m_PI.ResourceManager.TransformInternalId(m_PI.Location); if (File.Exists(path)) { #if NET_4_6 if (path.Length >= 260) { path = @"\\?\" + path; } #endif var text = File.ReadAllText(path); object result = m_Provider.Convert(m_PI.Type, text); m_PI.Complete(result, result != null, result == null ? new Exception($"Unable to load asset of type {m_PI.Type} from location {m_PI.Location}.") : null); } else if (ResourceManagerConfig.ShouldPathUseWebRequest(path)) { UnityWebRequest request = new UnityWebRequest(path, UnityWebRequest.kHttpVerbGET, new DownloadHandlerBuffer(), null); m_RequestQueueOperation = WebRequestQueue.QueueRequest(request); if (m_RequestQueueOperation.IsDone) { m_RequestOperation = m_RequestQueueOperation.Result; if (m_RequestOperation.isDone) { RequestOperation_completed(m_RequestOperation); } else { m_RequestOperation.completed += RequestOperation_completed; } } else { m_RequestQueueOperation.OnComplete += asyncOperation => { m_RequestOperation = asyncOperation; m_RequestOperation.completed += RequestOperation_completed; }; } } else { Exception exception = null; //Don't log errors when loading from the persistentDataPath since these files are expected to not exist until created if (!m_IgnoreFailures) { exception = new Exception(string.Format("Invalid path in " + nameof(TextDataProvider) + " : '{0}'.", path)); } m_PI.Complete <object>(null, m_IgnoreFailures, exception); } }
public void LoadCatalogFromBundleAsync() { //Debug.Log($"LoadCatalogFromBundleAsync frame : {Time.frameCount}"); if (m_OpInProgress) { Addressables.LogError($"Operation in progress : A catalog is already being loaded. Please wait for the operation to complete."); return; } m_OpInProgress = true; if (ResourceManagerConfig.ShouldPathUseWebRequest(m_BundlePath)) { var req = UnityWebRequestAssetBundle.GetAssetBundle(m_BundlePath); if (m_WebRequestTimeout > 0) { req.timeout = m_WebRequestTimeout; } m_WebRequestQueueOperation = WebRequestQueue.QueueRequest(req); if (m_WebRequestQueueOperation.IsDone) { m_RequestOperation = m_WebRequestQueueOperation.Result; if (m_RequestOperation.isDone) { WebRequestOperationCompleted(m_RequestOperation); } else { m_RequestOperation.completed += WebRequestOperationCompleted; } } else { m_WebRequestQueueOperation.OnComplete += asyncOp => { m_RequestOperation = asyncOp; m_RequestOperation.completed += WebRequestOperationCompleted; }; } } else { m_LoadBundleRequest = AssetBundle.LoadFromFileAsync(m_BundlePath); m_LoadBundleRequest.completed += loadOp => { if (loadOp is AssetBundleCreateRequest createRequest && createRequest.assetBundle != null) { m_CatalogAssetBundle = createRequest.assetBundle; m_LoadTextAssetRequest = m_CatalogAssetBundle.LoadAllAssetsAsync <TextAsset>(); if (m_LoadTextAssetRequest.isDone) { LoadTextAssetRequestComplete(m_LoadTextAssetRequest); } m_LoadTextAssetRequest.completed += LoadTextAssetRequestComplete; }
protected override void BeginOperationImpl(UnityWebRequest request) { m_WebRequestQueueOperation = WebRequestQueue.QueueRequest(request); if (m_WebRequestQueueOperation.IsDone) { OnQueueOperationCompleted(m_WebRequestQueueOperation.Result); return; } m_WebRequestQueueOperation.OnComplete += OnQueueOperationCompleted; }
/// <summary> /// Sends a payload to the service asynchronously. /// </summary> public async Task <XDocument> SendAndRecieveAsync(int cacheTimeout, CancellationToken token) { XDocument doc = await this.GetCachedDocument(cacheTimeout); if (doc == null) { for (int i = 0; i < 5; i++) { try { token.ThrowIfCancellationRequested(); this.uriBuilder.Query = this.CreateQueryString(); doc = await WebRequestQueue.SendAsync(this.uriBuilder.ToString(), token); // Verify that OBA sent us a valid document and that it's status code is 200: int returnCode = doc.Root.GetFirstElementValue <int>("code"); if (returnCode != 200) { string text = doc.Root.GetFirstElementValue <string>("text"); throw new ObaException(returnCode, text); } await this.TrySaveCachedDocument(doc, cacheTimeout); return(doc); } catch (Exception e) { // Make sure ObaExceptions bubble up because we expect them. // Note 401 means busy if (e is ObaException) { if (((ObaException)e).ErrorCode != 401) { throw; } } else if (e is OperationCanceledException) { throw; } } // If we keep getting 401s (permission denied), then we just need to keep retrying. await Task.Delay(50); } } // We tried and waited as long as we could....or we had a cached copy: return(doc); }
private void BeginOperation() { Debug.LogFormat("[{0}.{1}] location={2}", nameof(WebRequestBundleResource), nameof(BeginOperation), provideHandle.Location); var req = CreateWebRequest(provideHandle.Location); req.disposeDownloadHandlerOnDispose = false; webRequestQueueOperation = WebRequestQueue.QueueRequest(req); if (webRequestQueueOperation.IsDone) { OnQueueOperationCompleted(webRequestQueueOperation.Result); return; } webRequestQueueOperation.OnComplete += OnQueueOperationCompleted; }
protected override void Execute() { Addressables.LogFormat("Addressables - runtime data operation completed with status = {0}, result = {1}.", m_rtdOp.Status, m_rtdOp.Result); if (m_rtdOp.Result == null) { Addressables.LogWarningFormat("Addressables - Unable to load runtime data at location {0}.", m_rtdOp); Complete(Result, false, string.Format("Addressables - Unable to load runtime data at location {0}.", m_rtdOp)); return; } #if UNITY_2019_3_OR_NEWER Addressables.LogFormat("Initializing Addressables version {0}.", m_rtdOp.Result.AddressablesVersion); #endif var rtd = m_rtdOp.Result; m_Addressables.ResourceManager.postProfilerEvents = rtd.ProfileEvents; WebRequestQueue.SetMaxConcurrentRequests(rtd.MaxConcurrentWebRequests); m_Addressables.Release(m_rtdOp); if (rtd.CertificateHandlerType != null) { m_Addressables.ResourceManager.CertificateHandlerInstance = Activator.CreateInstance(rtd.CertificateHandlerType) as CertificateHandler; } #if UNITY_EDITOR if (UnityEditor.EditorUserBuildSettings.activeBuildTarget.ToString() != rtd.BuildTarget) { Addressables.LogErrorFormat("Addressables - runtime data was built with a different build target. Expected {0}, but data was built with {1}. Certain assets may not load correctly including shaders. You can rebuild player content via the Addressables window.", UnityEditor.EditorUserBuildSettings.activeBuildTarget, rtd.BuildTarget); } #endif if (!rtd.LogResourceManagerExceptions) { ResourceManager.ExceptionHandler = null; } if (!rtd.ProfileEvents) { m_Diagnostics.Dispose(); m_Diagnostics = null; m_Addressables.ResourceManager.ClearDiagnosticCallbacks(); } Addressables.Log("Addressables - loading initialization objects."); ContentCatalogProvider ccp = m_Addressables.ResourceManager.ResourceProviders .FirstOrDefault(rp => rp.GetType() == typeof(ContentCatalogProvider)) as ContentCatalogProvider; if (ccp != null) { ccp.DisableCatalogUpdateOnStart = rtd.DisableCatalogUpdateOnStartup; ccp.IsLocalCatalogInBundle = rtd.IsLocalCatalogInBundle; } var locMap = new ResourceLocationMap("CatalogLocator", rtd.CatalogLocations); m_Addressables.AddResourceLocator(locMap); IList <IResourceLocation> catalogs; if (!locMap.Locate(ResourceManagerRuntimeData.kCatalogAddress, typeof(ContentCatalogData), out catalogs)) { Addressables.LogWarningFormat( "Addressables - Unable to find any catalog locations in the runtime data."); m_Addressables.RemoveResourceLocator(locMap); Complete(Result, false, "Addressables - Unable to find any catalog locations in the runtime data."); } else { Addressables.LogFormat("Addressables - loading content catalogs, {0} found.", catalogs.Count); m_loadCatalogOp = LoadContentCatalogInternal(catalogs, 0, locMap); } }