- Home /
Making textures Scroll/ animate textures
how do i get a set texture to scroll from left to right, and or change a materials image over time?
Answer by Tetrad · Jun 17, 2010 at 09:16 PM
http://www.unifycommunity.com/wiki/index.php?title=Animating_Tiled_texture
That script will step through a texture sheet. The important bit is this particular line of code
renderer.material.SetTextureOffset ("_MainTex", offset);
With that you can also write a script that animates offset over time smoothly. In fact, here's an example of one:
using UnityEngine; using System.Collections;
public class AnimatedUVs : MonoBehaviour { public int materialIndex = 0; public Vector2 uvAnimationRate = new Vector2( 1.0f, 0.0f ); public string textureName = "_MainTex";
Vector2 uvOffset = Vector2.zero;
void LateUpdate()
{
uvOffset += ( uvAnimationRate * Time.deltaTime );
if( renderer.enabled )
{
renderer.materials[ materialIndex ].SetTextureOffset( textureName, uvOffset );
}
}
}
For changing the image, the most efficient way is to use a tiling texture, but if you must use different source images, you can use this:
http://unity3d.com/support/documentation/ScriptReference/Material-mainTexture.html
I.e.
renderer.material.mainTexture = myTexture;
Thanks. This is a lot easier than having to set all the vertex UVs separately!
The top link is no longer accessible. Here's a replacement: Animating Tiled Texture Wiki Documentation
Answer by zzeeshann · Jun 07, 2016 at 06:30 AM
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float scrollSpeed = 0.5F;
public Renderer rend;
void Start() {
rend = GetComponent<Renderer>();
}
void Update() {
float offset = Time.time * scrollSpeed;
rend.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
}
This code is in C#
Answer by 3D-Magic-VR · Sep 01, 2011 at 10:46 PM
Hi, if you want animate multiple materials check this, hopes help you.
Answer by Dayradon · May 18, 2017 at 11:09 AM
file missing: http://www.unifycommunity.com/wiki/index.php?title=Animating_Tiled_texture file missing: http://www.youtube.com/watch?v=uR5QVTEI8X
Answer by refardeon · May 05, 2019 at 04:47 PM
unity wiki link is now: http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture