0% found this document useful (0 votes)
83 views4 pages

AR Target Description With Button PlaySound ScriptsCS

Uploaded by

firman syah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

AR Target Description With Button PlaySound ScriptsCS

Uploaded by

firman syah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

DefaultTrackableHandle.

cs
/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
==============================================================================*/

using UnityEngine;
using UnityEngine.UI;

namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
public Transform TextTargetName;
public Transform ButtonAction;
public Transform TextDescription;
public Transform PanelDescription;

#region PRIVATE_MEMBER_VARIABLES

private TrackableBehaviour mTrackableBehaviour;

#endregion // PRIVATE_MEMBER_VARIABLES

#region UNTIY_MONOBEHAVIOUR_METHODS

void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}

#endregion // UNTIY_MONOBEHAVIOUR_METHODS

#region PUBLIC_METHODS

/// <summary>
/// Implementation of the ITrackableEventHandler function called when the
/// tracking state changes.
/// </summary>
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}

#endregion // PUBLIC_METHODS

#region PRIVATE_METHODS

private void OnTrackingFound()


{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}

// Enable colliders:
foreach (Collider component in colliderComponents)
{
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)
{
component.enabled = false;
}

// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}

Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");

//Evertime the target lost / no target found it will show “???” on the
TextTargetName. Button, Description and Panel will invicible (inactive)

TextTargetName.GetComponent<Text> ().text = "???";


ButtonAction.gameObject.SetActive(false);
TextDescription.gameObject.SetActive(false);
PanelDescription.gameObject.SetActive(false);

}
#endregion // PRIVATE_METHODS
}
}

targetData.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

namespace Vuforia
{
public class targetData : MonoBehaviour
{

public Transform TextTargetName;


public Transform TextDescription;
public Transform ButtonAction;
public Transform PanelDescription;

public AudioSource soundTarget;


public AudioClip clipTarget;

// Use this for initialization


void Start()
{
//add Audio Source as new game object component
soundTarget = (AudioSource)gameObject.AddComponent<AudioSource>();
}

// Update is called once per frame


void Update()
{
StateManager sm = TrackerManager.Instance.GetStateManager();
IEnumerable<TrackableBehaviour> tbs = sm.GetActiveTrackableBehaviours();

foreach (TrackableBehaviour tb in tbs)


{
string name = tb.TrackableName;
ImageTarget it = tb.Trackable as ImageTarget;
Vector2 size = it.GetSize();

Debug.Log("Active image target:" + name + " -size: " + size.x + ", "
+ size.y);

//Evertime the target found it will show “name of target” on the


TextTargetName. Button, Description and Panel will visible (active)

TextTargetName.GetComponent<Text>().text = name;
ButtonAction.gameObject.SetActive(true);
TextDescription.gameObject.SetActive(true);
PanelDescription.gameObject.SetActive(true);
//If the target name was “zombie” then add listener to ButtonAction
with location of the zombie sound (locate in Resources/sounds folder)
and set text on TextDescription a description of the zombie

if(name == "zombie"){
ButtonAction.GetComponent<Button>().onClick.AddListener(delegate {
playSound("sounds/ZombieLongDeath"); });
TextDescription.GetComponent<Text>().text = "A zombie (Haitian
French: zombi, Haitian Creole: zonbi) is a fictional undead being created through the
reanimation of a human corpse. Zombies are most commonly found in horror and fantasy
genre works.";
}

//If the target name was “unitychan” then add listener to ButtonAction
with location of the unitychan sound (locate in Resources/sounds
folder) and set text on TextDescription a description of the unitychan
/ robot

if (name == "unitychan")
{
ButtonAction.GetComponent<Button>().onClick.AddListener(delegate {
playSound("sounds/HelloBabyGirl"); });
TextDescription.GetComponent<Text>().text = "A robot is a
mechanical or virtual artificial agent, usually an electromechanical machine that is
guided by a computer program or electronic circuitry, and thus a type of an embedded
system.";
}
}
}

//function to play sound


void playSound(string ss){
clipTarget = (AudioClip)Resources.Load(ss);
soundTarget.clip = clipTarget;
soundTarget.loop = false;
soundTarget.playOnAwake = false;
soundTarget.Play();
}
}
}

You might also like