UniversalAnalytics.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine.Analytics;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using UnityEngine.Rendering.Universal;
  8. using UnityEditor.Build;
  9. using UnityEditor.Build.Reporting;
  10. namespace UnityEditor.Rendering.Universal
  11. {
  12. class UniversalAnalytics : IPostprocessBuildWithReport
  13. {
  14. static bool s_EventRegistered = false;
  15. const int k_MaxEventsPerHour = 1000;
  16. const int k_MaxNumberOfElements = 1000;
  17. const string k_VendorKey = "unity.universal";
  18. const string k_EventName = "uUniversalRenderPipelineUsage";
  19. static bool EnableAnalytics()
  20. {
  21. AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
  22. if (result == AnalyticsResult.Ok)
  23. s_EventRegistered = true;
  24. return s_EventRegistered;
  25. }
  26. static void SendUniversalEvent()
  27. {
  28. //The event shouldn't be able to report if this is disabled but if we know we're not going to report
  29. //Lets early out and not waste time gathering all the data
  30. if (!EditorAnalytics.enabled)
  31. return;
  32. if (!EnableAnalytics())
  33. return;
  34. // Needd to check if this isn't null
  35. UniversalRenderPipelineAsset rendererAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
  36. if (rendererAsset != null)
  37. {
  38. ScriptableRendererData[] rendererDataList = rendererAsset.m_RendererDataList;
  39. string mainLightMode = rendererAsset.mainLightRenderingMode.ToString();
  40. string additionalLightMode = rendererAsset.additionalLightsRenderingMode.ToString();
  41. HashSet<string> rendererDatas = new HashSet<string>();
  42. HashSet<string> renderFeatures = new HashSet<string>();
  43. int rendererDataAmount = 0;
  44. int rendererFeaturesAmount = 0;
  45. foreach (ScriptableRendererData rendererData in rendererDataList)
  46. {
  47. if (rendererData != null)
  48. {
  49. rendererDataAmount++;
  50. rendererDatas.Add(rendererData.GetType().ToString());
  51. foreach (ScriptableRendererFeature rendererFeature in rendererData.rendererFeatures)
  52. {
  53. rendererFeaturesAmount++;
  54. renderFeatures.Add(rendererFeature.GetType().ToString());
  55. }
  56. }
  57. }
  58. var data = new AnalyticsData()
  59. {
  60. renderer_data = rendererDatas.ToArray(),
  61. renderer_data_amount = rendererDataAmount,
  62. renderer_features = renderFeatures.ToArray(),
  63. renderer_features_amount = rendererFeaturesAmount,
  64. main_light_rendering_mode = mainLightMode,
  65. additional_light_rendering_mode = additionalLightMode,
  66. };
  67. EditorAnalytics.SendEventWithLimit(k_EventName, data);
  68. }
  69. }
  70. struct AnalyticsData
  71. {
  72. public string[] renderer_data;
  73. public int renderer_data_amount;
  74. public string[] renderer_features;
  75. public int renderer_features_amount;
  76. public string main_light_rendering_mode;
  77. public string additional_light_rendering_mode;
  78. }
  79. public int callbackOrder { get; }
  80. public void OnPostprocessBuild(BuildReport report)
  81. {
  82. SendUniversalEvent();
  83. }
  84. }
  85. }