Log in or register to post comments

Resetting Userdefined Target on Press

January 15, 2015 - 9:01am #1

Hi There,

I've been running through the SimpleUDTHandler.cs and can't seem to get around this one issue.

I need to be able to reset the capture of a user texture on button press. I can launch the first image, but on continuous clicks the same model stays constantly on screen and I can't recallobrate the user defined texture again. 

This is the code I'm working with using the SimpleUDTHandler.cs base script and also this other thread https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/please-explain-udt

I'm new to coding, but learning. ^_^

Any help would be fantastic.

Steve

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class SimpleUDTHandler : MonoBehaviour, IUserDefinedTargetEventHandler {
	
	private UserDefinedTargetBuildingBehaviour mTargetBuildingBehaviour;
	private ImageTracker mImageTracker;
	private DataSet mBuiltDataSet;
	
	private int mTargetCounter = 5;
	//private bool mUdtInitialized = false;
	private ImageTargetBuilder.FrameQuality mFrameQuality = ImageTargetBuilder.FrameQuality.FRAME_QUALITY_NONE;
	
	public ImageTargetBehaviour ImageTargetTemplate;
	
	public GUIStyle MyAppStyle;
	public Camera MyAppButtonCamera;
	public float ButtonSize = 1.0f;
	public float ButtonHeightMultiplier = 1.0F;
	
	void Start() {
		mTargetBuildingBehaviour = GetComponent<UserDefinedTargetBuildingBehaviour>();
		if (mTargetBuildingBehaviour) {
			mTargetBuildingBehaviour.RegisterEventHandler(this);
		}
		
		ButtonSize = Screen.width/ButtonSize;
		
	}

	public void OnInitialized()
	{
		mImageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
		if (mImageTracker != null)
		{
			// create a new dataset
			mBuiltDataSet = mImageTracker.CreateDataSet();
			mImageTracker.ActivateDataSet(mBuiltDataSet);
		}
	}

	public void OnFrameQualityChanged(ImageTargetBuilder.FrameQuality frameQuality) {
		mFrameQuality = frameQuality;
	}
	
	public void OnNewTrackableSource(TrackableSource trackableSource)
	{
		// deactivates the dataset first
		mImageTracker.DeactivateDataSet(mBuiltDataSet);
		
		// 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);
	}
	
	
	void OnGUI() {
		//if (!mUdtInitialized) return;
		
		// If Frame Quality is medium / high => show Button to build target
		if (mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_HIGH ||
		    mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_MEDIUM) {
			
			//When acceptable quality is reached turn on Camera with NGUI Button.
			MyAppButtonCamera.enabled = true;
			
			if (GUI.Button(new Rect(((Screen.width/2)-(ButtonSize/2)), ((Screen.height) - (ButtonSize*ButtonHeightMultiplier)), ButtonSize, ButtonSize), "", MyAppStyle)) {
				BuildNewTarget();
			}
		}else{
			//Turn off Camera when quality is too low.
			MyAppButtonCamera.enabled = false;
		}
	}
	
	private void BuildNewTarget() {
		
		//Turn off NGUI Camera once the button is pressed.
		if(MyAppButtonCamera.enabled == true)MyAppButtonCamera.enabled = false;
		
		string newTargetName = ImageTargetTemplate.TrackableName + "-" + mTargetCounter;
		mTargetBuildingBehaviour.BuildNewTarget(newTargetName, ImageTargetTemplate.GetSize().x);
	}
	
}

 

Resetting Userdefined Target on Press

January 15, 2015 - 11:50pm #3

The only action that I need to fix is that the AR stays on when the target has been lost. Which I'm trying to work out.

Resetting Userdefined Target on Press

January 15, 2015 - 2:07pm #2

Hi All,

 

I managed to fix the issue after a lot of head scratching. Hope this helps anyone else. 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class SimpleUDTHandler : MonoBehaviour, IUserDefinedTargetEventHandler {
	
	private UserDefinedTargetBuildingBehaviour mTargetBuildingBehaviour;
	private ImageTracker mImageTracker;
	private DataSet mBuiltDataSet;
	
	private int mTargetCounter = 0;
	//private bool mUdtInitialized = false;
	private ImageTargetBuilder.FrameQuality mFrameQuality = ImageTargetBuilder.FrameQuality.FRAME_QUALITY_NONE;
	
	public ImageTargetBehaviour ImageTargetTemplate;

	public GUIStyle MyAppStyle;
	public Camera MyAppButtonCamera;
	public float ButtonSize = 1.0f;
	public float ButtonHeightMultiplier = 1.0F;

	void Start()
	{
		mTargetBuildingBehaviour = GetComponent<UserDefinedTargetBuildingBehaviour>();
		if (mTargetBuildingBehaviour)
		{
			mTargetBuildingBehaviour.RegisterEventHandler(this);
		}
		
		ButtonSize = Screen.width/ButtonSize;
		
	}
	
	public void OnInitialized()
	{
		mImageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
		if (mImageTracker != null)
		{
			// create a new dataset
			mBuiltDataSet = mImageTracker.CreateDataSet();
			mImageTracker.ActivateDataSet(mBuiltDataSet);
		}
	}
	
	
	public void OnFrameQualityChanged(ImageTargetBuilder.FrameQuality frameQuality)
	{
		mFrameQuality = frameQuality;
	}
	
	public void OnNewTrackableSource(TrackableSource trackableSource)
	{
		// deactivates the dataset first
		mImageTracker.DeactivateDataSet(mBuiltDataSet);
		
		// 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);
	}
	
	void OnGUI() {
		//if (!mUdtInitialized) return;
		
		// If Frame Quality is medium / high => show Button to build target
		if (mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_HIGH ||
		    mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_MEDIUM) {
			
			//When acceptable quality is reached turn on Camera with NGUI Button.
			MyAppButtonCamera.enabled = true;
			
			if (GUI.Button(new Rect(((Screen.width/2)-(ButtonSize/2)), ((Screen.height) - (ButtonSize*ButtonHeightMultiplier)), ButtonSize, ButtonSize), "", MyAppStyle)) {
				BuildNewTarget();
			}
		}else{
			//Turn off Camera when quality is too low.
			MyAppButtonCamera.enabled = false;
		}
	}
	
	private void BuildNewTarget() {
		
		//Turn off NGUI Camera once the button is pressed.
		if(MyAppButtonCamera.enabled == true)MyAppButtonCamera.enabled = false;
		
		string newTargetName = ImageTargetTemplate.TrackableName + "-" + mTargetCounter;
		mTargetBuildingBehaviour.BuildNewTarget(newTargetName, ImageTargetTemplate.GetSize().x);
	}
	
}

 

Log in or register to post comments