I want to give your comments on this script please

the script loads three images from resources and assigns it a iguiimge and tested and works well for purpose to reproduce a sequence but I want to know if anyone knows a better way or some improvement.

using System.Collections.Generic;
using UnityEngine;
using System.Collections;

public class AnimationSequenceLoader : MonoBehaviour
{
    public iGUI.iGUIImage Projection;
    public Texture2D[] images = new Texture2D[3];
    public int max = 100;
    public string path = "Animations/1/sequence01_";
 
    void Start ()
    {
        StartCoroutine(PlaySequence(path));
    }

    private IEnumerator PlaySequence(string fullpath)
    {
        //assets are unloaded before starting the cycle
        Resources.UnloadUnusedAssets(); 
        for (int i = 0, e = 0,unload=0; i <= max; unload++)
        {

            //assigned only when the three previous images and were used
            if (e == 0)
            {
                //unload assets
               Resources.UnloadAsset(images[0]);
               Resources.UnloadAsset(images[1]);
               Resources.UnloadAsset(images[2]);
                //increment index and assign first image
                i++;
                images[0] =
                    Resources.Load(
                        string.Format("{0}{1}",fullpath, i)) as
                    Texture2D; 
                //increment index and assign second image
                i++;
                images[1] =
                    Resources.Load(
                        string.Format("{0}{1}", fullpath, i)) as
                    Texture2D; 
                //increment index and assign three image
                i++;
                images[2] =
                    Resources.Load(
                        string.Format("{0}{1}", fullpath, i)) as
                    Texture2D;
            }

            yield return new WaitForSeconds(0.08f);

            if(images[e]!=null)
                Projection.image = images[e];
            //increment index of image display
            e++;
            //if index is equal 3 resets to 0
            if (e == 3)
            {
                e = 0;
            }
            //this is for a loop sequence 
            //reset index to load image
            if (i > (max-1))
            {
                i = 0;
            }
        }
    } 
}

be grateful for any comments they may make the script :slight_smile:

Is there a reason it loads the images in groups of three, instead of one at a time?
Assuming there is, you could still make the script more generic by referring to images.Length instead of “3”, and using a loop inside the unload/load block.
Also, it looks like the variable unload is not actually used for anything.

UnloadUnusedAssets can be an intensive operation, probably not something you want to do at the start of every animation. If the reason you’re calling that is to clean up the last loaded images from the previous animation, you can do that in OnDestroy.

Other than that, it looks fine. You could make the speed adjustable, or allow loop toggling, but I’m not sure whether those are necessary in the purpose you’re using it for.

the purpose of charging the 3 is for the switch from one to another is smooth without waiting to load the next image.
would have preferred that when you deploy the first of three the following sicle preload the 4 image index 1, and so forth but not staying sufficient time to improve, and the lag I had to load the images was not when I use: )

the play animation when user play the animation the lag, on their moment not supposed a problem