Material设置纹理偏移
function SetTextureOffset (propertyName : string, offset : Vector2) : void //根据属性名称propertyName设置材质的纹理偏移offset
propertyName有三种:
"_MainTex" : 主要的漫反射纹理;
"_BumpMap": 法线贴图;
"_Cube" : 反射cubemap(立方体贴图);
尚未学习shader,从字面感觉"_MainTex"表示主要的纹理图案,目前用了"_MainTex"可以使用,其它暂不知道如何使用,先挖坑@@!
使用"_MainTex"的SetTextureOffset()方法等同于设置mainTexture属性
unity官方案例
1 // Scroll main texture based on time 2 //根据时间滚动主纹理 3 4 var scrollSpeed : float = 0.5; 5 6 function Update () { 7 var offset : float = Time.time * scrollSpeed; 8 renderer.material.SetTextureOffset ("_MainTex", Vector2 (offset,0)); 9 }
Unity 5.X 3D游戏开发技术详解与典型案例(改)
1 using UnityEngine; 2 using System.Collections; 3 4 public class DoogHua : MonoBehaviour { 5 public GameObject adBan1; 6 public GameObject adBan2; 7 public int materialIndex = 0; 8 public Vector2 moveRate = new Vector2(0.2f, 0); //以0.2的速度水平移动 9 public bool useTime = true; //标志位,使用时间控制速度 10 public Vector2 moveTime = new Vector2(1, 1); 11 12 string textureName = "_MainTex"; //主要的漫反射纹理 13 Vector2 offset = Vector2.zero; 14 15 void Update() { 16 if (useTime) { 17 if (moveTime.x != 0) { 18 moveRate.x = 1f / moveTime.x; 19 } 20 else { 21 moveRate.x = 0; 22 } 23 if (moveTime.y != 0) { 24 moveRate.y = 1f / moveTime.y; 25 } 26 else { 27 moveRate.y = 0; 28 } 29 } 30 moveRate = Vector2.Min(moveRate, new Vector2(100, 100)); 31 offset += (moveRate * Time.deltaTime); 32 33 if (adBan1.GetComponent<Renderer>().enabled) { 34 //adBan1.GetComponent<Renderer>().materials[materialIndex].SetTextureOffset(textureName, offset); 35 adBan1.GetComponent<Renderer>().materials[materialIndex].mainTextureOffset = offset; //效果同上 36 } 37 if (adBan2.GetComponent<Renderer>().enabled) { 38 adBan2.GetComponent<Renderer>().materials[materialIndex].SetTextureOffset(textureName, offset); 39 } 40 } 41 }
效果如下: