< Summary

Class:PlayVideo
Assembly:Test
File(s):D:/--UnityProject/VR/VRExplorer_subjects/VR-Room/Assets/_Course Library/Scripts/Test/PlayVideo.cs
Covered lines:61
Uncovered lines:21
Coverable lines:82
Total lines:155
Line coverage:74.3% (61 of 82)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:14
Method coverage:85.7% (12 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%000100%
PreviousClip()0%000100%
RandomClip()0%0000%
PlayAtIndex(...)0%0000%
Play()0%000100%
Stop()0%000100%
TogglePlayStop()0%000100%
TogglePlayPause()0%00083.33%
SetPlay(...)0%00066.67%
ApplyVideoMaterial()0%000100%
OnValidate()0%000100%

File(s)

D:/--UnityProject/VR/VRExplorer_subjects/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 VRExplorer;
 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")]
 531    public bool playAtStart = false;
 32
 33    [Tooltip("Material used for playing the video (Uses URP/Unlit by default)")]
 534    public Material videoMaterial = null;
 35
 36    [Tooltip("List of video clips to pull from")]
 537    public List<VideoClip> videoClips = new List<VideoClip>();
 38
 539    private VideoPlayer videoPlayer = null;
 540    private MeshRenderer meshRenderer = null;
 41
 542    private readonly string shaderUsed = "Universal Render Pipeline/Unlit";
 43
 544    private Material offMaterial = null;
 545    private int index = 0;
 46
 47    private void Awake()
 448    {
 449        meshRenderer = GetComponent<MeshRenderer>();
 450        videoPlayer = GetComponent<VideoPlayer>();
 51
 452        if(videoClips.Count > 0)
 453            videoPlayer.clip = videoClips[0];
 54
 455        offMaterial = meshRenderer.material;
 56
 457        videoMaterial = new Material(Shader.Find(shaderUsed));
 458        videoMaterial.color = Color.white;
 459    }
 60
 61
 62    private void Start()
 463    {
 464        if(playAtStart)
 065        {
 066            Play();
 067        }
 68        else
 469        {
 470            Stop();
 471        }
 472    }
 73
 74    public void NextClip()
 275    {
 276        index = ++index % videoClips.Count;
 277        Play();
 278    }
 79
 80    public void PreviousClip()
 281    {
 282        index = --index % videoClips.Count;
 283        Play();
 284    }
 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()
 4105    {
 4106        ApplyVideoMaterial();
 4107        videoPlayer.Play();
 4108    }
 109
 110    public void Stop()
 8111    {
 8112        meshRenderer.material = offMaterial;
 8113        videoPlayer.Stop();
 8114    }
 115
 116    public void TogglePlayStop()
 4117    {
 4118        bool isPlaying = !videoPlayer.isPlaying;
 4119        SetPlay(isPlaying);
 4120    }
 121
 122    public void TogglePlayPause()
 5123    {
 5124        meshRenderer.material = videoMaterial;
 125
 5126        if(videoPlayer.isPlaying)
 0127            videoPlayer.Pause();
 128        else
 5129            videoPlayer.Play();
 5130    }
 131
 132    public void SetPlay(bool value)
 4133    {
 4134        if(value)
 0135        {
 0136            Play();
 0137        }
 138        else
 4139        {
 4140            Stop();
 4141        }
 4142    }
 143
 144    private void ApplyVideoMaterial()
 4145    {
 4146        meshRenderer.material = videoMaterial;
 4147    }
 148
 149    private void OnValidate()
 5150    {
 151
 5152        if(TryGetComponent(out VideoPlayer videoPlayer))
 5153            videoPlayer.targetMaterialProperty = "_BaseMap";
 5154    }
 155}