unity3D中 material中tiling和offset属性解释
贴图有可能是多行多列的一些图案组成的。当我们需要一帧,一帧的播放时候。也就是帧序列动画,
我们就需要用到tiling和offset两个属性,
默认图片的左下角为坐标圆点即:(0,0)
tiling是图片的大小,offset是偏移量
来看看一些例子:
using UnityEngine;
using System.Collections; public class animspite : MonoBehaviour
{ public int totolFrame;//总帧数,即多少帧
public int fbs;//帧速度 即 1秒运行多少帧
public int rowNumber; //几行
public int colNumber; //几列
public bool isDes = false; //是否播放一次就销毁对象
// Use this for initialization
void Start()
{
//判断当前平台
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif #if UNITY_IPHONE
Debug.Log("Iphone");
#endif #if UNITY_STANDALONE_OSX
Debug.Log("Stand Alone OSX");
#endif #if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
#endif
} // Update is called once per frame
void Update()
{
int index = (int)(Time.time * fbs); index = index % totolFrame; float sizeX = 1.0f / colNumber;
float sizeY = 1.0f / rowNumber;
Vector2 size = new Vector2(sizeX, sizeY); float uIndex = index % colNumber;
float vIndex = index / colNumber;
//int vIndex = index % rowNumber; float offsetX = uIndex * size.x;
float offsetY = (1.0f - size.y) - (vIndex * size.y);
//offsetY = 1.0f * vIndex / rowNumber; Vector2 offset = new Vector2(offsetX, offsetY); transform.renderer.material.mainTextureScale = size;
transform.renderer.material.mainTextureOffset = offset; if (isDes)
{
if (Time.time > )
{
Destroy(this.gameObject);
}
}
}
}
//--------------更直观的分割线----------------------
int currentFame = (int)(Time.time * fbs); //算出当前帧
if (assing != 0) currentFame = assing;//当指定assing值时,即只播放指定帧的动画
Vector2 size = new Vector2(); //算出当前帧,图片的坐标
size.x = 1.0f / colNumber;
size.y = 1.0f / rowNumber; //算出当前帧,图片分别在x。y轴上的坐标
float xFrame = currentFame % colNumber;
float yFrmae = currentFame / colNumber; Vector2 offset = new Vector2(); //算出当前帧,图片的偏移量
offset.x = xFrame * size.x;
offset.y = 1.0f - size.y - (yFrmae * size.y); //赋值
transform.renderer.material.mainTextureScale = size;
transform.renderer.material.mainTextureOffset = offset;
/*参考自网络*/
public int rowCount = ; //图片的中帧图片的行数
public int colCount = ; //图片的中帧图片的列数
public int rowStart = ; //开始播放的行下标
public int colStart = ; //开始播放的列下标
public int totalCells = ; //帧图片的总数
public int fps = ; //播放速度
//Update
void Update()
{
SetSpriteAnimation(colCount, rowCount, rowStart, colStart, totalCells, fps);
} //SetSpriteAnimation
void SetSpriteAnimation(int colCount, int rowCount, int rowStart, int colStart, int totalCells, int fps)
{
//因为Time.time的为游戏运行的时间,所以index=1,2,3,4...
int index = (int)(Time.time * fps);
index = index % totalCells;
float sizeX = 1.0f / colCount;
float sizeY = 1.0f / rowCount;
Vector2 size = new Vector2(sizeX, sizeY);
var uIndex = index % colCount;
var vIndex = index / colCount;
float offsetX = (uIndex + colStart) * size.x;
//V轴位于图片的下方,因此V要翻转下
float offsetY = (1.0f - size.y) - (vIndex + rowStart) * size.y;
Vector2 offset = new Vector2(offsetX, offsetY);
renderer.material.SetTextureOffset("_MainTex", offset);
renderer.material.SetTextureScale("_MainTex", size);
}
unity3D中 material中tiling和offset属性解释的更多相关文章
- 图文详解Unity3D中Material的Tiling和Offset是怎么回事
图文详解Unity3D中Material的Tiling和Offset是怎么回事 Tiling和Offset概述 Tiling表示UV坐标的缩放倍数,Offset表示UV坐标的起始位置. 这样说当然是隔 ...
- TDD在Unity3D游戏项目开发中的实践
0x00 前言 关于TDD测试驱动开发的文章已经有很多了,但是在游戏开发尤其是使用Unity3D开发游戏时,却听不到特别多关于TDD的声音.那么本文就来简单聊一聊TDD如何在U3D项目中使用以及如何使 ...
- Thinking in Unity3D:渲染管线中的Rendering Path
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的 ...
- html5中新增的form表单属性
html5中新增两个表单属性,分别autocomplete和novalidate属性 1.autocomplete属性 该属性用于控制自动完成功能的开启和关闭.可以设置表单或者input元素,有两个属 ...
- 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。
找到该文件所在文件夹,右键属性,安全选项卡,添加-aspnet用户,并设置其权限为完全控制.如果还是不行,就添加一个Everyone用户并赋予完全控制权限windows server 2008中IIS ...
- javascript中元素的scrollLeft和scrollTop属性说明
再说意义之前,前说一下这两个属性的适用范围: 注意:这两个属性只能用于元素设置了overflow的css样式中.否者这两个属性没有任何意义.且overflow的值不能为visible,但可以为hidd ...
- css中的字体及文本相关属性
css中的字体及文本相关属性 1.字体相关属性 字体主要可以设置color.font-family.font-size.font-size-adjust.font-stretch.font-style ...
- C#winform中使用控件的Dock属性进行布局
想要实现如下布局,可以通过设置控件的Dock属性达到效果 1.拖放一个panel控件一个label控件(放在panel中)和一个treeView控件到TestForm中 2.设置panel的Dock属 ...
- Objective-C中变量采用@property的各个属性值的含义
我们在OC中定义变量,可以自己来定义变量的setter方法来设置变量值,用getter方法来获取变量值.但是当变量数量增多时,还采用手动添加setter/getter方法来操作变量,就会使得程序代码量 ...
随机推荐
- jQuery弹出层插件大全
1.thickbox 目前用的比较多的,最新版本是thickbox3.1 下载地址:http://jquery.com/demo/thickbox/#examples 2.colorBox 官方网站: ...
- Volley overview
https://developer.android.com/training/volley/ 需FQ https://developer.android.google.cn/training/vo ...
- source.android.google && developer.android.google
https://source.android.google.cn/ https://developer.android.google.cn/ https://source.android.com/co ...
- [原]git的使用(六)---远程仓库
10.远程仓库 -------------------------------------------------------------------------------------------- ...
- nginx 日志文件
默认日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status ...
- Amazon ec2 改成密码登录方式
sudo passwd rootsu rootvi /etc/ssh/sshd_config"# PasswordAuthentication yes" uncommentsbin ...
- WIN7开启wifi热点
1.首先,先确定自己的笔记本网卡支持“启动承载网络”的功能,使用管理员运行cmd命令.启用管理员运行CMD的方法Windows-所有程序-附件-运行(右键,以管理员身份运行)在弹出的CMD窗口里面敲击 ...
- js的mime类型有哪些?
js中的mime类型 常见类型 扩展名 类型/子类型 txt text/plain doc application/msword exe application/octet-stream pdf ap ...
- Elasticsearch 自定义映射
尽管在很多情况下基本域数据类型 已经够用,但你经常需要为单独域自定义映射 ,特别是字符串域.自定义映射允许你执行下面的操作: 全文字符串域和精确值字符串域的区别 使用特定语言分析器 优化域以适应部分匹 ...
- poj3347 Kadj Squares【计算几何】
Kadj Squares Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3594 Accepted: 1456 Desc ...