Easy way to load in a sequence of tga files?

I have a large number of animation sequences with each frame stored in individual tga files. Example, an explosion for a particular type of weapon has a sequence of files named:

explosion_proton_torpedo_1.tga,
explosion_proton_torpedo_2.tga
.
. <files 3 to 31>
.
explosion_proton_torpedo_32.tga

So 32 files each containing a frame that I would throw on a camera facing plane to show the explosion. In my game I have about 50 different types of weapons so basically 1600 files to load.

In c# is there a way I can load these files through a script that reads a directory these files are in? Along with loading the file I would also need to get the file name so I can parse it to know which weapon to associate the file to and what the frame number is.

Also, how would I get these tga files to be part of the “deployment package” for the game on installed and run on a phone, tablet, and pc/mac?

Thanks for any help!

Hi rsud

Just worth starting with a note that your approach is probably not the best for doing animations, especially explosions. Typically you might look up sprite animation in unity (in which you pack all your frames into 1 file), or for explosions have a look at the particle system which will yield very nice results that can vary from shot to shot. However, it may be too late to change, or you may be doing it this way for a good reason, so I’ll leave that to you and go onto a solution :slight_smile:

First up, getting them into the package. By default unity works out all the assets that are needed by looking at a scene, working out what assets it needs, checking if they depend on anything etc etc. It’s all automatic. However, if you want to manually load assets from code (as in your case), you have to tell unity ‘I dont care what you think - include these assets’! To do so, you’ll need to create a folder called ‘Resources’ somewhere in your project, and store your files inside that folder. Any files below a resources folder are automatically included in the project.

Once your textures are stored in a resource folder, you can use the unity ‘Resources’ api to load them. The code below is a simple script that takes a name (like explosion_proton_torpedo), then uses it to try and load a sequence of resources (like explosion_proton_torpedo_1, explosion_proton_torpedo_2 etc) until it fails to load something.

using UnityEngine;
using System.Collections;
using System.Collections.Generic; //need to include this, as 'List' is inside it

public class TestLoad : MonoBehaviour {

	//this will store a list of textures
	List<Texture2D> loaded_textures;

	// Use this for initialization
	void Start () 
	{
		//call our loading function - in this case I'm loading a sequence of resources
		//called testframe_1, testframe_2 etc etc
		loaded_textures = LoadTextures("testframe");
	}

	// Loads a list of textures
	List<Texture2D> LoadTextures(string name)
	{
		//create list
		List<Texture2D> textures = new List<Texture2D>();

		//keep loading textures until we fail to find one
		int index = 0;
		while(true)
		{
			//generate the name - it is <name>_<index>
			//as our textures start at '_1', we have to add 1 to the index when generating the name
			string resource_name = string.Format("{0}_{1}",name,index+1);

			//ask resource system to load the texture
			Texture2D mytexture = (Texture2D)Resources.Load(resource_name);

			//if it succeeds, store it. otherwise assume we hit the end and bail out
			if(mytexture != null)
				textures.Add(mytexture);
			else
				break;

			index++;
		}

		//print out how many textures we loaded
		Debug.Log(string.Format("Loaded {0} textures for {1}", index, name));

		//return it
		return textures;
	}
}

That gives you a loaded list of textures. I’ll leave it you to decide where to store the names (probably a public member variable you set in the inspector I imagine?), and how to use them. As I said though, unless you have a good reason not to, definitely consider the particle system for explosions - much easier to author and will give you nice varied effects every time an explosion occurs.

Enjoy!

-Chris