Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class that takes the content from a Vuforia Image target and puts it inside of a ground plane stage
using UnityEngine;
using Vuforia;
public class AnchorTargetListener : MonoBehaviour, ITrackableEventHandler
{
//The Goal of this class is to automatically Initiate a Ground Plane when the Image Target is detected
// Use this classs in combination with the PlaceContentFromImageTarget if you want to move the content
// of an image target onto the Ground Plane Stage
//This class should replace the default AnchorInputListenerBehaviour in the PlaneFinder GameObject
//Don't forget to set the OnInputReceivedEvent of this component to call the PlaneFinderBehaviour
public ImageTargetBehaviour ImageTarget;
private bool _planeFound = false;
public AnchorInputListenerBehaviour.InputReceivedEvent OnInputReceivedEvent;
void Start ()
{
if (this.ImageTarget != null)
{
this.ImageTarget.RegisterTrackableEventHandler(this);
}
}
public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
{
if (_planeFound)
{
return;
}
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED )
{
Debug.Log("Target Found!");
if (OnInputReceivedEvent != null)
{
//TODO: is Camera.main still slow?
var hitTestLocation = Camera.main.WorldToScreenPoint(this.ImageTarget.transform.position);
Debug.Log("hit test location: " + hitTestLocation);
this.OnInputReceivedEvent.Invoke(hitTestLocation);
_planeFound = true;
}
}
}
}
using System;
using UnityEngine;
using Vuforia;
public class PlaceContentFromImageTarget : MonoBehaviour
{
//This class should replace the default ContentPositioningBehaviour in the PlaneFinder GameObject
//Don't forget to set the OnInteractiveHitTest of the PlaneFinderBehaviour to call this class
//IMPORTANT:
// Be sure to enable Device Tracker and set mode to POSITIONAL in the Vuforia Configuration
public AnchorStageBehaviour AnchorStage;
public ImageTargetBehaviour ImageTarget;
private PositionalDeviceTracker _deviceTracker;
private bool _anchorSet;
public void Start ()
{
var x = new ContentPositioningBehaviour();
var y = x.AnchorStage;
if (AnchorStage == null)
{
Debug.LogWarning("AnchorStage must be specified");
return;
}
if (ImageTarget == null)
{
Debug.LogWarning("Image Target must be specified");
return;
}
AnchorStage.gameObject.SetActive(false);
}
public void Awake()
{
VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
}
public void OnDestroy()
{
VuforiaARController.Instance.UnregisterVuforiaStartedCallback(OnVuforiaStarted);
}
private void OnVuforiaStarted()
{
_deviceTracker = TrackerManager.Instance.GetTracker<PositionalDeviceTracker>();
}
public void OnInteractiveHitTest(HitTestResult result)
{
if (_anchorSet)
{
//Leave if the anchor has already been set
return;
}
if (result == null || AnchorStage == null)
{
Debug.LogWarning("Hit test is invalid or AnchorStage not set");
return;
}
//Let's go ahead and create the anchor at the position of the hit test
var anchor = _deviceTracker.CreatePlaneAnchor(Guid.NewGuid().ToString(), result);
if (anchor != null)
{
AnchorStage.transform.SetParent(anchor.transform);
AnchorStage.transform.localPosition = Vector3.zero;
AnchorStage.transform.localRotation = Quaternion.identity;
AnchorStage.gameObject.SetActive(true);
//Make sure that the Image Target is currently tracking
if (ImageTarget.CurrentStatus == TrackableBehaviour.Status.DETECTED ||
ImageTarget.CurrentStatus == TrackableBehaviour.Status.TRACKED ||
ImageTarget.CurrentStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
//Note: If you are handling objects differently, there is an opportunity to optimize this section
var defaultEventHandler = this.ImageTarget.GetComponent<DefaultTrackableEventHandler>();
if (defaultEventHandler != null)
{
defaultEventHandler.OnTrackableStateChanged(TrackableBehaviour.Status.UNKNOWN,
TrackableBehaviour.Status.TRACKED);
}
//Let's move the contents of the image target onto the ground plane
for (var i = 0; i < ImageTarget.transform.childCount; i++)
{
var content = ImageTarget.transform.GetChild(i);
content.SetParent(AnchorStage.transform);
}
}
_anchorSet = true;
}
}
}
@AsalazarG1

This comment has been minimized.

Copy link

@AsalazarG1 AsalazarG1 commented Jul 18, 2018

I have a question, why in this part of code:

var anchor = _deviceTracker.CreatePlaneAnchor(Guid.NewGuid().ToString(), result);

    if (anchor != null)
    {
        AnchorStage.transform.SetParent(anchor.transform); 
        AnchorStage.transform.localPosition = Vector3.zero;
        AnchorStage.transform.localRotation = Quaternion.identity;
        AnchorStage.gameObject.SetActive(true);

where say AnchorStage.transform.SetParent(anchor.transform); in anchor.transform have an error

@gsylvain2

This comment has been minimized.

Copy link

@gsylvain2 gsylvain2 commented Jul 20, 2018

The class Anchor has changed and doesn't have a 'transform' property.
The solution is to attach a component AnchorBehaviour on the gameObject you want anchored. Then, you do something like: anchoredObject.GetComponent().ConfigureAnchor(anchor);

@yosun

This comment has been minimized.

Copy link

@yosun yosun commented Sep 7, 2019

hey which version of Vuforia/Unity did you have this working with?

@vad710

This comment has been minimized.

Copy link
Owner Author

@vad710 vad710 commented Sep 11, 2019

This was probably using Vuforia 7 and Unity 2018.1 or 2018.2

@yosun

This comment has been minimized.

Copy link

@yosun yosun commented Sep 12, 2019

Okay so pre ARFoundation

@vad710

This comment has been minimized.

Copy link
Owner Author

@vad710 vad710 commented Sep 12, 2019

@yosun - yes, does Vuforia Engine now support ARFoundation?

@SirApplejuicer

This comment has been minimized.

Copy link

@SirApplejuicer SirApplejuicer commented Sep 20, 2019

is this still functioning in vuforia 8.1.10 than?

@ROOHITH

This comment has been minimized.

Copy link

@ROOHITH ROOHITH commented Apr 5, 2021

From where did you learn vuforia codes

@ROOHITH

This comment has been minimized.

Copy link

@ROOHITH ROOHITH commented Apr 5, 2021

It's seem Little complicated for to study vuforia documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment