You need to show button when traceable "ImageTarget" found. i.e. button should be popup on traceable event.
- If you create such simple UI in unity , wherever you place it in
hierarchy . it will always display regardless of marker detection.
- You first need to know marker get detected or not using method
provided by Vuforia and then have to create button grammatically.
For this you must understand how to keep a track of trackable events,
such as trackable found or trackable lost. Vuforia makes this easy
for us by providing a template script named
DefaultTrackableEventHandler.
This script is by default attached to any ImageTarget prefab. You can use this script or similar script as shown below.
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
You have to write C# script and attach it to "ImageTarget"
Script below describes how to show (popup) a GUI button overlaid on top of the 3D view in response to a target detection event, using thre Unity extension of the Vuforia SDK.
Here is the Script
using UnityEngine;
using System.Collections;
using Vuforia;
using System;
public class ButtonPopup : MonoBehaviour, ITrackableEventHandler {
float native_width= 1920f;
float native_height= 1080f;
public Texture btntexture;
public Texture LogoTexture;
public Texture MobiliyaTexture;
private TrackableBehaviour mTrackableBehaviour;
private bool mShowGUIButton = false;
void Start () {
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour) {
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
mShowGUIButton = true;
}
else
{
mShowGUIButton = false;
}
}
void OnGUI() {
//set up scaling
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));
Rect mButtonRect = new Rect(1920-215,5,210,110);
GUIStyle myTextStyle = new GUIStyle(GUI.skin.textField);
myTextStyle.fontSize = 50;
myTextStyle.richText=true;
GUI.DrawTexture(new Rect(5,1080- 115,350,110),LogoTexture);
GUI.DrawTexture (new Rect (1530, 970, 350, 110), MobiliyaTexture);
if (!btntexture) // This is the button that triggers AR and UI camera On/Off
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (mShowGUIButton) {
GUI.Label(new Rect(40, 25, 350, 70), "<b> G E 9 1 0 0 C </b>",myTextStyle);
//GUI.Box (new Rect (0,0,100,50), "Top-left");
//GUI.Box (new Rect (1920 - 100,0,100,50), "Top-right");
//GUI.Box (new Rect (0,1080- 50,100,50), "Bottom-left");
//GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Bottom right");
// draw the GUI button
if (GUI.Button(mButtonRect, btntexture)) {
// do something on button click
OpenVideoActivity();
}
}
}
public void OpenVideoActivity()
{
var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
// Accessing the class to call a static method on it
var jc = new AndroidJavaClass("com.mobiliya.gepoc.StartVideoActivity");
// Calling a Call method to which the current activity is passed
jc.CallStatic("Call", jo);
}
}