Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement