- Home /
Dynamic loading of textures - looking for best appraoch
In my unity project I have huge number of textures(frames) as assets. Around ten thousand. Basically these are collection of frames from a video shoot. If I try to use Resource.Load() for all of these frames in start() method then my unity project takes almost 5/7 minutes to run. So, this approach is not fruitful at all.
Analog electrical signals are coming from two Steppers (left and right) and the Unity is processing them. When the signal is detected from the Stepper I will have move forward 15(size of each step) frames at a time.
To do this kind of operation I have defined an array of size 15. Now in the start() method I load this array by first fifteen frames from my resource directory.
Now suppose the signal comes from the left Stepper, which is captured in update() method. In the FixedUpdate() I traverse this Array and render the frames one by one. After rendering I clear this array with Array.Clear(...) and load it with next 15 frames.
So when the next signal comes from the right stepper - these frames from the Array are also rendered one by one. So far so good.
The problem is - when the Stepper speed gets bit higher it sends signal more frequently and as a result Unity crashes.
What would be more efficient way to tackle this kind of scenario.
Should I use two Arrays (one for left, one for right) so that there is considerable lag. I'm trying to keep the solution simple in the sense that I'm assuming left-right-left-right...Stepper sequence for the time being.
sounds like a lot of data. What's the reason for the crash? Is it memory?
There is a lot important information missing:
What target platform?
Do you have some constraints on memory? Or what's the memory amount that you require?
What's the image size of one "frame"? (width x height)
What exactly do you mean by "rendered one by one"? Do you render one image every "frame" or Unity, do you draw the images next to each other as some sort of list? Why do you use FixedUpdate ? FixedUpdate has nothing to do with rendering so it's use here seems very odd.
How do you actually get the information from your stepper?
What do you consider a "higher speed"? What's the "normal" (or acceptable) speed in actual time units?
Answer by Glurth · Mar 04, 2017 at 05:13 PM
I would recommend the implementation of a "circular cache buffer" to store the frames. This kind of buffer is a specified size (usually a few seconds of playtime), and has TWO indexes; a read index and write index. The indexes are incremented such that when they reach the end of the array, they loop back to the start (thus: "circular").
The operations used to fill the next element of the array, at the write_index, are performed constantly/every update, for some specified amount of time, or until the write_buffer_index reaches the element before (circularly) the read_index_buffer. This way, when a read operation is performed, and the read index is increment, it will automatically load new data "behind" it.
Once you have this in place, you stepper "play" commands simply trigger the system to start drawing the next 15 elements in the circular cache buffer.
Watchout: if you are "playing" the frames faster than you can "load" them, your read index will eventually catch up to your write index! Not sure what you want to do in this situation; I would probably pause "playing" for a few seconds, while it buffers/loads more frames.
Your answer
Follow this Question
Related Questions
Standard assets problems 0 Answers
how to solve Texture is not accessible error and Can't read from cubemap error? 0 Answers
Do all assets from Assets folder is loading into memory? 1 Answer
Loading external file after build 1 Answer
Exporting Assets Bundle with Lightmaps included possible? 0 Answers