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?
↧