- Home /
Resources.Load have a bad performance
I'm trying to play a movie in Android by using SpriteRenderer because MovieTexture can't be used for Android. Basically, I convert the movie into images and load it as Sprite and assign it to a SpriteRenderer every frame. I put the images in Resources/Movie folder and use this code to load the Image. (Variable counter is what frame the movie currently playing)
Sprite GetFrame(){
Sprite newSprite = Resources.Load("Movie/picture" + counter.ToString("0000"), typeof(Sprite)) as Sprite;
return newSprite;
}
At the beginning, the movie play smoothly, but after 10 seconds or so the movie starting to lag. I try to unload the resource every 2 seconds using Resources.UnloadUnusedAssets();
but the problem still exist. Here is my full code for this.
using UnityEngine;
using System.Collections;
public class CPlayButton : MonoBehaviour {
private const int FPS = 24;
private const int UNLOAD_TIME = 2;
public SpriteRenderer movieTexture;
private float SPF = 1f / FPS;
private float UNLOAD_FRAME = 2 * FPS;
private float timeCounter = 0;
private int counter = 0;
private int unloadCounter = 0;
private bool isPlaying = false;
// Use this for initialization
void Start () {
timeCounter = SPF;
}
// Update is called once per frame
void FixedUpdate () {
if (isPlaying) {
if(timeCounter >= SPF){
counter++;
movieTexture.sprite = GetFrame();
timeCounter = timeCounter - SPF;
if(unloadCounter >= UNLOAD_FRAME){
Resources.UnloadUnusedAssets();
unloadCounter = 0;
}else{
unloadCounter++;
}
}else{
timeCounter += Time.fixedDeltaTime;
}
}
}
Sprite GetFrame(){
Sprite newSprite = Resources.Load("Movie/picture" + counter.ToString("0000"), typeof(Sprite)) as Sprite;
return newSprite;
}
void OnMouseDown(){
isPlaying = true;
}
}
Can somebody help me about this problem? Or anybody know a better way of doing this?
Best would be too load a certain amount of images before starting to play.
You could also try to run it all without unloading as it seems to correspond to the starting of the lagging.
$$anonymous$$aybe just nullify your images and let the GC do its job or call GC.Collect once you are done with the full movie.
Also, can't you just use Handheld class for playing movie?
Other idea, try to compare using Resources and placing your images in the data folder and using File.ReadAllBytes.
i try to load certain amount of images in the beginning and load the rest while playing the loaded image, but when the loaded image already played, the new images not finish loaded. so the problem still same. i try the handheld already but the problem is handheld stop all process other than playing the movie. so i cant make other calculation while playing the movie. $$anonymous$$aybe the only choice is to use plugin as suggested by zeppike
Answer by siaran · May 03, 2015 at 10:27 PM
Try not loading a single frame each update. Instead, load all your frames into memory in Start() and loop through them in Update().
Or make an animation prefab for your animation and use that + the animator.
Sorry not mention it in the question, but my movie is around 1 - 2 $$anonymous$$s long. So for 24 fps, there are around 2000 images. I tried load all the frames in the beginning but it load so long.
can't you store it as a movie ins$$anonymous$$d of a set of images? because you really shouldn't need to load/unload an image during an update...as you said it's not very fast...
as I said in the question, I tried $$anonymous$$ovieTexture to open movie file, but it can't be used for Android. There is only fullscreen movie player using HandHeld for Android. But I want to process something while the movie playing and using HandHeld make my process stop. Or do you have other way to play movie file?
Use the $$anonymous$$obile$$anonymous$$ovieTextures Asset is is 70 or so dollars, but it will save you a lot of time, performance and pain.
Actually, I never buy anything in asset store. Are you sure that plugin will work? I don't know how to check a plugin is really work or not without buy it first.
Your answer
![](https://koobas.hobune.stream/wayback/20220613194559im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Resources.Load sprite returning null 2 Answers
Change sprite in a sprite renderer with an animation 0 Answers
Assign multiple sprites to a renderer, or assign multiple renderers to single object 1 Answer
Change sprite's color without making an extra drawcall 0 Answers
Cut holes in sprite using other sprites 2 Answers