InputAnalytics.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #if UNITY_ANALYTICS || UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine.InputSystem.Layouts;
  5. #if UNITY_EDITOR
  6. using UnityEngine.InputSystem.Editor;
  7. #endif
  8. ////FIXME: apparently shutdown events are not coming through in the analytics backend
  9. namespace UnityEngine.InputSystem
  10. {
  11. internal static class InputAnalytics
  12. {
  13. public const string kEventStartup = "input_startup";
  14. public const string kEventShutdown = "input_shutdown";
  15. public static void Initialize(InputManager manager)
  16. {
  17. Debug.Assert(manager.m_Runtime != null);
  18. }
  19. public static void OnStartup(InputManager manager)
  20. {
  21. var data = new StartupEventData
  22. {
  23. version = InputSystem.version.ToString(),
  24. };
  25. // Collect recognized devices.
  26. var devices = manager.devices;
  27. var deviceList = new List<StartupEventData.DeviceInfo>();
  28. for (var i = 0; i < devices.Count; ++i)
  29. {
  30. var device = devices[i];
  31. deviceList.Add(
  32. StartupEventData.DeviceInfo.FromDescription(device.description, device.native, device.layout));
  33. }
  34. data.devices = deviceList.ToArray();
  35. // Collect unrecognized devices.
  36. deviceList.Clear();
  37. var availableDevices = manager.m_AvailableDevices;
  38. var availableDeviceCount = manager.m_AvailableDeviceCount;
  39. for (var i = 0; i < availableDeviceCount; ++i)
  40. {
  41. var deviceId = availableDevices[i].deviceId;
  42. if (manager.TryGetDeviceById(deviceId) != null)
  43. continue;
  44. deviceList.Add(StartupEventData.DeviceInfo.FromDescription(availableDevices[i].description,
  45. availableDevices[i].isNative));
  46. }
  47. data.unrecognized_devices = deviceList.ToArray();
  48. #if UNITY_EDITOR
  49. data.new_enabled = EditorPlayerSettingHelpers.newSystemBackendsEnabled;
  50. data.old_enabled = EditorPlayerSettingHelpers.oldSystemBackendsEnabled;
  51. #endif
  52. manager.m_Runtime.RegisterAnalyticsEvent(kEventStartup, 10, 100);
  53. manager.m_Runtime.SendAnalyticsEvent(kEventStartup, data);
  54. }
  55. public static void OnShutdown(InputManager manager)
  56. {
  57. var metrics = manager.metrics;
  58. var data = new ShutdownEventData
  59. {
  60. max_num_devices = metrics.maxNumDevices,
  61. max_state_size_in_bytes = metrics.maxStateSizeInBytes,
  62. total_event_bytes = metrics.totalEventBytes,
  63. total_event_count = metrics.totalEventCount,
  64. total_frame_count = metrics.totalUpdateCount,
  65. total_event_processing_time = (float)metrics.totalEventProcessingTime,
  66. };
  67. manager.m_Runtime.RegisterAnalyticsEvent(kEventShutdown, 10, 100);
  68. manager.m_Runtime.SendAnalyticsEvent(kEventShutdown, data);
  69. }
  70. /// <summary>
  71. /// Data about what configuration we start up with.
  72. /// </summary>
  73. /// <remarks>
  74. /// Has data about the devices present at startup so that we can know what's being
  75. /// used out there. Also has data about devices we couldn't recognize.
  76. ///
  77. /// Note that we exclude devices that are always present (e.g. keyboard and mouse
  78. /// on desktops or touchscreen on phones).
  79. /// </remarks>
  80. [Serializable]
  81. public struct StartupEventData
  82. {
  83. public string version;
  84. public DeviceInfo[] devices;
  85. public DeviceInfo[] unrecognized_devices;
  86. ////REVIEW: ATM we have no way of retrieving these in the player
  87. #if UNITY_EDITOR
  88. public bool new_enabled;
  89. public bool old_enabled;
  90. #endif
  91. [Serializable]
  92. public struct DeviceInfo
  93. {
  94. public string layout;
  95. public string @interface;
  96. public string product;
  97. public bool native;
  98. public static DeviceInfo FromDescription(InputDeviceDescription description, bool native = false, string layout = null)
  99. {
  100. string product;
  101. if (!string.IsNullOrEmpty(description.product) && !string.IsNullOrEmpty(description.manufacturer))
  102. product = $"{description.manufacturer} {description.product}";
  103. else if (!string.IsNullOrEmpty(description.product))
  104. product = description.product;
  105. else
  106. product = description.manufacturer;
  107. if (string.IsNullOrEmpty(layout))
  108. layout = description.deviceClass;
  109. return new DeviceInfo
  110. {
  111. layout = layout,
  112. @interface = description.interfaceName,
  113. product = product,
  114. native = native
  115. };
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Data about when after startup the user first interacted with the application.
  121. /// </summary>
  122. [Serializable]
  123. public struct FirstUserInteractionEventData
  124. {
  125. }
  126. /// <summary>
  127. /// Data about what level of data we pumped through the system throughout its lifetime.
  128. /// </summary>
  129. [Serializable]
  130. public struct ShutdownEventData
  131. {
  132. public int max_num_devices;
  133. public int max_state_size_in_bytes;
  134. public int total_event_bytes;
  135. public int total_event_count;
  136. public int total_frame_count;
  137. public float total_event_processing_time;
  138. }
  139. }
  140. }
  141. #endif // UNITY_ANALYTICS || UNITY_EDITOR