CinemachineEditorAnalytics.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Cinemachine.Editor
  6. {
  7. [InitializeOnLoad]
  8. static class CinemachineEditorAnalytics
  9. {
  10. const int k_MaxEventsPerHour = 360;
  11. const int k_MaxNumberOfElements = 1000;
  12. const string k_VendorKey = "unity.cinemachine";
  13. // register an event handler when the class is initialized
  14. static CinemachineEditorAnalytics()
  15. {
  16. EditorApplication.playModeStateChanged += SendAnalyticsOnPlayEnter;
  17. }
  18. /// <summary>
  19. /// Send analytics event when using Create -> Cinemachine menu
  20. /// </summary>
  21. /// <param name="name">Name of the vcam created</param>
  22. public static void SendCreateEvent(string name)
  23. {
  24. if (!EditorAnalytics.enabled)
  25. return;
  26. var data = new CreateEventData
  27. {
  28. vcam_created = name,
  29. };
  30. // Register our event
  31. EditorAnalytics.RegisterEventWithLimit("cm_create_vcam",
  32. k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
  33. // Send the data to the database
  34. EditorAnalytics.SendEventWithLimit("cm_create_vcam", data);
  35. }
  36. struct CreateEventData
  37. {
  38. public string vcam_created; // vcam created from Create -> Cinemachine menu
  39. }
  40. /// <summary>
  41. /// Send analytics event when using entering playmode
  42. /// </summary>
  43. /// <param name="state">State change to detect entering playmode</param>
  44. static void SendAnalyticsOnPlayEnter(PlayModeStateChange state)
  45. {
  46. // Only send analytics if it is enabled
  47. if (!EditorAnalytics.enabled)
  48. return;
  49. // Only send data when entering playmode
  50. if (state != PlayModeStateChange.EnteredPlayMode)
  51. return;
  52. var startTime = Time.realtimeSinceStartup;
  53. var cinemachineCore = CinemachineCore.Instance;
  54. var vcamCount = cinemachineCore.VirtualCameraCount;
  55. var vcamDatas = new List<VcamData>();
  56. // collect data from all vcams
  57. for (int i = 0; i < vcamCount; ++i)
  58. {
  59. var vcamBase = cinemachineCore.GetVirtualCamera(i);
  60. CollectData(vcamBase, i.ToString(), ref vcamDatas);
  61. }
  62. var projectData = new ProjectData
  63. {
  64. brain_count = cinemachineCore.BrainCount,
  65. vcam_count = cinemachineCore.VirtualCameraCount,
  66. cam_count = Camera.allCamerasCount,
  67. vcams = vcamDatas,
  68. time_elapsed = Time.realtimeSinceStartup - startTime,
  69. };
  70. // Register our event
  71. EditorAnalytics.RegisterEventWithLimit("cm_vcams_on_play",
  72. k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
  73. // Send the data to the database
  74. EditorAnalytics.SendEventWithLimit("cm_vcams_on_play", projectData);
  75. }
  76. static void CollectData(CinemachineVirtualCameraBase vcamBase, string id, ref List<VcamData> vcamDatas)
  77. {
  78. if (vcamBase == null) return;
  79. var vcamData = new VcamData(id, vcamBase);
  80. // VirtualCamera
  81. var vcam = vcamBase as CinemachineVirtualCamera;
  82. if (vcam != null)
  83. {
  84. vcamData.SetTransitionsAndLens(vcam.m_Transitions, vcam.m_Lens);
  85. vcamData.SetComponents(vcam.GetComponentPipeline());
  86. vcamDatas.Add(vcamData);
  87. return;
  88. }
  89. #if CINEMACHINE_EXPERIMENTAL_VCAM
  90. // NewVirtualCamera or NewFreeLook
  91. var vcamNew = vcamBase as CinemachineNewVirtualCamera;
  92. if (vcamNew != null)
  93. {
  94. vcamData.SetTransitionsAndLens(vcamNew.m_Transitions, vcamNew.m_Lens);
  95. vcamData.SetComponents(vcamNew.ComponentCache);
  96. vcamDatas.Add(vcamData);
  97. return;
  98. }
  99. #endif
  100. // Composite vcam (Freelook, Mixing, StateDriven, ClearShot...):
  101. var freeLook = vcamBase as CinemachineFreeLook;
  102. if (freeLook != null)
  103. {
  104. vcamData.SetTransitionsAndLens(freeLook.m_Transitions, freeLook.m_Lens);
  105. }
  106. vcamDatas.Add(vcamData);
  107. var vcamChildren =
  108. vcamBase.GetComponentsInChildren<CinemachineVirtualCameraBase>();
  109. for (var c = 1; c < vcamChildren.Length; c++)
  110. {
  111. if ((CinemachineVirtualCameraBase)vcamChildren[c].ParentCamera == vcamBase)
  112. CollectData(vcamChildren[c], id + "." + c, ref vcamDatas);
  113. }
  114. }
  115. [Serializable]
  116. struct ProjectData
  117. {
  118. public int brain_count;
  119. public int vcam_count;
  120. public int cam_count;
  121. public List<VcamData> vcams;
  122. public float time_elapsed;
  123. }
  124. [Serializable]
  125. struct VcamData
  126. {
  127. public string id;
  128. public string vcam_class;
  129. public bool has_follow_target;
  130. public bool has_lookat_target;
  131. public string blend_hint;
  132. public bool inherit_position;
  133. public string standby_update;
  134. public string mode_overwrite;
  135. public string body_component;
  136. public string aim_component;
  137. public string noise_component;
  138. public int custom_component_count;
  139. public string[] extensions;
  140. public int custom_extension_count;
  141. public VcamData(string id, CinemachineVirtualCameraBase vcamBase) : this()
  142. {
  143. var _ = 0;
  144. this.id = id;
  145. vcam_class = GetTypeName(vcamBase.GetType(), ref _);
  146. has_follow_target = vcamBase.Follow != null;
  147. has_lookat_target = vcamBase.LookAt != null;
  148. blend_hint = "";
  149. inherit_position = false;
  150. standby_update = vcamBase.m_StandbyUpdate.ToString();
  151. mode_overwrite = "";
  152. body_component = "";
  153. aim_component = "";
  154. noise_component = "";
  155. custom_component_count = 0;
  156. custom_extension_count = 0;
  157. var vcamExtensions = vcamBase.mExtensions;
  158. if (vcamExtensions != null)
  159. {
  160. extensions = new string[vcamExtensions.Count];
  161. for (var i = 0; i < vcamExtensions.Count; i++)
  162. {
  163. extensions[i] = (GetTypeName(vcamExtensions[i].GetType(), ref custom_extension_count));
  164. }
  165. }
  166. else
  167. {
  168. extensions = Array.Empty<string>();
  169. }
  170. }
  171. public void SetTransitionsAndLens(
  172. CinemachineVirtualCameraBase.TransitionParams transitions, LensSettings lens)
  173. {
  174. blend_hint = transitions.m_BlendHint.ToString();
  175. inherit_position = transitions.m_InheritPosition;
  176. mode_overwrite = lens.ModeOverride.ToString();
  177. }
  178. public void SetComponents(CinemachineComponentBase[] cmComps)
  179. {
  180. custom_component_count = 0;
  181. body_component = aim_component = noise_component = "";
  182. if (cmComps != null)
  183. {
  184. for (var i = 0; i < cmComps.Length; i++)
  185. {
  186. #if CINEMACHINE_EXPERIMENTAL_VCAM
  187. if (cmComps[i] == null) continue;
  188. #endif
  189. var componentName = GetTypeName(cmComps[i].GetType(), ref custom_component_count);
  190. switch (cmComps[i].Stage)
  191. {
  192. case CinemachineCore.Stage.Body:
  193. body_component = componentName;
  194. break;
  195. case CinemachineCore.Stage.Aim:
  196. aim_component = componentName;
  197. break;
  198. case CinemachineCore.Stage.Noise:
  199. noise_component = componentName;
  200. break;
  201. default:
  202. break;
  203. }
  204. }
  205. }
  206. }
  207. static string GetTypeName(Type type, ref int customTypeCount)
  208. {
  209. if (typeof(CinemachineBrain).Assembly != type.Assembly)
  210. {
  211. ++customTypeCount;
  212. return "Custom";
  213. }
  214. return type.Name;
  215. }
  216. }
  217. }
  218. }