TimelineAnalytics.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Build;
  5. using UnityEditor.Build.Reporting;
  6. using UnityEngine.Playables;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.Timeline;
  9. namespace UnityEditor.Timeline.Analytics
  10. {
  11. class TimelineSceneInfo
  12. {
  13. public Dictionary<string, int> trackCount = new Dictionary<string, int>
  14. {
  15. {"ActivationTrack", 0},
  16. {"AnimationTrack", 0},
  17. {"AudioTrack", 0},
  18. {"ControlTrack", 0},
  19. {"PlayableTrack", 0},
  20. {"UserType", 0},
  21. {"Other", 0}
  22. };
  23. public Dictionary<string, int> userTrackTypesCount = new Dictionary<string, int>();
  24. public HashSet<TimelineAsset> uniqueDirectors = new HashSet<TimelineAsset>();
  25. public int numTracks = 0;
  26. public int minDuration = int.MaxValue;
  27. public int maxDuration = int.MinValue;
  28. public int minNumTracks = int.MaxValue;
  29. public int maxNumTracks = int.MinValue;
  30. public int numRecorded = 0;
  31. }
  32. [Serializable]
  33. struct TrackInfo
  34. {
  35. public string name;
  36. public double percent;
  37. }
  38. [Serializable]
  39. class TimelineEventInfo
  40. {
  41. public int num_timelines;
  42. public int min_duration, max_duration;
  43. public int min_num_tracks, max_num_tracks;
  44. public double recorded_percent;
  45. public List<TrackInfo> track_info = new List<TrackInfo>();
  46. public string most_popular_user_track = string.Empty;
  47. public TimelineEventInfo(TimelineSceneInfo sceneInfo)
  48. {
  49. num_timelines = sceneInfo.uniqueDirectors.Count;
  50. min_duration = sceneInfo.minDuration;
  51. max_duration = sceneInfo.maxDuration;
  52. min_num_tracks = sceneInfo.minNumTracks;
  53. max_num_tracks = sceneInfo.maxNumTracks;
  54. recorded_percent = Math.Round(100.0 * sceneInfo.numRecorded / sceneInfo.numTracks, 1);
  55. foreach (KeyValuePair<string, int> kv in sceneInfo.trackCount.Where(x => x.Value > 0))
  56. {
  57. track_info.Add(new TrackInfo()
  58. {
  59. name = kv.Key,
  60. percent = Math.Round(100.0 * kv.Value / sceneInfo.numTracks, 1)
  61. });
  62. }
  63. if (sceneInfo.userTrackTypesCount.Any())
  64. {
  65. most_popular_user_track = sceneInfo.userTrackTypesCount
  66. .First(x => x.Value == sceneInfo.userTrackTypesCount.Values.Max()).Key;
  67. }
  68. }
  69. public static bool IsUserType(Type t)
  70. {
  71. string nameSpace = t.Namespace;
  72. return string.IsNullOrEmpty(nameSpace) || !nameSpace.StartsWith("UnityEngine.Timeline");
  73. }
  74. }
  75. static class TimelineAnalytics
  76. {
  77. static TimelineSceneInfo _timelineSceneInfo = new TimelineSceneInfo();
  78. class TimelineAnalyticsPreProcess : IPreprocessBuildWithReport
  79. {
  80. public int callbackOrder { get { return 0; } }
  81. public void OnPreprocessBuild(BuildReport report)
  82. {
  83. _timelineSceneInfo = new TimelineSceneInfo();
  84. }
  85. }
  86. class TimelineAnalyticsProcess : IProcessSceneWithReport
  87. {
  88. public int callbackOrder
  89. {
  90. get { return 0; }
  91. }
  92. public void OnProcessScene(Scene scene, BuildReport report)
  93. {
  94. var timelines = UnityEngine.Object.FindObjectsOfType<PlayableDirector>().Select(pd => pd.playableAsset).OfType<TimelineAsset>().Distinct();
  95. foreach (var timeline in timelines)
  96. {
  97. if (_timelineSceneInfo.uniqueDirectors.Add(timeline))
  98. {
  99. _timelineSceneInfo.numTracks += timeline.flattenedTracks.Count();
  100. _timelineSceneInfo.minDuration = Math.Min(_timelineSceneInfo.minDuration, (int)(timeline.duration * 1000));
  101. _timelineSceneInfo.maxDuration = Math.Max(_timelineSceneInfo.maxDuration, (int)(timeline.duration * 1000));
  102. _timelineSceneInfo.minNumTracks = Math.Min(_timelineSceneInfo.minNumTracks, timeline.flattenedTracks.Count());
  103. _timelineSceneInfo.maxNumTracks = Math.Max(_timelineSceneInfo.maxNumTracks, timeline.flattenedTracks.Count());
  104. foreach (var track in timeline.flattenedTracks)
  105. {
  106. string key = track.GetType().Name;
  107. if (_timelineSceneInfo.trackCount.ContainsKey(key))
  108. {
  109. _timelineSceneInfo.trackCount[key]++;
  110. }
  111. else
  112. {
  113. if (TimelineEventInfo.IsUserType(track.GetType()))
  114. {
  115. _timelineSceneInfo.trackCount["UserType"]++;
  116. if (_timelineSceneInfo.userTrackTypesCount.ContainsKey(key))
  117. _timelineSceneInfo.userTrackTypesCount[key]++;
  118. else
  119. _timelineSceneInfo.userTrackTypesCount[key] = 1;
  120. }
  121. else
  122. _timelineSceneInfo.trackCount["Other"]++;
  123. }
  124. if (track.clips.Any(x => x.recordable))
  125. _timelineSceneInfo.numRecorded++;
  126. else
  127. {
  128. var animationTrack = track as AnimationTrack;
  129. if (animationTrack != null)
  130. {
  131. if (animationTrack.CanConvertToClipMode())
  132. _timelineSceneInfo.numRecorded++;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. class TimelineAnalyticsPostProcess : IPostprocessBuildWithReport
  141. {
  142. public int callbackOrder {get { return 0; }}
  143. public void OnPostprocessBuild(BuildReport report)
  144. {
  145. if (_timelineSceneInfo.uniqueDirectors.Count > 0)
  146. {
  147. var timelineEvent = new TimelineEventInfo(_timelineSceneInfo);
  148. EditorAnalytics.SendEventTimelineInfo(timelineEvent);
  149. }
  150. }
  151. }
  152. }
  153. }