暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TimelineAnalytics.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. PlayableDirector[] directorsInScene = GetAllDirectorsInScene();
  95. IEnumerable<TimelineAsset> timelines = directorsInScene.Select(pd => pd.playableAsset).OfType<TimelineAsset>().Distinct();
  96. foreach (var timeline in timelines)
  97. {
  98. if (_timelineSceneInfo.uniqueDirectors.Add(timeline))
  99. {
  100. _timelineSceneInfo.numTracks += timeline.flattenedTracks.Count();
  101. _timelineSceneInfo.minDuration = Math.Min(_timelineSceneInfo.minDuration, (int)(timeline.duration * 1000));
  102. _timelineSceneInfo.maxDuration = Math.Max(_timelineSceneInfo.maxDuration, (int)(timeline.duration * 1000));
  103. _timelineSceneInfo.minNumTracks = Math.Min(_timelineSceneInfo.minNumTracks, timeline.flattenedTracks.Count());
  104. _timelineSceneInfo.maxNumTracks = Math.Max(_timelineSceneInfo.maxNumTracks, timeline.flattenedTracks.Count());
  105. foreach (var track in timeline.flattenedTracks)
  106. {
  107. string key = track.GetType().Name;
  108. if (_timelineSceneInfo.trackCount.ContainsKey(key))
  109. {
  110. _timelineSceneInfo.trackCount[key]++;
  111. }
  112. else
  113. {
  114. if (TimelineEventInfo.IsUserType(track.GetType()))
  115. {
  116. _timelineSceneInfo.trackCount["UserType"]++;
  117. if (_timelineSceneInfo.userTrackTypesCount.ContainsKey(key))
  118. _timelineSceneInfo.userTrackTypesCount[key]++;
  119. else
  120. _timelineSceneInfo.userTrackTypesCount[key] = 1;
  121. }
  122. else
  123. _timelineSceneInfo.trackCount["Other"]++;
  124. }
  125. if (track.clips.Any(x => x.recordable))
  126. _timelineSceneInfo.numRecorded++;
  127. else
  128. {
  129. var animationTrack = track as AnimationTrack;
  130. if (animationTrack != null)
  131. {
  132. if (animationTrack.CanConvertToClipMode())
  133. _timelineSceneInfo.numRecorded++;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. static PlayableDirector[] GetAllDirectorsInScene()
  142. {
  143. #if UNITY_2023_1_OR_NEWER
  144. return UnityEngine.Object.FindObjectsByType<PlayableDirector>(UnityEngine.FindObjectsSortMode.None);
  145. #else
  146. return UnityEngine.Object.FindObjectsOfType<PlayableDirector>();
  147. #endif
  148. }
  149. class TimelineAnalyticsPostProcess : IPostprocessBuildWithReport
  150. {
  151. public int callbackOrder { get { return 0; } }
  152. public void OnPostprocessBuild(BuildReport report)
  153. {
  154. if (_timelineSceneInfo.uniqueDirectors.Count > 0)
  155. {
  156. var timelineEvent = new TimelineEventInfo(_timelineSceneInfo);
  157. EditorAnalytics.SendEventTimelineInfo(timelineEvent);
  158. }
  159. }
  160. }
  161. }
  162. }