问题
Good day everyone, I am currently on the process of creating my first AR application using unity and vuforia sdk. I already know some basics of AR that you need an image target to display the 3d object or information associated with the image target. I have this AR spinning cube
this sample app works when it found a valid image target and a 3d cube will display and start to spin. My question is while the phone scans for image target there should be a message saying "Scanning for image target" and when found it displays "Image target found" and it will display a popup button "Show" that when pressed the cube will appear.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateScript : MonoBehaviour {
public int speed;
//Update is called once per frame
void Update () {
transform.Rotate (new Vector3 (0, Time.deltaTime * speed, 0));
}
}
Updates:
I found this script and modified it a bit to display a UI text above the screen, I intend to change the text into "Image Target Lost" when no image target detected and "Image Target Found" when the image target is present. I put the script on image target prefab but it displays an error UI Text Error. Any suggestions on where I did wrong. Thanks
Sample Code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Vuforia;
public class TextScript : MonoBehaviour, ITrackableEventHandler {
private TrackableBehaviour mTrackableBehaviour;
//Declares a UI text
Text uiText;
void Start () {
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
//uiText = gameObject.GetComponent<Text> ();
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED)
{
uiText.text = "Image Target Found";
}
else
{
uiText.text = "Image Target Lost";
}
}
}
Update (10/30/2017)
I'm sorry if I update late, I've tried all the solution you are all giving but it still don't work. The name of the code is TextScript and I've attached it to image target but still the result is unsuccessful. Here is the sample code.
TextScript
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Vuforia;
public class TextScript : MonoBehaviour, ITrackableEventHandler {
private TrackableBehaviour mTrackableBehaviour;
//Declares a UI text
public Text uiText;
void Start () {
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
uiText = gameObject.GetComponent<Text> ();
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
uiText.text = "Image Target Found";
}
else
{
OnTrackingLost();
//uiText.text = "Image Target Lost";
}
}
private void OnTrackingFound()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
uiText.text = "Image Target Found";
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
uiText.text = "Image Target Found";
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
}
private void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
uiText.text = "Image Target Lost";
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
uiText.text = "Image Target Lost";
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
}
P.S
I already name the text UI as Scanning so when I press play the text already display the said word. But when it found the image target the text doesn't change. Please guys I badly needed you help, if there are unwanted code please feel free to edit it or comment so that I can delete it. Thanks
回答1:
Maybe you should look into this method OnTrackingFound() in vuforia/scripts/defaulttrackable.cs
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
you could use a boolean to manage when to render the cube.
回答2:
See the Properties of TrackableBehaviour in given link: https://library.vuforia.com/content/vuforia-library/en/reference/unity/classVuforia_1_1TrackableBehaviour.html
When Image target is Detected Set the TEXT to "Scanning", On OnTrackingFound() Method Set the TEXT to "Found" and Set Button Object visible true, Onbuttonclik event Instantiate Cube object
On OntrackingLost() Method Set Button object visible false
Hope this helps.
回答3:
In your script you just declared the uiText but not initialised it. It should be like this
public Text uiText;
and then drag your relevant Text gameobject from project window to the TextScript script in the inspector or initialise it in onStart() like
uiText = GameObject.Find("<your_text_object_name>").GetComponent<Text>();
The second option is not recommended as the Text initialises number of times equal to the image targets count in the database.
Hope it helps.
来源:https://stackoverflow.com/questions/46679619/augmented-reality-application-using-unity-and-vuforia