Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public interface ICardIdentifierListener {
  5. void OnTargetChange(Transform t);
  6. void OnTargetFound(Transform t);
  7. void OnTargetTracked(Transform t);
  8. void OnTargetLost(Transform t);
  9. }
  10.  
  11. public static class CardIdentifierExt {
  12. // notify gameObject by sendMessage
  13. public static void listenCardIdentifier(this GameObject obj, string identifierName) {
  14. CardIdentifier.addObserver(obj, identifierName);
  15. }
  16.  
  17. // notify monoBehaviour by Interface
  18. public static void listenCardIdentifier(this ICardIdentifierListener listener, string identifierName) {
  19. CardIdentifier.addObserver(listener, identifierName);
  20. }
  21. }
  22.  
  23. /// <summary>
  24. /// CardIdentifier[Observalbe]<br/>
  25. /// notify the card transform
  26. /// set a list of [GameObject/ICardIdentifierListener]
  27. /// </summary>
  28. public class CardIdentifier : HiARBaseObjectMonoBehaviour {
  29.  
  30. public static readonly Dictionary<string, CardIdentifier> Identifiers = new Dictionary<string, CardIdentifier>();
  31.  
  32. private readonly List<GameObject> _objects = new List<GameObject>();
  33. private readonly List<ICardIdentifierListener> _behaviours = new List<ICardIdentifierListener>();
  34.  
  35. public bool IsFound { get; private set; }
  36.  
  37. public void addObserver(GameObject obj) {
  38. if (_objects.Contains(obj)) return;
  39. _objects.Add(obj);
  40. }
  41.  
  42. public void addObserver(ICardIdentifierListener behaviour) {
  43. if (_behaviours.Contains(behaviour)) return;
  44. _behaviours.Add(behaviour);
  45. }
  46.  
  47. public static void addObserver(GameObject obj, string identifierName) {
  48. if (!Identifiers.ContainsKey(identifierName)) {
  49. Debug.LogWarning("no such CardIdentifier which named " + identifierName);
  50. return;
  51. }
  52. Identifiers[identifierName].addObserver(obj);
  53. }
  54.  
  55. public static void addObserver(ICardIdentifierListener behaviour, string identifierName) {
  56. if (!Identifiers.ContainsKey(identifierName)) {
  57. Debug.LogWarning("no such CardIdentifier which named " + identifierName);
  58. return;
  59. }
  60. Identifiers[identifierName].addObserver(behaviour);
  61. }
  62.  
  63. public void removeObserver(GameObject obj) {
  64. _objects.Remove(obj);
  65. }
  66.  
  67. public void removeObserver(ICardIdentifierListener behaviour) {
  68. _behaviours.Remove(behaviour);
  69. }
  70.  
  71. public static void removeObserver(GameObject obj, string identifierName) {
  72. if (!Identifiers.ContainsKey(identifierName)) {
  73. Debug.LogWarning("no such CardIdentifier which named " + identifierName);
  74. return;
  75. }
  76. Identifiers[identifierName].removeObserver(obj);
  77. }
  78.  
  79. public static void removeObserver(ICardIdentifierListener behaviour, string identifierName) {
  80. if (!Identifiers.ContainsKey(identifierName)) {
  81. Debug.LogWarning("no such CardIdentifier which named " + identifierName);
  82. return;
  83. }
  84. Identifiers[identifierName].removeObserver(behaviour);
  85. }
  86.  
  87. void Awake() {
  88. if (Identifiers.ContainsKey(name)) {
  89. Debug.LogError("CardIdentifier: you already have this CardIdentifier, which named " + name);
  90. Debug.Break();
  91. }
  92. Identifiers.Add(name, this);
  93. }
  94.  
  95. /// <summary>
  96. /// do not hide the children in this object
  97. /// </summary>
  98. void Start() {
  99. // to override the default
  100. }
  101.  
  102. // change target to another
  103. public void OnTargetChange(anotherTransform) {
  104. foreach (var o in _objects) {
  105. o.SendMessage("OnTargetChange", anotherTransform);
  106. }
  107. foreach (var c in _behaviours) {
  108. c.OnTargetChange(anotherTransform);
  109. }
  110. }
  111.  
  112. // *** the callback interface ***
  113.  
  114. public void OnTargetFound(string targetId) {
  115. IsFound = true;
  116. foreach (var o in _objects) {
  117. o.SendMessage("OnTargetFound", transform);
  118. }
  119. foreach (var c in _behaviours) {
  120. c.OnTargetFound(transform);
  121. }
  122. }
  123.  
  124. public void OnTargetTracked(string targetId) {
  125. foreach (var o in _objects) {
  126. o.SendMessage("OnTargetTracked", transform);
  127. }
  128. foreach (var c in _behaviours) {
  129. c.OnTargetTracked(transform);
  130. }
  131. }
  132.  
  133. public void OnTargetLost(string targetId) {
  134. IsFound = false;
  135. foreach (var o in _objects) {
  136. o.SendMessage("OnTargetLost", transform);
  137. }
  138. foreach (var c in _behaviours) {
  139. c.OnTargetLost(transform);
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement