TextureSwap Animator

From Unify Community Wiki
Jump to: navigation, search


by PsychicParrot.

Description

Attach it to a GUITexture, feed it a bunch of materials and it will cycle through them. I don't recommend this method, since using a texture atlas is more memory efficient.. but it's fine for simple GUI animations or for getting things up and running quickly.

usage

Save the file below as "autoTextureSwappingGUITexture.cs"

Attach to GUITexture.

Add materials using the editor (put them into the materials list)

Watch as they cycle!

If you need to adjust the speed, change the changeInterval declaration (here it is shown as 0.33f).

using UnityEngine;
 
public class autoTextureSwappingGUITexture : MonoBehaviour {
 
	public Texture[] materials; 
	public float changeInterval = 0.33f;
	public int startFrame = 0;
	private GUITexture myGUITexture;
 
	// Use this for initialization
	void Start () {
 
		startFrame=0;
 
		// if you want to start the animation on a random frame, uncomment the line below:
		//startFrame=Random.Range(0,materials.Length);	
 
		myGUITexture=gameObject.guiTexture;
 
	}
 
	// Update is called once per frame
	void Update () {
 
			// we want this material index now 
			float index = startFrame + (Time.time / changeInterval); 
 
			// take a modulo with materials count so that animation repeats 
			index = index % materials.Length; 
 
			// assign it to the GUITexture 
			myGUITexture.texture=materials[(int)index];
 
	}
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox