Advertisement
noradninja

ImageSequencePlayer

Jul 4th, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. /*
  2. To use this script, if your project doesn't already have a Resources folder, create one inside the root Assets folder in Unity. Then, make a folder in there to hold your image sequence.
  3. Number your images Image000, Image001, etc. IT IS CRUCIAL TO START WITH 000- if you don't, you will have a single blank frame when the animation loops
  4. Put the script on the object you want to play back the sequence.
  5. Set the options according to the comments below to point Unity at your image sequence.
  6. */
  7.  
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. public class SequencePlayer : MonoBehaviour {
  13.  
  14.      //A texture object that will output the animation  
  15.     private Texture texture;  
  16.     //With this Material object, a reference to the game object Material can be stored  
  17.     public RawImage rawImage;  
  18.     //An integer to advance frames  
  19.     private int frameCounter = 0;  
  20.  
  21.     //A string that holds the name of the folder which contains the image sequence  
  22.     public string folderName;  
  23.     //The name of the image sequence  
  24.     public string imageSequenceName;  
  25.     //The number of frames the animation has  
  26.     public int numberOfFrames;  
  27.     //Toggle animation looping
  28.     public bool loop = true;
  29.     //The base name of the files of the sequence  
  30.     public float frameDelay = 0.05f;
  31.    private string baseName;  
  32.    private float lastCallTime;
  33.  
  34.     void Awake()  
  35.     {  
  36.         //Get a reference to the Material of the game object this script is attached to  
  37.         this.rawImage = this.GetComponent<RawImage>();
  38.         //With the folder name and the sequence name, get the full path of the images (without the numbers)  
  39.         this.baseName = this.folderName + "/" + this.imageSequenceName;  
  40.     }  
  41.  
  42.     void Start ()  
  43.     {  
  44.         //set the initial frame as the first texture. Load it from the first image on the folder  
  45.         texture = (Texture)Resources.Load(baseName + "000", typeof(Texture));  
  46.     }  
  47.  
  48.     void Update ()  
  49.     {  
  50.      
  51.             if (loop == true){
  52.                 //Start the 'PlayLoop' method as a coroutine  
  53.                 StartCoroutine("PlayLoop", frameDelay);  
  54.             }
  55.             else if (loop == false){
  56.                 //Start the 'Play' method as a coroutine
  57.                 StartCoroutine("Play", frameDelay);
  58.             }
  59.             //Set the material's texture to the current value of the frameCounter variable  
  60.             rawImage.texture = this.texture;  
  61.        
  62.     }  
  63.  
  64.     //The following methods return a IEnumerator so they can be yielded:  
  65.     //A method to play the animation in a loop  
  66.     IEnumerator PlayLoop(float delay)  
  67.     {  
  68.         //wait for the time defined at the delay parameter  
  69.         yield return new WaitForSeconds(delay);    
  70.  
  71.         //advance one frame  
  72.         frameCounter = (++frameCounter)%numberOfFrames;  
  73.  
  74.         //load the current frame  
  75.         this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D3"), typeof(Texture));  
  76.  
  77.         //Stop this coroutine  
  78.         StopCoroutine("PlayLoop");  
  79.     }  
  80.  
  81.     //A method to play the animation just once  
  82.     IEnumerator Play(float delay)  
  83.     {  
  84.         //wait for the time defined at the delay parameter  
  85.         yield return new WaitForSeconds(delay);    
  86.  
  87.         //if it isn't the last frame  
  88.         if(frameCounter < numberOfFrames)  
  89.         {  
  90.             //Advance one frame  
  91.             ++frameCounter;  
  92.  
  93.             //load the current frame  
  94.             this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D3"), typeof(Texture));  
  95.         }  
  96.  
  97.         //Stop this coroutine  
  98.         StopCoroutine("Play");  
  99.     }  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement