how do i get a set texture to scroll from left to right, and or change a materials image over time?
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;
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#
For URP materials
using UnityEngine;
// animate a texture
public class AnimateMaterial : MonoBehaviour
{
public float rateX;
public float rateY;
private Renderer r;
private float offsetX;
private float offsetY;
private void Start() {
r=GetComponent<Renderer>();
}
void Update()
{
offsetX += rateX * Time.deltaTime;
offsetY += rateY * Time.deltaTime;
r.materials[0].SetTextureOffset( "_BaseMap", new Vector2(offsetX, offsetY) );
}
}
Hi, if you want animate multiple materials check this, hopes help you.
file missing: unifycommunity.com
file missing: - YouTube
unity wiki link is now: http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture