126 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using Unity.XR.CoreUtils;
 | |
| using UnityEngine;
 | |
| using UnityEngine.Rendering;
 | |
| using UnityEngine.UI;
 | |
| using UnityEngine.XR.Interaction.Toolkit;
 | |
| using FMOD.Studio;
 | |
| using FMODUnity;
 | |
| 
 | |
| public class MenuTeleportButton : MonoBehaviour
 | |
| {
 | |
|     public Sprite NormalSprite;
 | |
|     public Sprite HoverSprite;
 | |
|     public String TargetName;
 | |
|     public XRBaseControllerInteractor LeftXRInteractor; // Reference to XR controller interactor (e.g., Ray Interactor)
 | |
|     public XRBaseControllerInteractor RightXRInteractor;
 | |
|     public XROrigin Player;
 | |
|     public TeleportationProvider teleportationProvider; // Reference to TeleportationProvider
 | |
| 
 | |
|     private Button button;
 | |
|     private TeleportLocation target; // Target teleport position
 | |
| 
 | |
|     private EventInstance TeleportingSound;
 | |
|     FMOD.Studio.Bus SpecialBus; //FMOD bus variable
 | |
| 
 | |
|     private void Awake()
 | |
|     {
 | |
|         TeleportingSound = AudioManager.Instance.CreateInstance(FMODEvents.Instance.Teleport); //initialise the instance
 | |
|         SpecialBus = FMODUnity.RuntimeManager.GetBus("bus:/LogicalMute");
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
|     void Start()
 | |
|     {
 | |
|         button = GetComponent<Button>();
 | |
| 
 | |
|         // Subscribe to button events
 | |
|         button.onClick.AddListener(TeleportPlayer);
 | |
| 
 | |
|         TeleportLocation[] locations = FindObjectsOfType<TeleportLocation>(); // Fetch all teleport locations in the scene.
 | |
|         foreach (TeleportLocation location in locations) // Find the target location.
 | |
|         {
 | |
|             if (location.Name.Equals(TargetName))
 | |
|             {
 | |
|                 target = location;
 | |
|                 break;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         if (target == null)
 | |
|         {
 | |
|             Debug.LogError("Teleport target of " + TargetName + " not found.");
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public void SetStateSelected()
 | |
|     {
 | |
|         if (button != null && HoverSprite != null)
 | |
|         {
 | |
|             button.targetGraphic.GetComponent<Image>().sprite = HoverSprite;
 | |
|         }
 | |
|         
 | |
|     }
 | |
| 
 | |
|     public void SetStateDefault()
 | |
|     {
 | |
|         // Refresh the button state.
 | |
|         button.interactable = false;
 | |
|         button.interactable = true;
 | |
| 
 | |
|         if (NormalSprite != null)
 | |
|         {
 | |
|             button.targetGraphic.GetComponent<Image>().sprite = NormalSprite;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void TeleportPlayer()
 | |
|     {
 | |
|         if (target == null || Player == null || teleportationProvider == null)
 | |
|         {
 | |
|             Debug.LogWarning("Teleportation failed: Target, Player, or TeleportationProvider is missing.");
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         // Prepare teleport request
 | |
|         TeleportRequest request = new TeleportRequest
 | |
|         {
 | |
|             destinationPosition = target.transform.position,
 | |
|             destinationRotation = target.transform.rotation,
 | |
|             matchOrientation = MatchOrientation.TargetUpAndForward
 | |
|         };
 | |
| 
 | |
|         // Queue teleport request
 | |
|         bool success = teleportationProvider.QueueTeleportRequest(request);
 | |
| 
 | |
|         if (!success)
 | |
|         {
 | |
|             Debug.LogWarning("Teleport request failed to queue.");
 | |
|         }
 | |
| 
 | |
|         // Refresh the button state
 | |
|         button.interactable = false;
 | |
|         button.interactable = true;
 | |
| 
 | |
|         StartCoroutine(MuteBusForSeconds(1.5f));
 | |
| 
 | |
|         TeleportingSound.start(); //playing 2d oneshot
 | |
|     }
 | |
| 
 | |
|     private IEnumerator MuteBusForSeconds(float duration)
 | |
|     {
 | |
|         // Lower volume to 0 instantly
 | |
|         SpecialBus.setVolume(0f);
 | |
|         Debug.Log("[MenuTeleportButton] Muting LogicalMute bus...");
 | |
| 
 | |
|         yield return new WaitForSeconds(duration);
 | |
| 
 | |
|         // Restore volume
 | |
|         SpecialBus.setVolume(1f);
 | |
|         Debug.Log("[MenuTeleportButton] Unmuted LogicalMute bus.");
 | |
|     }
 | |
| }
 |