|
9 | 9 | using UnityEngine.AddressableAssets;
|
10 | 10 | using UnityEngine.ResourceManagement.AsyncOperations;
|
11 | 11 | using Utilities.Async;
|
12 |
| -using Utilities.Async.Addressables; |
13 | 12 |
|
14 | 13 | namespace Utilities.Extensions
|
15 | 14 | {
|
@@ -38,110 +37,133 @@ public static void Release(this AsyncOperationHandle handle)
|
38 | 37 | /// </summary>
|
39 | 38 | /// <param name="key">Path.</param>
|
40 | 39 | /// <param name="progress">Optional, <see cref="IProgress{T}"/></param>
|
41 |
| - public static async Task DownloadAddressableAsync(object key, IProgress<float> progress = null) |
| 40 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/></param> |
| 41 | + public static async Task DownloadAddressableAsync(object key, IProgress<float> progress = null, CancellationToken cancellationToken = default) |
42 | 42 | {
|
43 | 43 | var downloadSizeOp = Addressables.GetDownloadSizeAsync(key);
|
44 |
| - var downloadSize = await downloadSizeOp; |
45 |
| - downloadSizeOp.Release(); |
| 44 | + long downloadSize; |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + downloadSize = await downloadSizeOp.Task.WithCancellation(cancellationToken); |
| 49 | + } |
| 50 | + finally |
| 51 | + { |
| 52 | + downloadSizeOp.Release(); |
| 53 | + } |
46 | 54 |
|
47 | 55 | if (downloadSize > 0)
|
48 | 56 | {
|
49 |
| - await Addressables.DownloadDependenciesAsync(key).AwaitWithProgress(progress); |
| 57 | + await Addressables.DownloadDependenciesAsync(key).AwaitWithProgress(progress, true, cancellationToken); |
50 | 58 | }
|
51 | 59 | }
|
52 | 60 |
|
53 | 61 | /// <summary>
|
54 | 62 | /// Wait on the <see cref="AsyncOperationHandle{T}"/> with the provided <see cref="IProgress{T}"/>
|
55 | 63 | /// </summary>
|
56 | 64 | /// <typeparam name="T"></typeparam>
|
57 |
| - /// <param name="operation"></param> |
58 |
| - /// <param name="progress"></param> |
59 |
| - /// <param name="autoRelease"></param> |
60 |
| - public static async Task<T> AwaitWithProgress<T>(this AsyncOperationHandle<T> operation, IProgress<float> progress, bool autoRelease = true) |
| 65 | + /// <param name="operation"><see cref="AsyncOperationHandle{T}"/></param> |
| 66 | + /// <param name="progress">Optional, <see cref="IProgress{T}"/></param> |
| 67 | + /// <param name="autoRelease">Should the <see cref="AsyncOperationHandle{T}"/> be automatically released? Defaults to true.</param> |
| 68 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/></param> |
| 69 | + public static async Task<T> AwaitWithProgress<T>(this AsyncOperationHandle<T> operation, IProgress<float> progress, bool autoRelease = true, CancellationToken cancellationToken = default) |
61 | 70 | {
|
62 | 71 | Thread backgroundThread = null;
|
63 | 72 |
|
64 | 73 | if (progress != null)
|
65 | 74 | {
|
66 |
| - backgroundThread = new Thread(() => ProgressThread(operation, progress)) |
| 75 | + backgroundThread = new Thread(() => ProgressThread(operation, progress, cancellationToken)) |
67 | 76 | {
|
68 | 77 | IsBackground = true
|
69 | 78 | };
|
70 | 79 | }
|
71 | 80 |
|
72 |
| - backgroundThread?.Start(); |
73 |
| - var result = await operation.Task; |
74 |
| - backgroundThread?.Join(); |
75 |
| - |
76 |
| - var opException = operation.OperationException; |
| 81 | + T result; |
77 | 82 |
|
78 |
| - if (autoRelease) |
| 83 | + try |
79 | 84 | {
|
80 |
| - operation.Release(); |
| 85 | + backgroundThread?.Start(); |
| 86 | + result = await operation.Task.WithCancellation(cancellationToken); |
81 | 87 | }
|
82 |
| - |
83 |
| - if (opException != null) |
| 88 | + finally |
84 | 89 | {
|
85 |
| - throw opException; |
86 |
| - } |
| 90 | + backgroundThread?.Join(); |
| 91 | + progress?.Report(100f); |
| 92 | + |
| 93 | + var opException = operation.OperationException; |
87 | 94 |
|
88 |
| - progress?.Report(100f); |
| 95 | + if (autoRelease) |
| 96 | + { |
| 97 | + operation.Release(); |
| 98 | + } |
| 99 | + if (opException != null) |
| 100 | + { |
| 101 | + throw opException; |
| 102 | + } |
| 103 | + } |
89 | 104 |
|
90 | 105 | return result;
|
91 | 106 | }
|
92 | 107 |
|
93 | 108 | /// <summary>
|
94 | 109 | /// Wait on the <see cref="AsyncOperationHandle"/> with the provided <see cref="IProgress{T}"/>
|
95 | 110 | /// </summary>
|
96 |
| - /// <param name="operation"></param> |
97 |
| - /// <param name="progress"></param> |
98 |
| - /// <param name="autoRelease"></param> |
99 |
| - public static async Task AwaitWithProgress(this AsyncOperationHandle operation, IProgress<float> progress, bool autoRelease = true) |
| 111 | + /// <param name="operation"><see cref="AsyncOperationHandle"/></param> |
| 112 | + /// <param name="progress">Optional, <see cref="IProgress{T}"/></param> |
| 113 | + /// <param name="autoRelease">Should the <see cref="AsyncOperationHandle"/> be automatically released? Defaults to true.</param> |
| 114 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/></param> |
| 115 | + public static async Task AwaitWithProgress(this AsyncOperationHandle operation, IProgress<float> progress, bool autoRelease = true, CancellationToken cancellationToken = default) |
100 | 116 | {
|
101 | 117 | Thread backgroundThread = null;
|
102 | 118 |
|
103 | 119 | if (progress != null)
|
104 | 120 | {
|
105 |
| - backgroundThread = new Thread(() => ProgressThread(operation, progress)) |
| 121 | + backgroundThread = new Thread(() => ProgressThread(operation, progress, cancellationToken)) |
106 | 122 | {
|
107 | 123 | IsBackground = true
|
108 | 124 | };
|
109 | 125 | }
|
110 | 126 |
|
111 |
| - backgroundThread?.Start(); |
112 |
| - await operation.Task; |
113 |
| - backgroundThread?.Join(); |
114 |
| - |
115 |
| - var opException = operation.OperationException; |
116 |
| - |
117 |
| - if (autoRelease) |
| 127 | + try |
118 | 128 | {
|
119 |
| - operation.Release(); |
| 129 | + backgroundThread?.Start(); |
| 130 | + await operation.Task.WithCancellation(cancellationToken); |
120 | 131 | }
|
121 |
| - |
122 |
| - if (opException != null) |
| 132 | + finally |
123 | 133 | {
|
124 |
| - throw opException; |
125 |
| - } |
| 134 | + backgroundThread?.Join(); |
| 135 | + var opException = operation.OperationException; |
| 136 | + |
| 137 | + if (autoRelease) |
| 138 | + { |
| 139 | + operation.Release(); |
| 140 | + } |
| 141 | + |
| 142 | + if (opException != null) |
| 143 | + { |
| 144 | + throw opException; |
| 145 | + } |
126 | 146 |
|
127 |
| - progress?.Report(100f); |
| 147 | + progress?.Report(100f); |
| 148 | + } |
128 | 149 | }
|
129 | 150 |
|
130 |
| - private static async void ProgressThread(AsyncOperationHandle handle, IProgress<float> progress) |
| 151 | + private static async void ProgressThread(AsyncOperationHandle handle, IProgress<float> progress, CancellationToken cancellationToken) |
131 | 152 | {
|
132 |
| - await Awaiters.UnityMainThread; |
133 |
| - |
134 | 153 | try
|
135 | 154 | {
|
136 |
| - while (handle.IsValid() && !handle.IsDone) |
| 155 | + // ensure we're on main thread. |
| 156 | + await Awaiters.UnityMainThread; |
| 157 | + |
| 158 | + while (handle.IsValid() && !handle.IsDone && !cancellationToken.IsCancellationRequested) |
137 | 159 | {
|
138 | 160 | if (handle.OperationException != null)
|
139 | 161 | {
|
140 | 162 | break;
|
141 | 163 | }
|
142 | 164 |
|
143 | 165 | progress.Report(handle.PercentComplete * 100f);
|
144 |
| - await Awaiters.UnityMainThread; |
| 166 | + await Task.Yield(); |
145 | 167 | }
|
146 | 168 | }
|
147 | 169 | catch (Exception)
|
|
0 commit comments