using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; public class VideoPlayerPlane : MonoBehaviour { [SerializeField]private Transform m_TransCamera = null; [SerializeField] float m_ActiveDistance = 10.0f; private VideoPlayer m_VideoPlayer = null; private MeshRenderer m_MeshRenderer = null; private float m_Distance = 0; private Transform m_Trans = null; private void Awake() { m_VideoPlayer = this.GetComponent(); m_MeshRenderer = this.GetComponent(); m_Trans = transform; if (m_MeshRenderer != null) m_MeshRenderer.enabled = false; if (m_VideoPlayer != null) m_VideoPlayer.Stop(); } private void OnEnable() { StartCoroutine(EnumCheckDistance()); } private void OnDisable() { StopAllCoroutines(); } IEnumerator EnumCheckDistance() { while (true) { yield return new WaitForSeconds(1.0f); m_Distance = Vector3.Distance(m_Trans.position, m_TransCamera.position); Debug.LogFormat("\"{0}\" 距离:{1}", name, m_Distance); if (m_Distance <= m_ActiveDistance) { ActiveVideoPlayer(); } else { DeactiveVideoPlayer(); } } } void ActiveVideoPlayer() { if (m_MeshRenderer != null) m_MeshRenderer.enabled = true; if (m_VideoPlayer != null) m_VideoPlayer.Play(); } void DeactiveVideoPlayer() { if (m_MeshRenderer != null) m_MeshRenderer.enabled = false; if (m_VideoPlayer != null) m_VideoPlayer.Stop(); } }