using UnityEngine; using Unity.VRTemplate; [RequireComponent(typeof(Collider))] public class ArtWorkIntroController : MonoBehaviour { [SerializeField] private AudioClip introClip; [SerializeField, Tooltip("对应的艺术品,例如 Painting7")] private GameObject artwork; private int collidersInside; private bool isFacing; private CalloutGazeController gazeController; private AudioManager audioManager; private void Awake() { if (artwork != null) { gazeController = artwork.GetComponent(); } } private void OnEnable() { if (gazeController == null) { return; } gazeController.FacingEnteredEvent.AddListener(OnFacingEntered); gazeController.FacingExitedEvent.AddListener(OnFacingExited); } private void Start() { audioManager = AudioManager.Instance; } private void OnDisable() { if (audioManager != null) { audioManager.PauseArtWorkIntro(introClip); } collidersInside = 0; isFacing = false; if (gazeController != null) { gazeController.FacingEnteredEvent.RemoveListener(OnFacingEntered); gazeController.FacingExitedEvent.RemoveListener(OnFacingExited); } } private void OnTriggerEnter(Collider other) { if (!isActiveAndEnabled || !other.CompareTag("Player")) { return; } collidersInside++; UpdatePlayback(); } private void OnTriggerExit(Collider other) { if (!isActiveAndEnabled || !other.CompareTag("Player")) { return; } collidersInside = Mathf.Max(0, collidersInside - 1); UpdatePlayback(); } private void OnFacingEntered() { isFacing = true; UpdatePlayback(); } private void OnFacingExited() { isFacing = false; UpdatePlayback(); } private void UpdatePlayback() { if (audioManager == null) { return; } if (collidersInside > 0 && isFacing) { audioManager.PlayArtWorkIntro(introClip); } else { audioManager.PauseArtWorkIntro(introClip); } } private void Reset() { GetComponent().isTrigger = true; } }