public static AsyncOperationMonitor Create(AsyncOperationHandle handle, Callback callback, bool selfDestroy = true)
    {
        GameObject            go      = new GameObject("AsyncOperationMonitor");
        AsyncOperationMonitor monitor = go.AddComponent <AsyncOperationMonitor>();

        monitor.Begin(handle, callback, selfDestroy);
        return(monitor);
    }
Example #2
0
    void GUIBundles()
    {
        var bundles = BundleManager.Instance.Bundles;

        if (bundles == null || bundles.Count == 0)
        {
            return;
        }

        GUILayout.BeginVertical("box");

        if (BundleManager.Instance.HasUpdate() && GUILayout.Button("Download ALL"))
        {
            var handle = Addressables.DownloadDependenciesAsync(BundleManager.Instance.GetUpdateKeysAll(), Addressables.MergeMode.Union);
            if (DownloadingText)
            {
                AsyncOperationMonitor.Create(handle, (finish, percent) =>
                {
                    if (finish)
                    {
                        DownloadingText.text = $"Downloaded";
                        BundleManager.Instance.RefreshBundles();
                        cacheSize = Caches.GetAllCachedSize();
                    }
                    else
                    {
                        DownloadingText.text = $"Downloading {percent:F2} %";
                    }
                });
            }
            else
            {
                handle.Completed += (h) =>
                {
                    BundleManager.Instance.RefreshBundles();
                    cacheSize = Caches.GetAllCachedSize();
                };
            }
        }

        foreach (var bundle in bundles)
        {
            GUILayout.BeginHorizontal("box");
            {
                if (GUILayout.Button(bundle.name) && bundle.size > 0)
                {
                    Debug.Log($"Start Loading {bundle.key}");
                    var handle = Addressables.DownloadDependenciesAsync(bundle.key);
                    if (DownloadingText)
                    {
                        DownloadingText.text = "Start";
                        AsyncOperationMonitor.Create(handle, (finish, percent) =>
                        {
                            if (finish)
                            {
                                DownloadingText.text = $"{bundle.name} Downloaded";
                                BundleManager.Instance.RefreshBundles();
                                cacheSize = Caches.GetAllCachedSize();

                                // Release _aa resource
                                Addressables.Release(handle.Result);
                            }
                            else
                            {
                                DownloadingText.text = $"{bundle.name} Downloading {percent:F2} %";
                            }
                        });
                    }
                    else
                    {
                        handle.Completed += (h) =>
                        {
                            BundleManager.Instance.RefreshBundles();
                            cacheSize = Caches.GetAllCachedSize();
                        };
                    }
                }

                GUILayout.Label($"[ {bundle.size} ]");

                if (!string.IsNullOrEmpty(bundle.bundleName))
                {
                    GUILayout.Button($"{bundle.bundleName.Substring(0, 6)}");
                    GUILayout.Button($"{bundle.bundleHash.Substring(0, 6)}");
                }
                else
                {
                    GUILayout.Label("no bundle name & hash");
                }
            }
            GUILayout.EndHorizontal();
        }
        GUILayout.EndVertical();
    }