TextureSwap Animator

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 17: Line 17:
using UnityEngine;
using UnityEngine;
using System.Collections;
public class autoTextureSwappingGUITexture : MonoBehaviour {
public class autoTextureSwappingGUITexture : MonoBehaviour {

Revision as of 15:17, 5 September 2009

by PsychicParrot.


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.


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).

<csharp> 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 () {


// if you want to start the animation on a random frame, uncomment the line below: //startFrame=Random.Range(0,materials.Length);



// 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];


} </csharp>

Personal tools