Log in or register to post comments

Enableing AR Trackables with Button Press

May 14, 2014 - 8:01am #1

I realize that this question isn’t entirely related to Vuforia.  I’m just trying to gather as much helpful info as I can as a new Unity developer. 

My Vuforia application recognizes my Tracking Image and successfully displays the eight different Trackers that I have created in Unity when the image is in the camera.  (…do I have the nomenclature right there?)

What I would like to do now is to create a button on the screen that the user can press.  Each press of the button will enable one more of the “Trackables”. 

So far I’ve managed to find code that creates buttons that I can use to load different scenes in Unity.  My idea is to adapt this code to increase the value of a variable when the button is pressed.  I’ll then use that variable in the DefaultTrackableEventHandler.cs 

My first thought is to use if statements; If the button press variable is greater than the threshold for that Trackable, then Enable.  After enough button presses, all of my Trackables will be visible. 

I’m still working through examples to figure out what I need to do exactly.  But any feedback on the approach, or where I can find more related info/examples would be a big help.  

Enableing AR Trackables with Button Press

May 21, 2014 - 6:54am #3

The scripts more or less work now.  But I occassionally get a "flash" of the disabled objects when the counter is below the threshold to have them appear.  I'm sure this is a Unity thing and not a Vuforia thing, but someone may have seen it before.

Next and Reset Buttons Script

using UnityEngine;
using System.Collections;

public class NextButton : MonoBehaviour 
{
public int nextcounter=0; //Init counter at zero

	void OnGUI () 
	{
		// Make a background box
		GUI.Box(new Rect(10,10,150,90), "Press Next");
		
		// Make the first button. If it is pressed, the variable will be increased
		if(GUI.Button(new Rect(20,40,130,20), "Next")) {
			// This code executed when Button is clicked
			nextcounter ++;
			Debug.Log ("counter = " + nextcounter);
		}
		// Make the second button. If it is pressed, the variable will be reset
		if(GUI.Button(new Rect(20,70,130,20), "Reset")) {
			// This code executed when Button is clicked
			nextcounter=0;
			Debug.Log ("counter = " + nextcounter);
	}
}
}

 

Game Objects Script

using UnityEngine;
using System.Collections;

public class Activate01 : MonoBehaviour {
	public GameObject player;
	private NextButton playerScript;

	void Start () {
				playerScript = player.GetComponent<NextButton> ();
		}
	// Update is called once per frame
	void Update () {
		if(playerScript.nextcounter >= 1) {
			Debug.Log ("01 On");
			renderer.enabled = true;
	}
		if(playerScript.nextcounter < 1) {
			Debug.Log ("01 Off");
			renderer.enabled = false;
}
}
}

I've basically got 9 copies of the game object script.  Each copy "triggers" on a different count. 

This works fairly well.  But there are sometimes 'flashes' of the disabled objects. 

Enableing AR Trackables with Button Press

May 16, 2014 - 12:37pm #2

Hopefully it isn't bad form to reply to my own post here.

What I'm trying to do is to dynamically activate the 3D objects I have attached to my Image Tracker.  I don't know if these 3D objects have a special name when they've been associated with a tracker.

My application has 9 3D objects associated with the Image Tracker.  When the Image Tracker is found, all 9 objects are shown. 

What I want to do is have the 3D objects appear one at a time with a button press. 

I have created a Next Button and  Reset Button.  The Next button increments a counter, and the Reset button resets the counter.

using UnityEngine;
using System.Collections;

public class NextButton : MonoBehaviour 
{
public static int nextcounter=0; //Init counter at zero

	void OnGUI () 
	{
		// Make a background box
		GUI.Box(new Rect(10,10,150,90), "Press Next");
		
		// Make the first button. If it is pressed, the variable will be increased
		if(GUI.Button(new Rect(20,40,130,20), "Next")) {
			// This code executed when Button is clicked
			nextcounter ++;
			Debug.Log ("counter = " + nextcounter);
		}
		// Make the second button. If it is pressed, the variable will be reset
		if(GUI.Button(new Rect(20,70,130,20), "Reset")) {
			// This code executed when Button is clicked
			nextcounter=0;
			Debug.Log ("counter = " + nextcounter);
	}
}
}

This code appears to be working. 

I have also created a script for one of my 3D objects attached to my Image Tracker.

using UnityEngine;
using System.Collections;

public class wireActivate09 : MonoBehaviour {
	//renderer.enabled = false; Error Line  
	public static int nextcounter;
	// Update is called once per frame
	void Update () {
		if(nextcounter > 1) {
			Debug.Log ("On");
	}
		if(nextcounter < 1) {
			Debug.Log ("Off");
}
}
}

I'm having some troubles with this code.  nextcounter is always logging as less than 1, even when my other debugger is showing it's not.  It doesn't seem to be reading the same variable. 

Also, I can't seem to get the syntax right for enabling the renderer or activating the game object.  I've tried several different ways, but there's always a compiler error like an invalid token (as above) or something. 

Any suggestions?

 

Log in or register to post comments