-
-
Save vad710/015629ca8d56b4a016e072cd0c2dd18c to your computer and use it in GitHub Desktop.
| 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; | |
| } | |
| } | |
| } |
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);
hey which version of Vuforia/Unity did you have this working with?
This was probably using Vuforia 7 and Unity 2018.1 or 2018.2
Okay so pre ARFoundation
@yosun - yes, does Vuforia Engine now support ARFoundation?
is this still functioning in vuforia 8.1.10 than?
From where did you learn vuforia codes
It's seem Little complicated for to study vuforia documentation
@vad710 I got this ported to Vuforia 10. Is that something you want to share by any chance? I don't use github much but maybe I can upload it and you could link it?
Sure - I'd be happy to update it!
hey checking back any updates @contrejo27 @vad710
I have a question, why in this part of code:
var anchor = _deviceTracker.CreatePlaneAnchor(Guid.NewGuid().ToString(), result);
where say AnchorStage.transform.SetParent(anchor.transform); in anchor.transform have an error