x


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?

more ▼

asked Jun 17 '10 at 09:00 PM

Fishman92 gravatar image

Fishman92
2.6k 109 119 133

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
more ▼

answered Jun 17 '10 at 09:16 PM

Tetrad gravatar image

Tetrad
7.8k 27 40 94

awesome!!.......

Sep 02 '10 at 01:15 PM Bentford

Thanks. This is a lot easier than having to set all the vertex UVs separately!

Jun 06 '11 at 11:43 AM bernardfrancois

Great work....!! Worked for me too....

Jul 06 '12 at 04:33 AM hirenkacha

superb man you are awesome thanks for this solution (y)

Jan 26 '13 at 08:03 AM Sanky

Arent you worried about Vector2 overflow in uvOffset variable after time?

Dec 18 '13 at 09:07 AM DuckOfDoom
(comments are locked)
10|3000 characters needed characters left

Hi, if you want animate multiple materials check this, hopes help you.

more ▼

answered Sep 01 '11 at 10:46 PM

3D-Magic-VR gravatar image

3D-Magic-VR
56 13 15 16

(comments are locked)
10|3000 characters needed characters left

How can you control the offset with Camera.main.transform.eulerAngles.y ? I'd like to scroll the texture based on the camera rotation instead of time.

more ▼

answered Oct 10 '12 at 09:35 PM

Cheeeese gravatar image

Cheeeese
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3130
x319
x84

asked: Jun 17 '10 at 09:00 PM

Seen: 45465 times

Last Updated: Dec 18 '13 at 09:07 AM