55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using System.Runtime.InteropServices;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.Interaction.Toolkit;
|
|
using UnityEngine.XR.Interaction.Toolkit.Interactors;
|
|
using VRShopping.Items;
|
|
using VRShopping.UI;
|
|
|
|
namespace VRShopping.Interact
|
|
{
|
|
public class ItemInfoOnGrab : MonoBehaviour
|
|
{
|
|
private ItemData _data;
|
|
|
|
private Handedness _lastHand;
|
|
|
|
private void Awake()
|
|
{
|
|
ItemInstance itemIns = GetComponent<ItemInstance>();
|
|
if(itemIns != null)
|
|
{
|
|
_data = itemIns.ItemDataInfo;
|
|
}
|
|
}
|
|
|
|
public void HandleSelectEntered(SelectEnterEventArgs args)
|
|
{
|
|
if (!TryGetHandedness(args.interactorObject, out var hand)) return;
|
|
if (ItemInfoHud.Instance == null) return;
|
|
|
|
_lastHand = hand;
|
|
ItemInfoHud.Instance.Show(hand, _data);
|
|
}
|
|
|
|
public void HandleSelectExited(SelectExitEventArgs args)
|
|
{
|
|
if (ItemInfoHud.Instance == null) return;
|
|
ItemInfoHud.Instance.Hide(_lastHand);
|
|
}
|
|
|
|
private static bool TryGetHandedness(IXRInteractor interactor, out Handedness hand)
|
|
{
|
|
hand = Handedness.Left;
|
|
if (interactor is XRBaseInputInteractor input)
|
|
{
|
|
switch (input.handedness)
|
|
{
|
|
case InteractorHandedness.Left: hand = Handedness.Left; return true;
|
|
case InteractorHandedness.Right: hand = Handedness.Right; return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|