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