Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShaderGraphAnalytics.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine.Analytics;
  4. using UnityEngine;
  5. using System;
  6. namespace UnityEditor.ShaderGraph
  7. {
  8. class ShaderGraphAnalytics
  9. {
  10. const int k_MaxEventsPerHour = 1000;
  11. const int k_MaxNumberOfElements = 1000;
  12. const string k_VendorKey = "unity.shadergraph";
  13. const string k_EventName = "uShaderGraphUsage";
  14. [AnalyticInfo(eventName: k_EventName, vendorKey: k_VendorKey, maxEventsPerHour:k_MaxEventsPerHour, maxNumberOfElements:k_MaxNumberOfElements)]
  15. public class Analytic : IAnalytic
  16. {
  17. public Analytic(string assetGuid, GraphData graph)
  18. {
  19. this.assetGuid = assetGuid;
  20. this.graph = graph;
  21. }
  22. [Serializable]
  23. struct AnalyticsData : IAnalytic.IData
  24. {
  25. public string nodes;
  26. public int node_count;
  27. public string asset_guid;
  28. public int subgraph_count;
  29. }
  30. public bool TryGatherData(out IAnalytic.IData data, out Exception error)
  31. {
  32. Dictionary<string, int> nodeTypeAndCount = new Dictionary<string, int>();
  33. var nodes = graph.GetNodes<AbstractMaterialNode>();
  34. int subgraphCount = 0;
  35. foreach (var materialNode in nodes)
  36. {
  37. string nType = materialNode.GetType().ToString().Split('.').Last();
  38. if (nType == "SubGraphNode")
  39. {
  40. subgraphCount += 1;
  41. }
  42. if (!nodeTypeAndCount.ContainsKey(nType))
  43. {
  44. nodeTypeAndCount[nType] = 1;
  45. }
  46. else
  47. {
  48. nodeTypeAndCount[nType] += 1;
  49. }
  50. }
  51. var jsonRepr = DictionaryToJson(nodeTypeAndCount);
  52. data = new AnalyticsData()
  53. {
  54. nodes = jsonRepr,
  55. node_count = nodes.Count(),
  56. asset_guid = assetGuid,
  57. subgraph_count = subgraphCount
  58. };
  59. error = null;
  60. return true;
  61. }
  62. string assetGuid;
  63. GraphData graph;
  64. };
  65. public static void SendShaderGraphEvent(string assetGuid, GraphData graph)
  66. {
  67. //The event shouldn't be able to report if this is disabled but if we know we're not going to report
  68. //Lets early out and not waste time gathering all the data
  69. if (!EditorAnalytics.enabled)
  70. return;
  71. Analytic analytic = new Analytic(assetGuid, graph);
  72. EditorAnalytics.SendAnalytic(analytic);
  73. }
  74. static string DictionaryToJson(Dictionary<string, int> dict)
  75. {
  76. var entries = dict.Select(d => $"\"{d.Key}\":{string.Join(",", d.Value)}");
  77. return "{" + string.Join(",", entries) + "}";
  78. }
  79. }
  80. }