Skip to content

Instantly share code, notes, and snippets.

@kamend
Last active December 16, 2022 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamend/cea52cb940e52eb0f33337b66eb43c44 to your computer and use it in GitHub Desktop.
Save kamend/cea52cb940e52eb0f33337b66eb43c44 to your computer and use it in GitHub Desktop.
XR Interaction Toolkit: Custom ARPlacementInteractable to place object with the left mouse button when in the Editor
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit.AR;
public class CustomPlacementInteractable : ARPlacementInteractable
{
ARObjectPlacementEventArgs m_ObjectPlacementEventArgs = new ARObjectPlacementEventArgs();
void Update()
{
if (Input.GetMouseButtonDown(0))
{
MousePressed(Input.mousePosition);
}
}
TapGesture GenerateTapGestureFromPosition(Vector2 p)
{
Touch t = new Touch();
t.position = p;
TapGesture tp = new TapGesture(new TapGestureRecognizer(), t);
return tp;
}
void MousePressed(Vector2 p)
{
var tp = GenerateTapGestureFromPosition(p);
if (TryGetPlacementPose(tp, out var pose))
{
var placementObject = PlaceObject(pose);
m_ObjectPlacementEventArgs.placementInteractable = this;
m_ObjectPlacementEventArgs.placementObject = placementObject;
OnObjectPlaced(m_ObjectPlacementEventArgs);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment