VideoPlayerPlane.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. public class VideoPlayerPlane : MonoBehaviour {
  6. [SerializeField]private Transform m_TransCamera = null;
  7. [SerializeField] float m_ActiveDistance = 10.0f;
  8. private VideoPlayer m_VideoPlayer = null;
  9. private MeshRenderer m_MeshRenderer = null;
  10. private float m_Distance = 0;
  11. private Transform m_Trans = null;
  12. private void Awake() {
  13. m_VideoPlayer = this.GetComponent<VideoPlayer>();
  14. m_MeshRenderer = this.GetComponent<MeshRenderer>();
  15. m_Trans = transform;
  16. if (m_MeshRenderer != null)
  17. m_MeshRenderer.enabled = false;
  18. if (m_VideoPlayer != null)
  19. m_VideoPlayer.Stop();
  20. }
  21. private void OnEnable() {
  22. StartCoroutine(EnumCheckDistance());
  23. }
  24. private void OnDisable() {
  25. StopAllCoroutines();
  26. }
  27. IEnumerator EnumCheckDistance() {
  28. while (true) {
  29. yield return new WaitForSeconds(1.0f);
  30. m_Distance = Vector3.Distance(m_Trans.position, m_TransCamera.position);
  31. Debug.LogFormat("\"{0}\" 距离:{1}", name, m_Distance);
  32. if (m_Distance <= m_ActiveDistance) {
  33. ActiveVideoPlayer();
  34. } else {
  35. DeactiveVideoPlayer();
  36. }
  37. }
  38. }
  39. void ActiveVideoPlayer() {
  40. if (m_MeshRenderer != null)
  41. m_MeshRenderer.enabled = true;
  42. if (m_VideoPlayer != null)
  43. m_VideoPlayer.Play();
  44. }
  45. void DeactiveVideoPlayer() {
  46. if (m_MeshRenderer != null)
  47. m_MeshRenderer.enabled = false;
  48. if (m_VideoPlayer != null)
  49. m_VideoPlayer.Stop();
  50. }
  51. }