| | 1 | | using JetBrains.Annotations; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Emotes |
| | 6 | | { |
| | 7 | | public class EmoteClipData |
| | 8 | | { |
| 0 | 9 | | public AnimationClip AvatarClip { get; } |
| 0 | 10 | | public bool Loop { get; } |
| | 11 | |
|
| | 12 | | [CanBeNull] private Animation animation; |
| 0 | 13 | | [CanBeNull] public GameObject ExtraContent { get; set; } |
| | 14 | |
|
| | 15 | | [CanBeNull] private Renderer[] renderers; |
| | 16 | |
|
| 0 | 17 | | [CanBeNull] public AudioSource AudioSource { get; } |
| | 18 | |
|
| 726 | 19 | | public EmoteClipData(AnimationClip avatarClip, bool loop = false) |
| | 20 | | { |
| 726 | 21 | | this.AvatarClip = avatarClip; |
| 726 | 22 | | this.Loop = loop; |
| 726 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | public EmoteClipData(AnimationClip mainClip, GameObject container, AudioSource audioSource, bool loop = false) |
| | 26 | | { |
| 0 | 27 | | this.AvatarClip = mainClip; |
| 0 | 28 | | this.Loop = loop; |
| 0 | 29 | | this.ExtraContent = container; |
| 0 | 30 | | this.AudioSource = audioSource; |
| | 31 | |
|
| 0 | 32 | | if (ExtraContent == null) return; |
| | 33 | |
|
| 0 | 34 | | animation = ExtraContent.GetComponentInChildren<Animation>(); |
| | 35 | |
|
| 0 | 36 | | if (animation == null) |
| 0 | 37 | | Debug.LogError($"Animation {AvatarClip.name} extra content does not have an animation"); |
| | 38 | | else |
| | 39 | | { |
| 0 | 40 | | animation.wrapMode = WrapMode.Default; |
| | 41 | |
|
| 0 | 42 | | foreach (AnimationState state in animation) |
| | 43 | | { |
| 0 | 44 | | if (state.clip == AvatarClip) continue; |
| 0 | 45 | | state.wrapMode = Loop ? WrapMode.Loop : WrapMode.Once; |
| | 46 | | } |
| | 47 | | } |
| | 48 | |
|
| 0 | 49 | | renderers = ExtraContent.GetComponentsInChildren<Renderer>(); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Play(int gameObjectLayer, bool spatial, bool occlude) |
| | 53 | | { |
| 0 | 54 | | if (renderers != null) |
| | 55 | | { |
| 0 | 56 | | foreach (Renderer renderer in renderers) |
| | 57 | | { |
| 0 | 58 | | if (renderer.name.Contains("_reference", StringComparison.OrdinalIgnoreCase)) continue; |
| | 59 | |
|
| 0 | 60 | | renderer.enabled = true; |
| 0 | 61 | | renderer.gameObject.layer = gameObjectLayer; |
| 0 | 62 | | renderer.allowOcclusionWhenDynamic = occlude; |
| 0 | 63 | | renderer.forceRenderingOff = false; |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | if (animation != null) |
| | 68 | | { |
| 0 | 69 | | animation.gameObject.layer = gameObjectLayer; |
| 0 | 70 | | animation.cullingType = occlude ? AnimationCullingType.BasedOnRenderers : AnimationCullingType.AlwaysAni |
| 0 | 71 | | animation.enabled = true; |
| | 72 | |
|
| 0 | 73 | | foreach (AnimationState state in animation) |
| | 74 | | { |
| 0 | 75 | | if (state.clip == AvatarClip) continue; |
| | 76 | |
|
| | 77 | | // this reduntant stop is intended, sometimes when animations are triggered their first frame is not |
| 0 | 78 | | animation.Stop(state.clip.name); |
| 0 | 79 | | animation.CrossFade(state.clip.name, 0, PlayMode.StopAll); |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | if (AudioSource == null) return; |
| | 84 | |
|
| 0 | 85 | | AudioSource.spatialBlend = spatial ? 1 : 0; |
| 0 | 86 | | AudioSource.loop = Loop; |
| 0 | 87 | | AudioSource.Play(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void Stop() |
| | 91 | | { |
| 0 | 92 | | if (renderers != null) |
| | 93 | | { |
| 0 | 94 | | foreach (Renderer renderer in renderers) { renderer.enabled = false; } |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | if (animation != null) |
| | 98 | | { |
| 0 | 99 | | foreach (AnimationState state in animation) |
| | 100 | | { |
| 0 | 101 | | if (state.clip == AvatarClip) continue; |
| 0 | 102 | | animation.Stop(state.clip.name); |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | animation.enabled = false; |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | if (AudioSource != null) |
| 0 | 109 | | AudioSource.Stop(); |
| 0 | 110 | | } |
| | 111 | | } |
| | 112 | | } |