/CloudImageTracker.cs Secret
Created
February 21, 2020 12:13
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Wikitude; | |
public class CloudImageTracker : MonoBehaviour { | |
public GameObject trackerPrefab; | |
private GameObject trackerObject; | |
private ImageTracker imageTracker; | |
private string TrackerName = "CloudImageTracker"; | |
private void ActivateImageTracker (string clientToken, string targetCollectionId) { | |
RemoveImageTracker (); | |
CreateImageTracker (); | |
ConfigureImageTracker (clientToken, targetCollectionId); | |
ListenToImageTrackerEvents (); | |
} | |
private void RemoveImageTracker () { | |
trackerObject = GameObject.Find (TrackerName); | |
if (trackerObject) { Destroy (trackerObject); } | |
} | |
private void CreateImageTracker () { | |
trackerObject = GameObject.Instantiate (trackerPrefab); | |
trackerObject.name = TrackerName; | |
trackerObject.transform.SetParent (transform, false); | |
} | |
private void ConfigureImageTracker (string clientToken, string targetCollectionId) { | |
if (trackerObject != null) { | |
imageTracker = trackerObject.GetComponent<ImageTracker> (); | |
imageTracker.TargetSourceType = TargetSourceType.CloudRecognitionService; | |
imageTracker.CloudRecognitionService.ClientToken = clientToken; | |
imageTracker.CloudRecognitionService.TargetCollectionId = targetCollectionId; | |
} else { | |
Log ("Tracker not available"); | |
} | |
} | |
private void ListenToImageTrackerEvents () { | |
if (imageTracker != null) { | |
imageTracker.CloudRecognitionService.OnInitialized.AddListener (OnInitialized); | |
} else { | |
Log ("Image Tracker not available"); | |
} | |
} | |
// Will be called once CloudRecognitionService was initialized | |
private void OnInitialized () { | |
Log ("OnInitialized"); | |
StartContinuousRecognition (); // this only call server once | |
// InvokeRepeating ("RecognizeOnce", 0.0f, 1f); // this works as expected - calls server once a second | |
} | |
private void RecognizeOnce () { | |
Log ("RecognizeOnce"); | |
if (imageTracker != null) { | |
imageTracker.CloudRecognitionService.Recognize (); | |
} else { | |
Log ("Image Tracker not available"); | |
} | |
} | |
private void StartContinuousRecognition () { | |
Log ("StartContinuousRecognition"); | |
double intervalInMilliseconds = 1000; | |
if (imageTracker != null) { | |
imageTracker.CloudRecognitionService.StartContinuousRecognition (intervalInMilliseconds); | |
} else { | |
Log ("Image Tracker not available"); | |
} | |
} | |
private void StopContinuousRecognition () { | |
Log ("StopContinuousRecognition"); | |
if (imageTracker != null) { | |
imageTracker.CloudRecognitionService.StopContinuousRecognition (); | |
} else { | |
Log ("Image Tracker not available"); | |
} | |
} | |
// NOTE: Will be called via Button.Click() for testing | |
public void ActivateWorkspace1 () { | |
string clientToken = "c5b6478a67039df68bbf74c6c60ad700"; | |
string targetCollectionId = "5e4a598fe6133e24c76b3ee1"; | |
ActivateImageTracker (clientToken, targetCollectionId); | |
} | |
// NOTE: Will be called via Button.Click() for testing | |
public void ActivateWorkspace2 () { | |
string clientToken = "c5b6478a67039df68bbf74c6c60ad700"; | |
string targetCollectionId = "5e4a57350f913b24c03a4244"; | |
ActivateImageTracker (clientToken, targetCollectionId); | |
} | |
// NOTE: Will be called via Button.Click() for testing | |
public void Reset () { | |
StopContinuousRecognition (); | |
RemoveImageTracker (); | |
} | |
private void Log (string message) { | |
Debug.Log (message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment