- Sort Posts
- 8 replies
- Last post
Assigning different user defined targets to different image targ
Also, how are you calling mTargetBuildingBehaviour.BuildNewTarget() ?
This reference in the example BuildNewTarget method pulls a NullReferenceException for multile ImageTargets set to UDT
mCurrentTargetName = string.Format("{0}-{1}", ImageTargetTemplate.TrackableName, mTargetCounter);
Assigning different user defined targets to different image targ
Hi David, this error shows up for multiple user defined targets:
Assigning different user defined targets to different image targ
Perhaps I'm confused as to what you're trying to accomplish, but the method I've described is very easy to implement.
You can add another ImageTarget prefab instance to your scene, define it as a User Defined type, and name the instance so that you can obtain it using GameObject.Find(). Add a model to confirm that you're getting the right target.
Let's say you have an ImageTarget named AlternateUDT in the scene.
private ImageTargetBehaviour altImageTarget;
...
public void OnNewTrackableSource(TrackableSource trackableSource) { // deactivates the dataset first mImageTracker.DeactivateDataSet(mBuiltDataSet); // Destroy the oldest target if the dataset is full or the dataset // already contains five user-defined targets. if (mBuiltDataSet.HasReachedTrackableLimit() || mBuiltDataSet.GetTrackables().Count() >= 5) { IEnumerable<Trackable> trackables = mBuiltDataSet.GetTrackables(); Trackable oldest = null; foreach (Trackable trackable in trackables) if (oldest == null || trackable.ID < oldest.ID) oldest = trackable; if (oldest != null) { Debug.Log("Destroying oldest trackable in UDT dataset: " + oldest.Name); mBuiltDataSet.Destroy(oldest, true); } } if( mBuiltDataSet.GetTrackables().Count() == 0 ){ // get predefined trackable and instantiate it ImageTargetBehaviour imageTargetCopy = (ImageTargetBehaviour)Instantiate(ImageTargetTemplate); // add the duplicated trackable to the data set and activate it mBuiltDataSet.CreateTrackable(trackableSource, imageTargetCopy.gameObject); }else{ altImageTarget = GameObject.Find("AlternateUDT").GetComponent<ImageTargetBehaviour>(); mBuiltDataSet.CreateTrackable( trackableSource , altImageTarget.gameObject ); } mTargetCounter++; // activate the dataset again mImageTracker.ActivateDataSet(mBuiltDataSet); }
So in the code above, the first UDT instance will be assigned the template and the second will use the AlternateUDT ImageTarget.
Is that what you're trying to accomplish?
Assigning different user defined targets to different image targ
Have you tested this to work? If so, how do you get the other user defined targets to not also collide with the same imagetarget? It seems all the user defined targets show up on the same image target?
Can you include the unity scene file or export package
Take look at the UserDefinedTargetEventHandler.cs file that's included w/ the UDT sample, specifically the OnNewTrackableSource method. This shows you how to access the active trackables and how they are identified. Each is a distinct trackable w/ a corresponding ImageTargetBehaviour. If you want to associate a given UDT trackable with a distinct ImageTarget instance, you can implement that here by instantiating that ImageTarget rather than the template.
Assigning different user defined targets to different image targ
Take look at the UserDefinedTargetEventHandler.cs file that's included w/ the UDT sample, specifically the OnNewTrackableSource method. This shows you how to access the active trackables and how they are identified. Each is a distinct trackable w/ a corresponding ImageTargetBehaviour. If you want to associate a given UDT trackable with a distinct ImageTarget instance, you can implement that here by instantiating that ImageTarget rather than the template.
public void OnNewTrackableSource(TrackableSource trackableSource) { // deactivates the dataset first mImageTracker.DeactivateDataSet(mBuiltDataSet); // Destroy the oldest target if the dataset is full or the dataset // already contains five user-defined targets. if (mBuiltDataSet.HasReachedTrackableLimit() || mBuiltDataSet.GetTrackables().Count() >= 5) { IEnumerable<Trackable> trackables = mBuiltDataSet.GetTrackables(); Trackable oldest = null; foreach (Trackable trackable in trackables){ if (oldest == null || trackable.ID < oldest.ID) oldest = trackable; } if (oldest != null) { Debug.Log("Destroying oldest trackable in UDT dataset: " + oldest.Name); mBuiltDataSet.Destroy(oldest, true); } } // get predefined trackable and instantiate it ImageTargetBehaviour imageTargetCopy = (ImageTargetBehaviour)Instantiate(ImageTargetTemplate); // add the duplicated trackable to the data set and activate it mBuiltDataSet.CreateTrackable(trackableSource, imageTargetCopy.gameObject); mTargetCounter++; // activate the dataset again mImageTracker.ActivateDataSet(mBuiltDataSet); }
Also, how do you keep track of which trackable to delete?