• Unity3D 给模型偏移纹理


    给模型偏移纹理

    using UnityEngine;
    using System.Collections;
    
    [RequireComponent(typeof(Renderer))]
    public class ModelTextureAnimation : MonoBehaviour {
    
    	//材质索引
    	public int MaterialIndex;
    
    	//材质主纹理名字
    	public string TextureName = "_MainTex";
    
    	//行数
    	public int Column = 1;
    
    	//列数
    	public int Row = 1;
    
    	//每帧间隔
    	public float Inverval = 0.333f;
    
    	//当前帧索引
    	public int Index = 0;
    
    	private Renderer mRenderer;
    	private Material mMaterial;
    
    	// Use this for initialization
    	void Start () {
    		mRenderer = renderer;
    		mMaterial = mRenderer.sharedMaterials[MaterialIndex];
    
    		mMaterial.SetTextureScale(TextureName, new Vector2(1.0f / Column, 1.0f / Row));
    		
    		StartCoroutine(Animate());
    	}
    
    	IEnumerator Animate()
    	{
    		SetTextureOffset();
     		while(true)
    		{
    			yield return new WaitForSeconds(Inverval);
    
    			if (true == mRenderer.active)
    			{
    				Index++;
    				Index %= (Column * Row);
    				SetTextureOffset();
    			}
    		}
    	}
    
    
    	//根据帧数算出Offset
    	void SetTextureOffset()
    	{
    		float x = (1.0f / Column) * (Index % Column);
    		float y = (1.0f / Row) * (Index / Column);
    		mMaterial.SetTextureOffset(TextureName, new Vector2(x,y));
    	}
    
    	// Update is called once per frame
    #if UNITY_EDITOR
    	void Update () {
    		mMaterial.SetTextureScale(TextureName, new Vector2(1.0f / Column, 1.0f / Row));
    	}
    #endif
    }
    

      

  • 相关阅读:
    web访问权限实现方法-探面向对象的编码设计
    解析二进制反码算数求和
    可以把erp当做一个分支-找自己的方向
    电脑开机是怎样自动加载进程
    球管模型和Java
    发明和发现
    漂浮
    js中的一些循环
    ES5中新增的一些方法
    git的一些操作
  • 原文地址:https://www.cnblogs.com/mrblue/p/4597639.html
Copyright © 2020-2023  润新知