PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Assets/GameAnalytics/Plugins/Components/GA_Tracker.cs

https://bitbucket.org/AgentCodeMonkey/gameframework-unity-project
C# | 131 lines | 98 code | 19 blank | 14 comment | 11 complexity | 0f32859421559fe6884c3a9de2d90065 MD5 | raw file
  1. /// <summary>
  2. /// Add to a game object or prefab to set up Game Analytic's automatic event tracking.
  3. /// </summary>
  4. using UnityEngine;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System;
  8. public class GA_Tracker : MonoBehaviour
  9. {
  10. public enum GAEventType { BreadCrumb, Start, OnDestroy, OnMouseDown, OnLevelWasLoaded, OnTriggerEnter, OnCollisionEnter, OnControllerColliderHit }
  11. public static Dictionary<GAEventType, string> EventTooltips = new Dictionary<GAEventType, string>()
  12. {
  13. { GAEventType.BreadCrumb, "Send an event every second. Good for generating heatmaps of player position/movement in your levels." },
  14. { GAEventType.Start, "Send an event when the Start method is run. Use this for tracking spawning of the object" },
  15. { GAEventType.OnDestroy, "Send an event when the OnDestroy method is run. Use this for tracking \"death\" of the object." },
  16. { GAEventType.OnMouseDown, "Send an event when the OnMouseDown method is run. Use this for tracking when the player performs a click/touch on the object." },
  17. { GAEventType.OnLevelWasLoaded, "Send an event when the OnLevelWasLoaded method is run. Use this for tracking when a new level is loaded." },
  18. { GAEventType.OnTriggerEnter, "Send an event when the OnTriggerEnter method is run. Use this for tracking when something (f.x. the player) enters a trigger area." },
  19. { GAEventType.OnCollisionEnter, "Send an event when the OnCollisionEnter method is run. Use this for tracking when objects collide." },
  20. { GAEventType.OnControllerColliderHit, "Send an event when the OnControllerColliderHit method is run. Use this for tracking when a controller hits a collider while performing a Move." }
  21. };
  22. [SerializeField]
  23. public List<GAEventType> TrackedEvents = new List<GAEventType>();
  24. public bool TrackedEventsFoldOut = false;
  25. public bool TrackTarget = false;
  26. private static bool _trackTargetAlreadySet;
  27. private float _lastBreadCrumbTrackTime;
  28. /*void Awake()
  29. {
  30. if (_trackCounter == null)
  31. {
  32. _trackCounter = new Dictionary<string, int>[Enum.GetValues(typeof(GAEventType)).Length];
  33. for (int i = 0; i < _trackCounter.Length; i++)
  34. {
  35. _trackCounter[i] = new Dictionary<string, int>();
  36. }
  37. }
  38. }*/
  39. void Start()
  40. {
  41. if (TrackedEvents.Contains(GAEventType.Start))
  42. {
  43. GA.API.Design.NewEvent("Start:"+gameObject.name, transform.position);
  44. }
  45. if (TrackTarget)
  46. {
  47. GA.Settings.TrackTarget = transform;
  48. if (_trackTargetAlreadySet)
  49. {
  50. GA.LogWarning("You should only set the Track Target of GA_Tracker once per scene");
  51. }
  52. _trackTargetAlreadySet = true;
  53. }
  54. }
  55. void Update()
  56. {
  57. if (TrackedEvents.Contains(GAEventType.BreadCrumb))
  58. {
  59. if (Time.time > _lastBreadCrumbTrackTime + 1.0f)
  60. {
  61. _lastBreadCrumbTrackTime = Time.time;
  62. GA.API.Design.NewEvent("BreadCrumb:"+gameObject.name, transform.position);
  63. }
  64. }
  65. }
  66. void OnDestroy()
  67. {
  68. if (TrackedEvents.Contains(GAEventType.OnDestroy))
  69. {
  70. GA.API.Design.NewEvent("OnDestroy:"+gameObject.name, transform.position);
  71. }
  72. }
  73. void OnMouseDown()
  74. {
  75. if (TrackedEvents.Contains(GAEventType.OnMouseDown))
  76. {
  77. GA.API.Design.NewEvent("OnMouseDown:"+gameObject.name, transform.position);
  78. }
  79. }
  80. public void OnLevelWasLoaded ()
  81. {
  82. if (TrackedEvents.Contains(GAEventType.OnLevelWasLoaded))
  83. {
  84. GA.API.Design.NewEvent("OnLevelWasLoaded:"+gameObject.name, transform.position);
  85. }
  86. }
  87. public void OnTriggerEnter ()
  88. {
  89. if (TrackedEvents.Contains(GAEventType.OnTriggerEnter))
  90. {
  91. GA.API.Design.NewEvent("OnTriggerEnter:"+gameObject.name, transform.position);
  92. }
  93. }
  94. public void OnCollisionEnter ()
  95. {
  96. if (TrackedEvents.Contains(GAEventType.OnCollisionEnter))
  97. {
  98. GA.API.Design.NewEvent("OnCollisionEnter:"+gameObject.name, transform.position);
  99. }
  100. }
  101. public void OnControllerColliderHit ()
  102. {
  103. if (TrackedEvents.Contains(GAEventType.OnControllerColliderHit))
  104. {
  105. GA.API.Design.NewEvent("OnControllerColliderHit:"+gameObject.name, transform.position);
  106. }
  107. }
  108. public System.Array GetEventValues()
  109. {
  110. return Enum.GetValues(typeof(GAEventType));
  111. }
  112. }