< Summary

Class:PlayVideo
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Room/Assets/_Course Library/Scripts/Test/PlayVideo.cs
Covered lines:38
Uncovered lines:44
Coverable lines:82
Total lines:155
Line coverage:46.3% (38 of 82)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:14
Method coverage:50% (7 of 14)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayVideo()0%000100%
Awake()0%000100%
Start()0%00066.67%
NextClip()0%0000%
PreviousClip()0%0000%
RandomClip()0%0000%
PlayAtIndex(...)0%0000%
Play()0%000100%
Stop()0%000100%
TogglePlayStop()0%0000%
TogglePlayPause()0%0000%
SetPlay(...)0%0000%
ApplyVideoMaterial()0%000100%
OnValidate()0%000100%

File(s)

E:/Unity/Unity Project/VR-Room/Assets/_Course Library/Scripts/Test/PlayVideo.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Diagnostics.CodeAnalysis;
 3using UnityEngine;
 4using UnityEngine.Video;
 5using HenryLab;
 6/// <summary>
 7/// Play a single video or play from a list of videos
 8/// </summary>
 9[RequireComponent(typeof(VideoPlayer))]
 10public class PlayVideo : MonoBehaviour, ITriggerableEntity
 11{
 12    [ExcludeFromCodeCoverage] public float TriggeringTime => 1.5f;
 13    [ExcludeFromCodeCoverage] public string Name => Str.Triggerable;
 14
 15    [ExcludeFromCodeCoverage]
 16    public void Triggerring()
 17    {
 18        TogglePlayStop();
 19        NextClip();
 20        PreviousClip();
 21        TogglePlayPause();
 22    }
 23
 24    [ExcludeFromCodeCoverage]
 25    public void Triggerred()
 26    {
 27        TogglePlayStop();
 28    }
 29
 30    [Tooltip("Whether video should play on load")]
 431    public bool playAtStart = false;
 32
 33    [Tooltip("Material used for playing the video (Uses URP/Unlit by default)")]
 434    public Material videoMaterial = null;
 35
 36    [Tooltip("List of video clips to pull from")]
 437    public List<VideoClip> videoClips = new List<VideoClip>();
 38
 439    private VideoPlayer videoPlayer = null;
 440    private MeshRenderer meshRenderer = null;
 41
 442    private readonly string shaderUsed = "Universal Render Pipeline/Unlit";
 43
 444    private Material offMaterial = null;
 445    private int index = 0;
 46
 47    private void Awake()
 248    {
 249        meshRenderer = GetComponent<MeshRenderer>();
 250        videoPlayer = GetComponent<VideoPlayer>();
 51
 252        if(videoClips.Count > 0)
 253            videoPlayer.clip = videoClips[0];
 54
 255        offMaterial = meshRenderer.material;
 56
 257        videoMaterial = new Material(Shader.Find(shaderUsed));
 258        videoMaterial.color = Color.white;
 259    }
 60
 61
 62    private void Start()
 263    {
 264        if(playAtStart)
 065        {
 066            Play();
 067        }
 68        else
 269        {
 270            Stop();
 271        }
 272    }
 73
 74    public void NextClip()
 075    {
 076        index = ++index % videoClips.Count;
 077        Play();
 078    }
 79
 80    public void PreviousClip()
 081    {
 082        index = --index % videoClips.Count;
 083        Play();
 084    }
 85
 86    public void RandomClip()
 087    {
 088        if(videoClips.Count > 0)
 089        {
 090            index = Random.Range(0, videoClips.Count);
 091            Play();
 092        }
 093    }
 94
 95    public void PlayAtIndex(int value)
 096    {
 097        if(videoClips.Count > 0)
 098        {
 099            index = Mathf.Clamp(value, 0, videoClips.Count);
 0100            Play();
 0101        }
 0102    }
 103
 104    public void Play()
 1105    {
 1106        ApplyVideoMaterial();
 1107        videoPlayer.Play();
 1108    }
 109
 110    public void Stop()
 2111    {
 2112        meshRenderer.material = offMaterial;
 2113        videoPlayer.Stop();
 2114    }
 115
 116    public void TogglePlayStop()
 0117    {
 0118        bool isPlaying = !videoPlayer.isPlaying;
 0119        SetPlay(isPlaying);
 0120    }
 121
 122    public void TogglePlayPause()
 0123    {
 0124        meshRenderer.material = videoMaterial;
 125
 0126        if(videoPlayer.isPlaying)
 0127            videoPlayer.Pause();
 128        else
 0129            videoPlayer.Play();
 0130    }
 131
 132    public void SetPlay(bool value)
 0133    {
 0134        if(value)
 0135        {
 0136            Play();
 0137        }
 138        else
 0139        {
 0140            Stop();
 0141        }
 0142    }
 143
 144    private void ApplyVideoMaterial()
 1145    {
 1146        meshRenderer.material = videoMaterial;
 1147    }
 148
 149    private void OnValidate()
 4150    {
 151
 4152        if(TryGetComponent(out VideoPlayer videoPlayer))
 4153            videoPlayer.targetMaterialProperty = "_BaseMap";
 4154    }
 155}