【Unity Shader学习】UV动画变色效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetTextureUVST : MonoBehaviour {
    // 属性
    public int width;
    public int height;
    public int fps;
    private int currentIndex;

    IEnumerator Start()
    {
        // 获取组件中的材质
        Material mat = GetComponent<Renderer>().material;

        float scale_x = 1.0f / width;
        float scale_y = 1.0f / height;

        while (true)
        {
            float offset_x = currentIndex % width * scale_x;
            float offset_y = currentIndex / width * scale_y;

            // 设置纹理UV坐标的缩放比例和偏移量
            mat.SetTextureScale("_MainTex",new Vector2(scale_x, scale_y));
            mat.SetTextureOffset("_MainTex",new Vector2(offset_x,offset_y));
            // 设置等待时间
            yield return new WaitForSeconds(1.0f/fps);

            // 改变索引
            currentIndex = (++currentIndex) % (width * height);
        }
    }

	
	// Update is called once per frame
	void Update () {
		
	}
}

效果截图:

posted @ 2022-07-23 16:39  香菇0_0  阅读(178)  评论(0)    收藏  举报