// Update this asset bundle. public void Tick() { float progressSum = 0; bool done = true; // Update each async request and recalculate data. for (int i = 0; i < asyncRequests.Length; i++) { AsyncResourceRequest request = asyncRequests [i]; request.Tick(); progressSum += request.Progress(); if (!request.IsDone()) { done = false; } } // If finished, fire callback. if (done && callback != null) { callback(); callback = null; } // Record data. progress = progressSum / asyncRequests.Length; isDone = done; }
/// <summary> /// 异步加载资源 /// </summary> /// <param name="resourceName">资源名称</param> /// <param name="func">加载成功的回调</param> public void LoadAsync(string resourceName, LuaFunction func) { var resourceRequest = new AsyncResourceRequest(); resourceRequest.ResourceName = resourceName; resourceRequest.CallBack = func; resourceRequest.Id = Time.realtimeSinceStartup; _asyncResourceRequestList.Add(resourceRequest); StartLoadAsync(); }
// Constructor for an AsyncResourceRequestBundle. public AsyncResourceRequestBundle(ResourceRequest[] requests, Action callback) { // Create AsyncResourceRequest objects for each request. asyncRequests = new AsyncResourceRequest[requests.Length]; for (int i = 0; i < requests.Length; i++) { asyncRequests [i] = new AsyncResourceRequest(requests [i], null); } // Register callback. this.callback = callback; }
// Update all pending async load requests. public static void Update() { // Keep a running list of the requests that are finished and need to get cleared in a cleanup pass. List <int> toRemove = new List <int> (); int i; // Update pass for single requests. for (i = 0; i < singleRequests.Count; i++) { // Update the request. AsyncResourceRequest request = singleRequests [i]; request.Tick(); // If done, mark for removal and fire callback. if (request.IsDone()) { toRemove.Add(i); } } // Garbage collection pass for single requests. for (i = 0; i < toRemove.Count; i++) { singleRequests.RemoveAt(toRemove [i]); } // Clear garbage collection tracker. toRemove.Clear(); // Update pass for bundle requests. for (i = 0; i < bundleRequests.Count; i++) { // Update the request bundle. AsyncResourceRequestBundle bundle = bundleRequests [i]; bundle.Tick(); // If done, mark for removal and fire callback. if (bundle.IsDone()) { toRemove.Add(i); } } // Garbage collection pass for bundle requests. for (i = 0; i < toRemove.Count; i++) { bundleRequests.RemoveAt(toRemove [i]); } }