Skip to content

Commit 1193878

Browse files
authored
opt: reduce the frequency of loading images in the Editor (#453)
1 parent 8d0f326 commit 1193878

6 files changed

Lines changed: 25 additions & 9 deletions

File tree

Editor/Scripts/EditorManagers/EditorApplicationManager.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,19 @@ static void EditorApplicationManager_Update() {
9696
//Remove jobs
9797
foreach (IEditorTask job in m_toRemoveTasks) {
9898
m_activeEditorTasks.Remove(job);
99-
}
99+
m_lastTaskExecuteTime.Remove(job);
100+
}
100101
m_toRemoveTasks.Clear();
101102

102103
//Execute
103104
foreach (IEditorTask job in m_activeEditorTasks) {
105+
if (m_lastTaskExecuteTime.TryGetValue(job, out double lastExecuteTime)) {
106+
if ((time - lastExecuteTime) <= job.GetExecutionFrequency())
107+
continue;
108+
}
109+
104110
job.Execute();
111+
m_lastTaskExecuteTime[job] = time;
105112
}
106113

107114

@@ -183,6 +190,9 @@ private static readonly ImageLoadEditorTask[] m_imageLoadEditorUpdateTasks
183190
private static readonly List<IEditorTask> m_requestedTasks = new List<IEditorTask>();
184191
private static readonly HashSet<IEditorTask> m_toRemoveTasks = new HashSet<IEditorTask>();
185192

193+
private static readonly Dictionary<IEditorTask, double> m_lastTaskExecuteTime = new Dictionary<IEditorTask, double>();
194+
195+
186196
private static event Action<bool> OnUnityEditorFocus;
187197
private static bool m_editorFocused;
188198

Editor/Scripts/EditorManagers/IEditorTask.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33

44
internal interface IEditorTask : ITask {
5-
void Reset();
5+
void Reset();
6+
float GetExecutionFrequency(); //once every xx in seconds
67
}
78

89
}

Editor/Scripts/EditorManagers/ImageLoadEditorTask.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public void Reset() {
1111
m_latestFrame = 0;
1212
}
1313

14+
public float GetExecutionFrequency() {
15+
return 0; //as soon as possible
16+
}
17+
1418
//----------------------------------------------------------------------------------------------------------------------
1519
public void Execute() {
1620

Editor/Scripts/EditorManagers/SISPlayableMixerEditorTask.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public SISPlayableMixerEditorTask(StreamingImageSequencePlayableMixer mixer) : b
1818
public void Reset() {
1919
}
2020

21+
public float GetExecutionFrequency() {
22+
return 1.0f / 10.0f;
23+
}
2124

2225
//----------------------------------------------------------------------------------------------------------------------
2326

@@ -67,7 +70,7 @@ public void Execute() {
6770
bool needsRefresh = false;
6871
foreach (KeyValuePair<TimelineClip, StreamingImageSequencePlayableAsset> kv in clipAssets) {
6972
StreamingImageSequencePlayableAsset sisAsset = kv.Value;
70-
sisAsset.ContinuePreloadingImages();
73+
sisAsset.ContinuePreloadingImages(numNeighboringImagesToLoad:1);
7174

7275
if (sisAsset.UpdateTextureWithRequestedImage()) {
7376
needsRefresh = true;

Runtime/Scripts/Features/SIS/StreamingImageSequencePlayableAsset.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,16 @@ internal bool UpdateTextureWithRequestedImage() {
294294

295295
//----------------------------------------------------------------------------------------------------------------------
296296

297-
internal void ContinuePreloadingImages() {
297+
internal void ContinuePreloadingImages(int numNeighboringImagesToLoad) {
298298

299299
if (null == m_imageFiles || 0== m_imageFiles.Count)
300300
return;
301301

302302
if (m_folder.IsRegularAssetPath())
303303
return;
304304

305-
const int NUM_IMAGES = 2;
306-
307305
//forward
308-
int maxForwardPreloadIndex = Mathf.Min(m_forwardPreloadImageIndex + NUM_IMAGES, m_imageFiles.Count) -1;
306+
int maxForwardPreloadIndex = Mathf.Min(m_forwardPreloadImageIndex + numNeighboringImagesToLoad, m_imageFiles.Count) -1;
309307
int startForwardPreloadIndex = m_forwardPreloadImageIndex;
310308
for (int i = startForwardPreloadIndex; i <= maxForwardPreloadIndex; ++i) {
311309
if (QueueImageLoadTask(i, out _)) {
@@ -316,7 +314,7 @@ internal void ContinuePreloadingImages() {
316314
}
317315

318316
//backward
319-
int minBackwardPreloadIndex = Mathf.Max((m_backwardPreloadImageIndex - NUM_IMAGES)+1, 0);
317+
int minBackwardPreloadIndex = Mathf.Max((m_backwardPreloadImageIndex - numNeighboringImagesToLoad)+1, 0);
320318
int startBackwardPreloadIndex = m_backwardPreloadImageIndex;
321319
for (int i = startBackwardPreloadIndex; i >=minBackwardPreloadIndex; --i) {
322320
if (QueueImageLoadTask(i, out _)) {

Runtime/Scripts/Features/SIS/StreamingImageSequencePlayableMixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public override void ProcessFrame(Playable playable, FrameData info, object play
9696

9797
//Start to preload images before the clip is active
9898
if ( directorTime>= startTime - loadStartOffsetTime && directorTime < endTime) {
99-
sisAsset.ContinuePreloadingImages();
99+
sisAsset.ContinuePreloadingImages(numNeighboringImagesToLoad:1);
100100
}
101101

102102
}

0 commit comments

Comments
 (0)