Skip to content

Instantly share code, notes, and snippets.

@5alamander
Last active June 1, 2016 03:17
Show Gist options
  • Save 5alamander/2b5185c96ba752a41f77956fdbcc3d87 to your computer and use it in GitHub Desktop.
Save 5alamander/2b5185c96ba752a41f77956fdbcc3d87 to your computer and use it in GitHub Desktop.
TargetTrack for HiAR-SDK
using UnityEngine;
using System.Collections.Generic;
public interface ICardIdentifierListener {
void OnTargetChange(Transform t);
void OnTargetFound(Transform t);
void OnTargetTracked(Transform t);
void OnTargetLost(Transform t);
}
public static class CardIdentifierExt {
// notify gameObject by sendMessage
public static void listenCardIdentifier(this GameObject obj, string identifierName) {
CardIdentifier.addObserver(obj, identifierName);
}
// notify monoBehaviour by Interface
public static void listenCardIdentifier(this ICardIdentifierListener listener, string identifierName) {
CardIdentifier.addObserver(listener, identifierName);
}
}
/// <summary>
/// CardIdentifier[Observalbe]<br/>
/// notify the card transform
/// set a list of [GameObject/ICardIdentifierListener]
/// </summary>
public class CardIdentifier : HiARBaseObjectMonoBehaviour {
public static readonly Dictionary<string, CardIdentifier> Identifiers = new Dictionary<string, CardIdentifier>();
private readonly List<GameObject> _objects = new List<GameObject>();
private readonly List<ICardIdentifierListener> _behaviours = new List<ICardIdentifierListener>();
public bool IsFound { get; private set; }
public void addObserver(GameObject obj) {
if (_objects.Contains(obj)) return;
_objects.Add(obj);
}
public void addObserver(ICardIdentifierListener behaviour) {
if (_behaviours.Contains(behaviour)) return;
_behaviours.Add(behaviour);
}
public static void addObserver(GameObject obj, string identifierName) {
if (!Identifiers.ContainsKey(identifierName)) {
Debug.LogWarning("no such CardIdentifier which named " + identifierName);
return;
}
Identifiers[identifierName].addObserver(obj);
}
public static void addObserver(ICardIdentifierListener behaviour, string identifierName) {
if (!Identifiers.ContainsKey(identifierName)) {
Debug.LogWarning("no such CardIdentifier which named " + identifierName);
return;
}
Identifiers[identifierName].addObserver(behaviour);
}
public void removeObserver(GameObject obj) {
_objects.Remove(obj);
}
public void removeObserver(ICardIdentifierListener behaviour) {
_behaviours.Remove(behaviour);
}
public static void removeObserver(GameObject obj, string identifierName) {
if (!Identifiers.ContainsKey(identifierName)) {
Debug.LogWarning("no such CardIdentifier which named " + identifierName);
return;
}
Identifiers[identifierName].removeObserver(obj);
}
public static void removeObserver(ICardIdentifierListener behaviour, string identifierName) {
if (!Identifiers.ContainsKey(identifierName)) {
Debug.LogWarning("no such CardIdentifier which named " + identifierName);
return;
}
Identifiers[identifierName].removeObserver(behaviour);
}
void Awake() {
if (Identifiers.ContainsKey(name)) {
Debug.LogError("CardIdentifier: you already have this CardIdentifier, which named " + name);
Debug.Break();
}
Identifiers.Add(name, this);
}
/// <summary>
/// do not hide the children in this object
/// </summary>
void Start() {
// to override the default
}
// change target to another
public void OnTargetChange(anotherTransform) {
foreach (var o in _objects) {
o.SendMessage("OnTargetChange", anotherTransform);
}
foreach (var c in _behaviours) {
c.OnTargetChange(anotherTransform);
}
}
// *** the callback interface ***
public void OnTargetFound(string targetId) {
IsFound = true;
foreach (var o in _objects) {
o.SendMessage("OnTargetFound", transform);
}
foreach (var c in _behaviours) {
c.OnTargetFound(transform);
}
}
public void OnTargetTracked(string targetId) {
foreach (var o in _objects) {
o.SendMessage("OnTargetTracked", transform);
}
foreach (var c in _behaviours) {
c.OnTargetTracked(transform);
}
}
public void OnTargetLost(string targetId) {
IsFound = false;
foreach (var o in _objects) {
o.SendMessage("OnTargetLost", transform);
}
foreach (var c in _behaviours) {
c.OnTargetLost(transform);
}
}
}
using UnityEngine;
public class Usage : MonoBehaviour, ICardIdentifierListener {
public string CardIdentifierName;
void Start () {
this.listenCardIdentifier(CardIdentifierName);
// this.listenCardIdentifier("card2");
}
void Update () {}
public void OnTargetChange(Transform t) {}
public void OnTargetFound(Transform t) {}
public void OnTargetTracked(Transform t) {
this.transform.rotation = t.rotation;
}
public void OnTargetLost(Transform t) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment