public InternalProviderOperation <TObject> Start(IResourceLocation location, float loadDelay)
 {
     m_Result = null;
     Context  = location;
     DelayedActionManager.AddAction((Action)CompleteLoad, loadDelay);
     return(base.Start(location));
 }
            public InternalProviderOperation <TObject> Start(IResourceLocation location, IAsyncOperation <IList <object> > loadDependencyOperation)
            {
                m_Result                           = null;
                m_RequestOperation                 = null;
                m_DependencyOperation              = loadDependencyOperation;
                loadDependencyOperation.Completed += op =>
                {
                    if (op.Status == AsyncOperationStatus.Succeeded)
                    {
                        AssetBundle bundle = op.Result[0] as AssetBundle;
                        if (bundle == null)
                        {
                            var handler = op.Result[0] as DownloadHandlerAssetBundle;
                            if (handler != null)
                            {
                                bundle = handler.assetBundle;
                            }
                        }

                        if (bundle == null)
                        {
                            //TODO - handle error case properly
                            SetResult(default(TObject));
                            OnComplete();
                        }
                        else
                        {
                            var t = typeof(TObject);
                            if (t.IsArray)
                            {
                                m_RequestOperation = bundle.LoadAssetWithSubAssetsAsync(location.InternalId, t.GetElementType());
                            }
                            else if (t.IsGenericType && typeof(IList <>) == t.GetGenericTypeDefinition())
                            {
                                m_RequestOperation = bundle.LoadAssetWithSubAssetsAsync(location.InternalId, t.GetGenericArguments()[0]);
                            }
                            else
                            {
                                m_RequestOperation = bundle.LoadAssetAsync <TObject>(location.InternalId);
                            }

                            if (m_RequestOperation.isDone)
                            {
                                DelayedActionManager.AddAction((Action <AsyncOperation>)OnComplete, 0, m_RequestOperation);
                            }
                            else
                            {
                                m_RequestOperation.completed += OnComplete;
                            }
                        }
                    }
                    else
                    {
                        m_Error = op.OperationException;
                        SetResult(default(TObject));
                        OnComplete();
                    }
                };
                return(base.Start(location));
            }
 /// <summary>
 /// Destroy the current object
 /// </summary>
 public static void Clear()
 {
     if (s_Instance != null)
     {
         s_Instance.DestroyWhenComplete();
     }
     s_Instance = null;
 }
 /// <summary>
 /// Starts the operation.
 /// </summary>
 /// <param name="context">Context object.  This is usually set to the IResourceLocation.</param>
 /// <param name="key">Key value.  This is usually set to the address.</param>
 /// <param name="val">Completed result object.  This may be null if error is set.</param>
 /// <param name="error">Optional exception.  This should be set when val is null.</param>
 public virtual IAsyncOperation <TObject> Start(object context, object key, TObject val, Exception error = null)
 {
     Context            = context;
     OperationException = error;
     Key = key;
     SetResult(val);
     Retain();
     DelayedActionManager.AddAction((Action)InvokeCompletionEvent);
     return(this);
 }
Beispiel #5
0
            public InternalProviderOperation <TObject> StartOp(IResourceLocation location)
            {
                m_Result           = null;
                m_RequestOperation = Resources.LoadAsync <Object>(location.InternalId);

                if (m_RequestOperation.isDone)
                {
                    DelayedActionManager.AddAction((Action <AsyncOperation>)OnComplete, 0, m_RequestOperation);
                }
                else
                {
                    m_RequestOperation.completed += OnComplete;
                }
                return(base.Start(location));
            }
        /// <summary>
        /// Schedule an action to execute after a time delay.  If delay is less than or equal 0, the action will wait until the LateUpdate of this object to be invoked.
        /// </summary>
        /// <param name="action">The delegate to invoke.</param>
        /// <param name="delay">Time delay to wait until invocation.  If less than or equal to 0, the action will be invoked during the LateUpdate call of the current frame.</param>
        /// <param name="parameters">The parameters to be passed to the action when invoked.</param>
        public static void AddAction(Delegate action, float delay = 0, params object[] parameters)
        {
            if (s_Instance == null)
            {
                s_Instance = new GameObject("DelayedActionManager", typeof(DelayedActionManager)).GetComponent <DelayedActionManager>();
                s_Instance.gameObject.hideFlags = HideFlags.HideAndDontSave;
#if UNITY_EDITOR
                if (!Application.isPlaying)
                {
//                    Debug.Log("DelayedActionManager called outside of play mode, registering with EditorApplication.update.");
                    EditorApplication.update += s_Instance.LateUpdate;
                }
#endif
            }
            s_Instance.AddActionInternal(action, delay, parameters);
        }
            public IAsyncOperation <Scene> Start(IResourceLocation location, Scene scene)
            {
                Validate();
                m_Scene = scene;
                Context = location;
                ResourceManagerEventCollector.PostEvent(ResourceManagerEventCollector.EventType.ReleaseSceneAsyncRequest, Context, 0);
                var unloadOp = SceneManager.UnloadSceneAsync(scene);

                if (unloadOp.isDone)
                {
                    DelayedActionManager.AddAction((Action <AsyncOperation>)OnSceneUnloaded, 0, unloadOp);
                }
                else
                {
                    unloadOp.completed += OnSceneUnloaded;
                }
                return(this);
            }
 public InternalOp()
 {
     m_DependencyLoadAction = op =>
     {
         if ((op == null || op.Status == AsyncOperationStatus.Succeeded) && Context is IResourceLocation)
         {
             var loc  = Context as IResourceLocation;
             var path = loc.InternalId;
             if (File.Exists(path))
             {
                 var options = loc.Data as AssetBundleRequestOptions;
                 m_RequestOperation = AssetBundle.LoadFromFileAsync(path, options == null ? 0 : options.Crc);
             }
             else if (path.Contains("://"))
             {
                 m_RequestOperation = CreateWebRequest(loc).SendWebRequest();
             }
             else
             {
                 m_RequestOperation = null;
                 OperationException = new Exception(string.Format("Invalid path in AssetBundleProvider: '{0}'.", path));
                 SetResult(default(TObject));
                 OnComplete();
             }
             if (m_RequestOperation != null)
             {
                 if (m_RequestOperation.isDone)
                 {
                     DelayedActionManager.AddAction((Action <AsyncOperation>)OnComplete, 0, m_RequestOperation);
                 }
                 else
                 {
                     m_RequestOperation.completed += OnComplete;
                 }
             }
         }
         else
         {
             m_Error = op == null ? null : op.OperationException;
             SetResult(default(TObject));
             OnComplete();
         }
     };
 }
            public InternalOp()
            {
                m_Action = op =>
                {
                    if ((op == null || op.Status == AsyncOperationStatus.Succeeded) && Context is IResourceLocation)
                    {
                        if (op != null)
                        {
                            foreach (var result in op.Result)
                            {
                                AssetBundle bundle = result as AssetBundle;
                                if (bundle == null)
                                {
                                    var handler = result as DownloadHandlerAssetBundle;
                                    if (handler != null)
                                    {
                                        bundle = handler.assetBundle;
                                    }
                                }
                            }
                        }

                        m_RequestOperation = SceneManager.LoadSceneAsync((Context as IResourceLocation).InternalId, m_LoadMode);
                        m_Scene            = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
                        if (m_RequestOperation == null || m_RequestOperation.isDone)
                        {
                            DelayedActionManager.AddAction((Action <AsyncOperation>)OnSceneLoaded, 0, m_RequestOperation);
                        }
                        else
                        {
                            m_RequestOperation.completed += OnSceneLoaded;
                        }
                    }
                    else
                    {
                        if (op != null)
                        {
                            m_Error = op.OperationException;
                        }
                        SetResult(default(Scene));
                        OnSceneLoaded(null);
                    }
                };
            }
Beispiel #10
0
 public InternalOp()
 {
     m_Action = op =>
     {
         if ((op == null || op.Status == AsyncOperationStatus.Succeeded) && Context is IResourceLocation)
         {
             var loc  = Context as IResourceLocation;
             var path = loc.InternalId;
             if (File.Exists(path))
             {
                 var text = File.ReadAllText(path);
                 SetResult(m_ConvertFunc(text));
                 DelayedActionManager.AddAction((Action)OnComplete);
             }
             else if (path.Contains("://"))
             {
                 m_RequestOperation = new UnityWebRequest(path, UnityWebRequest.kHttpVerbGET, new DownloadHandlerBuffer(), null).SendWebRequest();
                 if (m_RequestOperation.isDone)
                 {
                     DelayedActionManager.AddAction((Action <AsyncOperation>)OnComplete, 0, m_RequestOperation);
                 }
                 else
                 {
                     m_RequestOperation.completed += OnComplete;
                 }
             }
             else
             {
                 OperationException = new Exception(string.Format("Invalid path in RawDataProvider: '{0}'.", path));
                 SetResult(default(TObject));
                 OnComplete();
             }
         }
         else
         {
             if (op != null)
             {
                 m_Error = op.OperationException;
             }
             SetResult(default(TObject));
             OnComplete();
         }
     };
 }
Beispiel #11
0
            public InternalOp <TObject> Start(IAsyncOperation <TObject> loadOperation, IResourceLocation location, TObject value, InstantiationParameters instantiateParameters)
            {
                Validate();
                m_PrefabResult = null;
                m_InstParams   = instantiateParameters;
                SetResult(value);
                Context      = location;
                m_StartFrame = Time.frameCount;
                if (loadOperation != null)
                {
                    loadOperation.Completed += m_OnLoadOperationCompleteAction;
                }
                else
                {
                    DelayedActionManager.AddAction(m_OnValidResultCompleteAction, 0, Result);
                }

                return(this);
            }
 public InternalProviderOperation <TObject> Start(IResourceLocation location, IAsyncOperation <IList <object> > loadDependencyOperation)
 {
     m_Result = null;
     m_DependencyOperation = loadDependencyOperation;
     m_RequestOperation    = null;
     if (loadDependencyOperation != null && location != null)
     {
         loadDependencyOperation.Completed += obj =>
         {
             m_RequestOperation = UnityWebRequestTexture.GetTexture(location.InternalId).SendWebRequest();
             if (m_RequestOperation.isDone)
             {
                 DelayedActionManager.AddAction((Action <AsyncOperation>)OnComplete, 0, m_RequestOperation);
             }
             else
             {
                 m_RequestOperation.completed += OnComplete;
             }
         };
     }
     return(base.Start(location));
 }
        bool ReleaseCache(CacheList entryList)
        {
            if (entryList.Release())
            {
                if (!entryList.IsDone)
                {
                    if (m_RetryReleaseEntryAction == null)
                    {
                        m_RetryReleaseEntryAction = RetryEntryRelease;
                    }
                    entryList.Retain(); //hold on since this will be retried...
                    DelayedActionManager.AddAction(m_RetryReleaseEntryAction, .2f, entryList);
                    return(false);
                }

                if (m_Lru != null)
                {
                    m_Lru.AddFirst(entryList);
                    while (m_Lru.Count > maxLruCount && m_Lru.Last.Value.IsDone)
                    {
                        m_Lru.Last.Value.ReleaseAssets(m_InternalProvider);
                        m_Lru.RemoveLast();
                    }
                    ResourceManagerEventCollector.PostEvent(ResourceManagerEventCollector.EventType.CacheLruCount, ProviderId + " LRU", m_Lru.Count);
                }
                else
                {
                    entryList.ReleaseAssets(m_InternalProvider);
                }

                if (!m_Cache.Remove(entryList.GetHashCode()))
                {
                    Debug.LogWarningFormat("Unable to find entryList {0} in cache.", entryList.location);
                }
                return(true);
            }
            return(false);
        }