95 lines
2.7 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;
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
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;
}
}