Question by
KnightRiderGuy · Dec 11, 2016 at 08:42 PM ·
c#movietexturecounterrawmovie texture
Count Each Time Movie Clip Plays on a loop
I have this script that plays a movie to a loop but I want to add a counter so that I can view how many times the clip has played. For Counting Sheep.
public MovieTexture movTexture;
public RawImage mySheepMovie;
void Start() {
GetComponent<RawImage>().texture = movTexture as MovieTexture;
movTexture.Play();
movTexture.loop = true;
}
Comment
Best Answer
Answer by TBruce · Dec 11, 2016 at 08:50 PM
You could accomplish this using a coroutine like this
public MovieTexture movTexture;
public RawImage mySheepMovie;
public int maxSheepCount = -1;
private int sheepCount = 0;
void Start() {
if (movTexture != null)
{
GetComponent<RawImage>().texture = movTexture as MovieTexture;
StartCoroutine(CountSheepLoops());
}
}
IEnumerator CountSheepLoops()
{
// if the movie did not start playing yet, start it
if (!movTexture.isPlaying)
{
movTexture.loop = true;
movTexture.Play();
}
// wait till the end of the movie to increment the sheepCount value
yield return new WaitForSeconds(movTexture.duration);
sheepCount++;
// check to se iff we want to keep counting sheep
if ((maxSheepCount < 0) || (maxSheepCount > sheepCount))
{
// if so call the coroutine again
StartCoroutine(CountSheepLoops());
}
else
{
movTexture.Stop(); // remove this if you want
}
}
Thanks @$$anonymous$$avina, This is perfect, I was trying to do it with a coroutine that invoked repeating every 0.8 seconds which I figured was the duration of my short animated movie clip, but it was not very accurate at counting as it got out of sync with my movie after a few loops, this method looks to be WAY better. :)