| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using AvatarSystem; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Emotes |
| | 8 | | { |
| | 9 | | public class EmoteAnimationLoader : IEmoteAnimationLoader |
| | 10 | | { |
| | 11 | | private readonly IWearableRetriever retriever; |
| 2 | 12 | | public AnimationClip loadedAnimationClip { get; internal set; } |
| | 13 | |
|
| 10 | 14 | | public EmoteAnimationLoader(IWearableRetriever retriever) |
| | 15 | | { |
| 10 | 16 | | this.retriever = retriever; |
| 10 | 17 | | } |
| | 18 | |
|
| | 19 | | public async UniTask LoadEmote(GameObject container, WearableItem emote, string bodyShapeId, CancellationToken c |
| | 20 | | { |
| 7 | 21 | | if (container == null) |
| 1 | 22 | | throw new NullReferenceException("Container cannot be null"); |
| | 23 | |
|
| 6 | 24 | | if (emote == null) |
| 1 | 25 | | throw new NullReferenceException("Emote cannot be null"); |
| | 26 | |
|
| 5 | 27 | | if (string.IsNullOrEmpty(bodyShapeId)) |
| 2 | 28 | | throw new NullReferenceException("bodyShapeId cannot be null or empty"); |
| | 29 | |
|
| 3 | 30 | | ct.ThrowIfCancellationRequested(); |
| | 31 | |
|
| 2 | 32 | | WearableItem.Representation representation = emote.GetRepresentation(bodyShapeId); |
| | 33 | |
|
| 3 | 34 | | if (representation == null) { throw new Exception($"No representation for {bodyShapeId} of emote: {emote.id} |
| | 35 | |
|
| 1 | 36 | | Rendereable rendereable = await retriever.Retrieve(container, emote.GetContentProvider(bodyShapeId), emote.b |
| | 37 | |
|
| 1 | 38 | | var animation = rendereable.container.GetComponentInChildren<Animation>(); |
| | 39 | |
|
| 1 | 40 | | if (animation == null) |
| | 41 | | { |
| 0 | 42 | | Debug.LogError("Animation component not found in the container for emote " + emote.id); |
| 0 | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| 1 | 46 | | animation.enabled = false; |
| 1 | 47 | | var animationClip = animation.clip; |
| | 48 | |
|
| 1 | 49 | | if (animationClip == null) |
| | 50 | | { |
| 0 | 51 | | Debug.LogError("AnimationClip not found in the container for emote " + emote.id); |
| 0 | 52 | | return; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | //Setting animation name equal to emote id to avoid unity animation clip duplication on Animation.AddClip() |
| 1 | 56 | | this.loadedAnimationClip = animationClip; |
| 1 | 57 | | animationClip.name = emote.id; |
| 1 | 58 | | } |
| | 59 | |
|
| | 60 | | public void Dispose() |
| | 61 | | { |
| 0 | 62 | | retriever?.Dispose(); |
| 0 | 63 | | } |
| | 64 | | } |
| | 65 | | } |