EffectScreen.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class EffectScreen : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private UnityEngine.Video.VideoPlayer mPlayer;
  9. private MeshRenderer mRender;
  10. private Color DefaultColor;
  11. private Color HideColor = new Color(0,0,0,1);
  12. void Awake()
  13. {
  14. mRender = this.gameObject.GetComponent<MeshRenderer>();
  15. DefaultColor = mRender.material.GetColor("_TintColor");
  16. this.mRender.material.SetColor("_TintColor", HideColor);
  17. }
  18. private void OnEnable()
  19. {
  20. }
  21. private UnityEngine.Video.VideoClip clip;
  22. private float duration = 0.3f;
  23. private float startTime;
  24. public void ChangeMoive(UnityEngine.Video.VideoClip clip)
  25. {
  26. this.mRender.enabled = true;
  27. startTime = Time.realtimeSinceStartup;
  28. this.clip = clip;//先拿到要换的片
  29. //渐进式的影藏掉原来的片
  30. StartCoroutine(CheckHide());
  31. }
  32. IEnumerator CheckHide()
  33. {
  34. yield return null;
  35. if (!mPlayer.isPlaying)
  36. {
  37. ClickFinish();
  38. }
  39. else
  40. {
  41. float per = 0;
  42. while (mRender.material.GetColor("_TintColor") != HideColor)
  43. {
  44. per += Time.deltaTime / duration;
  45. this.mRender.material.SetColor("_TintColor", Color.Lerp(DefaultColor, HideColor, per));
  46. //CDebug.Log("WaitForEndOfFrame " + (Time.realtimeSinceStartup - startTime));
  47. yield return new WaitForEndOfFrame();
  48. }
  49. //LeanTween.color(this.gameObject, DefaultColor, duration).setEase(LeanTweenType.animationCurve);
  50. ClickFinish();
  51. }
  52. }
  53. private void ClickFinish()
  54. {
  55. //CDebug.Log("ClickFinish " + (Time.realtimeSinceStartup - startTime));
  56. mPlayer.Stop();
  57. if (this.clip == null)
  58. {
  59. return;
  60. }
  61. mPlayer.clip = this.clip;
  62. mPlayer.Play();
  63. StartCoroutine(CheckMovie());
  64. }
  65. IEnumerator CheckMovie()
  66. {
  67. yield return null;
  68. while(!mPlayer.isPlaying)
  69. {
  70. yield return new WaitForEndOfFrame();
  71. }
  72. float per = 0;
  73. while (mRender.material.GetColor("_TintColor") != DefaultColor)
  74. {
  75. per += Time.deltaTime / duration;
  76. this.mRender.material.SetColor("_TintColor", Color.Lerp(HideColor, DefaultColor, per));
  77. yield return new WaitForEndOfFrame();
  78. }
  79. //CDebug.Log("CheckMovie End " + (Time.realtimeSinceStartup - startTime));
  80. }
  81. }