< Summary

Class:DCL.Emotes.EmoteClipData
Assembly:AvatarAssets
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarAssets/EmoteClipData.cs
Covered lines:4
Uncovered lines:49
Coverable lines:53
Total lines:112
Line coverage:7.5% (4 of 53)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:9
Method coverage:11.1% (1 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EmoteClipData(...)0%110100%
EmoteClipData(...)0%72800%
Play(...)0%1821300%
Stop()0%72800%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarAssets/EmoteClipData.cs

#LineLine coverage
 1using JetBrains.Annotations;
 2using System;
 3using UnityEngine;
 4
 5namespace DCL.Emotes
 6{
 7    public class EmoteClipData
 8    {
 09        public AnimationClip AvatarClip { get; }
 010        public bool Loop { get; }
 11
 12        [CanBeNull] private Animation animation;
 013        [CanBeNull] public GameObject ExtraContent { get; set; }
 14
 15        [CanBeNull] private Renderer[] renderers;
 16
 017        [CanBeNull] public AudioSource AudioSource { get; }
 18
 72619        public EmoteClipData(AnimationClip avatarClip, bool loop = false)
 20        {
 72621            this.AvatarClip = avatarClip;
 72622            this.Loop = loop;
 72623        }
 24
 025        public EmoteClipData(AnimationClip mainClip, GameObject container, AudioSource audioSource, bool loop = false)
 26        {
 027            this.AvatarClip = mainClip;
 028            this.Loop = loop;
 029            this.ExtraContent = container;
 030            this.AudioSource = audioSource;
 31
 032            if (ExtraContent == null) return;
 33
 034            animation = ExtraContent.GetComponentInChildren<Animation>();
 35
 036            if (animation == null)
 037                Debug.LogError($"Animation {AvatarClip.name} extra content does not have an animation");
 38            else
 39            {
 040                animation.wrapMode = WrapMode.Default;
 41
 042                foreach (AnimationState state in animation)
 43                {
 044                    if (state.clip == AvatarClip) continue;
 045                    state.wrapMode = Loop ? WrapMode.Loop : WrapMode.Once;
 46                }
 47            }
 48
 049            renderers = ExtraContent.GetComponentsInChildren<Renderer>();
 050        }
 51
 52        public void Play(int gameObjectLayer, bool spatial, bool occlude)
 53        {
 054            if (renderers != null)
 55            {
 056                foreach (Renderer renderer in renderers)
 57                {
 058                    if (renderer.name.Contains("_reference", StringComparison.OrdinalIgnoreCase)) continue;
 59
 060                    renderer.enabled = true;
 061                    renderer.gameObject.layer = gameObjectLayer;
 062                    renderer.allowOcclusionWhenDynamic = occlude;
 063                    renderer.forceRenderingOff = false;
 64                }
 65            }
 66
 067            if (animation != null)
 68            {
 069                animation.gameObject.layer = gameObjectLayer;
 070                animation.cullingType = occlude ? AnimationCullingType.BasedOnRenderers : AnimationCullingType.AlwaysAni
 071                animation.enabled = true;
 72
 073                foreach (AnimationState state in animation)
 74                {
 075                    if (state.clip == AvatarClip) continue;
 76
 77                    // this reduntant stop is intended, sometimes when animations are triggered their first frame is not
 078                    animation.Stop(state.clip.name);
 079                    animation.CrossFade(state.clip.name, 0, PlayMode.StopAll);
 80                }
 81            }
 82
 083            if (AudioSource == null) return;
 84
 085            AudioSource.spatialBlend = spatial ? 1 : 0;
 086            AudioSource.loop = Loop;
 087            AudioSource.Play();
 088        }
 89
 90        public void Stop()
 91        {
 092            if (renderers != null)
 93            {
 094                foreach (Renderer renderer in renderers) { renderer.enabled = false; }
 95            }
 96
 097            if (animation != null)
 98            {
 099                foreach (AnimationState state in animation)
 100                {
 0101                    if (state.clip == AvatarClip) continue;
 0102                    animation.Stop(state.clip.name);
 103                }
 104
 0105                animation.enabled = false;
 106            }
 107
 0108            if (AudioSource != null)
 0109                AudioSource.Stop();
 0110        }
 111    }
 112}