UniversalAnalytics.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (rendererFeature != null)
  54. {
  55. rendererFeaturesAmount++;
  56. renderFeatures.Add(rendererFeature.GetType().ToString());
  57. }
  58. }
  59. }
  60. }
  61. var data = new AnalyticsData()
  62. {
  63. renderer_data = rendererDatas.ToArray(),
  64. renderer_data_amount = rendererDataAmount,
  65. renderer_features = renderFeatures.ToArray(),
  66. renderer_features_amount = rendererFeaturesAmount,
  67. main_light_rendering_mode = mainLightMode,
  68. additional_light_rendering_mode = additionalLightMode,
  69. };
  70. EditorAnalytics.SendEventWithLimit(k_EventName, data);
  71. }
  72. }
  73. struct AnalyticsData
  74. {
  75. public string[] renderer_data;
  76. public int renderer_data_amount;
  77. public string[] renderer_features;
  78. public int renderer_features_amount;
  79. public string main_light_rendering_mode;
  80. public string additional_light_rendering_mode;
  81. }
  82. public int callbackOrder { get; }
  83. public void OnPostprocessBuild(BuildReport report)
  84. {
  85. SendUniversalEvent();
  86. }
  87. }
  88. }