|
| 1 | +using Cysharp.Threading.Tasks; |
| 2 | +using System; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.EventSystems; |
| 5 | +using UnityEngine.UI; |
| 6 | + |
| 7 | +namespace Extensions.Unity.ImageLoader |
| 8 | +{ |
| 9 | + public static partial class FutureEx |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Set image into array of the generic target instances |
| 13 | + /// </summary> |
| 14 | + /// <param name="setter">Setter function that gets Target instance and Reference<Sprite> instance, it should set the Sprite value into Target instance</param> |
| 15 | + /// <param name="targets">Array of generic Target instances</param> |
| 16 | + /// <returns>Returns async Future</returns> |
| 17 | + public static Future<Reference<Sprite>> ThenSet<T>(this Future<Reference<Sprite>> future, Action<T, Reference<Sprite>> setter, params T[] targets) |
| 18 | + { |
| 19 | + if ((targets?.Length ?? 0) == 0) |
| 20 | + { |
| 21 | + future.FailToLoad(new Exception("No targets to set image")); |
| 22 | + return future; |
| 23 | + } |
| 24 | + |
| 25 | + return future.Then(reference => |
| 26 | + { |
| 27 | + UniTask.Post(() => // using only MainThread to set any images to any targets |
| 28 | + { |
| 29 | + foreach (var target in targets) |
| 30 | + { |
| 31 | + if (target is UIBehaviour uiBehaviour) |
| 32 | + { |
| 33 | + if (IsDestroyed(uiBehaviour)) |
| 34 | + { |
| 35 | + if (ImageLoader.settings.debugLevel <= DebugLevel.Warning) |
| 36 | + Debug.LogWarning($"The target UIBehaviour is destroyed. Can't set image into it. Skipping."); |
| 37 | + continue; |
| 38 | + } |
| 39 | + } |
| 40 | + setter?.Invoke(target, reference); |
| 41 | + if (target is Component monoBehaviour) |
| 42 | + reference.AddTo(monoBehaviour.GetCancellationTokenOnDestroy()); |
| 43 | + } |
| 44 | + }); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Set image into array of Images |
| 50 | + /// </summary> |
| 51 | + /// <param name="images">Array of Images</param> |
| 52 | + /// <returns>Returns async Future</returns> |
| 53 | + public static Future<Reference<Sprite>> ThenSet(this Future<Reference<Sprite>> future, params Image[] images) |
| 54 | + => future.ThenSet((target, reference) => target.sprite = reference.Value, images); |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Set image into array of RawImages |
| 58 | + /// </summary> |
| 59 | + /// <param name="images">Array of RawImages</param> |
| 60 | + /// <returns>Returns async Future</returns> |
| 61 | + public static Future<Reference<Sprite>> ThenSet(this Future<Reference<Sprite>> future, params RawImage[] rawImages) |
| 62 | + => future.ThenSet((target, reference) => target.texture = reference.Value.texture, rawImages); |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Set image into array of SpriteRenderers |
| 66 | + /// </summary> |
| 67 | + /// <param name="images">Array of SpriteRenderers</param> |
| 68 | + /// <returns>Returns async Future</returns> |
| 69 | + public static Future<Reference<Sprite>> ThenSet(this Future<Reference<Sprite>> future, params SpriteRenderer[] spriteRenderers) |
| 70 | + => future.ThenSet((target, reference) => target.sprite = reference.Value, spriteRenderers); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Set image into array of Materials |
| 74 | + /// </summary> |
| 75 | + /// <param name="images">Array of Materials</param> |
| 76 | + /// <returns>Returns async Future</returns> |
| 77 | + public static Future<Reference<Sprite>> ThenSet(this Future<Reference<Sprite>> future, string propertyName = "_MainTex", params Material[] materials) |
| 78 | + => future.ThenSet((target, reference) => target.SetTexture(propertyName, reference.Value.texture), materials); |
| 79 | + } |
| 80 | +} |
0 commit comments