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