AlphaVideoHandler.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. public class AlphaVideoHandler : MonoBehaviour
  6. {
  7. public static AlphaVideoHandler Instance;
  8. private VideoPlayer videoPlayer;
  9. // ==================================================
  10. private void Awake() => Instance = this;
  11. private void Start() => Init();
  12. private void Update() => ShortcutKey();
  13. // ==================================================
  14. private void Init()
  15. {
  16. videoPlayer = GetComponent<VideoPlayer>();
  17. StartCoroutine(nameof(InitAction));
  18. return;
  19. }
  20. private IEnumerator InitAction()
  21. {
  22. yield return new WaitForSeconds(1f); // 为了视频加载完毕而延迟
  23. videoPlayer.Play();
  24. yield return new WaitForSeconds(0.2f); // 显示擦除的短暂空白
  25. videoPlayer.Pause();
  26. yield break;
  27. }
  28. // ==================================================
  29. public void SwitchAlphaVideo(bool value)
  30. {
  31. if (value)
  32. {
  33. videoPlayer.Play();
  34. }
  35. else
  36. {
  37. videoPlayer.Pause();
  38. }
  39. return;
  40. }
  41. // ==================================================
  42. private void ShortcutKey()
  43. {
  44. if (Input.GetKeyDown(KeyCode.Q))
  45. {
  46. SwitchAlphaVideo(true);
  47. }
  48. if (Input.GetKeyDown(KeyCode.W))
  49. {
  50. SwitchAlphaVideo(false);
  51. }
  52. return;
  53. }
  54. }