位置: 编程技术 - 正文

Unity API 笔记(unity常用api)

编辑:rootadmin

推荐整理分享Unity API 笔记(unity常用api),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:unity bip,unity官方api,unity bip,unity常用api,unity bip,unity bip,unity.api,unity api中文手册,内容如对您有帮助,希望把文章链接给更多的朋友!

#1 Rotate Translate函数只是一次改变旋转和位置 是因为放在了Update函数里才使其有了平滑的连续的动画效果

如果把Rotate函数放在Start()函数里 旋转&#;会立刻改变 没有过渡。

#2

插&#;类函数 (Interpolating Functions)

Mathf.Lerp(a,b,t)

当t<0 时 t按0计算 当t>1时 t按1计算 当t属于[0,1]时 函数的返回&#;为a&#;(b-a)*t 连续的Update调用使其产生平滑过渡的效果。

类&#;的还有Vector3.Lerp Color.Lerp Quaternion.Lerp

有个类&#;但又不同的函数 Mathf.MoveTowards

static float MoveTowards(float current, float target, float maxDelta);

是以maxDelta为增量进行&#;的改变 当maxDelta为负时 current将远离target

static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

按直线把一个点从current移动到target 返回&#;是从current到target直线上离current距离为maxDistanceDelta的点的坐标

如果两点的连线距离不足maxDistanceDelta,则直接到达target 也就是说返回&#;永远不会超过target. 如果是maxDistanceDelta是负&#; 则current点将按直线远离target.

如print(Vector3.MoveTowards(new Vector3(0,0,0),new Vector3(3,4,0),1)); 结果是 (0.6,0.8,0.0)

同样有个类&#;的函数Mathf.MoveTowardsAngle

static float MoveTowardsAngle(float current, float target, float maxDelta);

和MoveToward类&#; 但是确保当超过度时按照角度的方式计算 由于优化原因 不支持负的maxDelta

current和target代表的角都是以角度为单位的、所以要注意:旋转角度之间的插&#;应该用MoveTowardsAngle 类&#;的函数还有Mathf.LerpAngle

球形插&#;Vector3.Slerp与线性插&#;Lerp不同之处在于将传入的向量当做方向处理 而非原来当做点来处理 且它的模也会被插&#;处理。

反过来计算需要多少插&#;参数才能从current到target的函数 Mathf.InverseLerp(from,to,value)

如 print(Mathf.InverseLerp(5,,8)); 结果为(8-5)/(-5)=0.6

Summary:

Lerp类函数:

Mathf.Lerp()

Vector3.Lerp()

Color.Lerp()

Quaternion.Lerp()

对应的角度插&#;函数为

Mathf.LerpAngle()

Slerp类函数

Vector3.Slerp()

MoveTowards类函数

Mathf.MoveTowards()

Vector3.MoveTowards()

对应的角度插&#;函数为

Mathf.MoveTowardsAngle()

反插&#;函数

Mathf.InverseLerp()

夹&#;函数

Mathf.Clamp

static float Clamp(float value, float min, float max);

static int Clamp(int value, int min, int max);

将value夹在min和max之间 如果value < min 则 返回min 如果value > max 则返回max 不改变value的&#;

static float Mathf.PingPong(float t,float length)

返回&#;将在[0,length]之间有序来回震荡 注意要放在Update()函数里面

让物体的x坐标在[0,3]来回震荡。

Transform.Translate()

重载1:

void Translate(Vector3 translation, Space relativeTo = Space.Self);

移动位移translation 默认是自身坐标系

相对:

coordinate(坐标)

Space.Self 自身坐标系

Space.World 世界坐标系

重载2:

void Translate(float x, float y, float z, Space relativeTo = Space.Self);

沿x轴位移x 沿y轴位移y 沿z轴位移z

重载3:

void Translate(Vector3 translation, Transform relativeTo);

位移向量translation 以relativeTo的局部坐标为参照 如果relativeTo为null 则以世界坐标为参照系。

重载4:

void Translate(float x, float y, float z, Transform relativeTo);

Transform.Rotate()

重载1:void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);

按照欧拉角旋转物体(z,x,y) 注意顺序(旋转顺序不同最终旋转效果也可能不同) 单位是角度

类&#;于Translate 默认是物体自身的局部坐标系

注意:以世界坐标旋转并不是绕着世界坐标轴旋转 而只是按照世界坐标系的方向 以自身枢轴点旋转。

重载2:void Rotate(float xAngle, float yAngle, float zAngle, SpacerelativeTo = Space.Self);

绕z轴z度 绕x轴x度 绕y轴y度 注意顺序

重载3:void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);

绕axis向量旋转angle度 默认axis向量的坐标系是物体自身的局部坐标系

Unity中采用左手坐标系

Vector3类

普通成员变量:

x y z 分量

this[index] (0:x 1:y 2:z)

eg:

只读:

magnitude 模

sqrMagnitude 模的平方(效率)

normalized 标准化向量

静态成员变量:

zero (0,0,0)

one (1,1,1)

缩写(shorthand)静态数据成员不用new

static Vector3 right;:Shorthand for writing Vector3(1, 0, 0). x轴

static Vector3 up; : Shorthand for writing Vector3(0, 1, 0).y轴

static Vector3 forward;:Shorthand for writing Vector3(0, 0, 1). z轴

对应的有left(-1,0,0)down(0,-1,0)back(0,0,-1)

构造函数

重载1 Vector3(float x,float y ,float z)

重载2 Vector3(float x,float y)

sets z to zero

普通成员函数:

void Set(float new_x, float new_y, float new_z);

重新为一个已存在的向量设置x y z分量

string ToString();string ToString(string format);Unity API 笔记(unity常用api)

静态函数:

static float Angle(Vector3 from, Vector3 to); 返回from和to的夹角 (单位为度 不大于度)

static Vector3 ClampMagnitude(Vector3 vector, float maxLength); 返回一个向量的拷贝 其模长不超过maxLength

static Vector3 Cross(Vector3 lhs, Vector3 rhs); 叉乘

static float Distance(Vector3 a, Vector3 b); 返回 a b 两点的距离 与(a-b).magnitude 作用相同

static float Dot(Vector3 lhs, Vector3 rhs); 点乘

static Vector3 Lerp(Vector3 from, Vector3 to, float t); 线性插&#;

static Vector3 Max(Vector3 lhs, Vector3 rhs); 返回模较大的向量

static Vector3 Min(Vector3 lhs, Vector3 rhs); 返回模较小的向量

static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta); 线性步移向量void Normalize(); 规范化(改变原始向量) 如 Vector3 p = Vector3.one; p.Normalize(); 如果不想改变原始向量 可以使用normalized数据成员或者Vector3.Normalize(p) 函数如果向量太小如(1e-8,0,0) 它将会被置为零向量static void OrthoNormalize(Vector3 normal, Vector3 tangent); 规范正交化两个向量static void OrthoNormalize(Vector3 normal, Vector3 tangent,Vector3 binormal); normal 是 tangent 和binormal的法线 tangent和binormal也规范正交 即 规范正交化三个向量 (在转换坐标系进行模型缩放时有用)static Vector3 Project(Vector3 vector, Vector3 onNormal); 返回vector在OnNormal上的投影向量 static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal); 返回以inNormal为法线的平面上 inDirection的反射向量 (模相等)static Vector3 RotateTowards(Vector3 current, Vector3 target, floatmaxRadiansDelta, float maxMagnitudeDelta);以maxRadiansDelta(单位是弧度)旋转 以maxMagnitudeDelta改变模长 将current 逐渐变到 target 这里是将current和target当做向量而非点如 旋转到目标位置

static Vector3 Scale(Vector3 a, Vector3 b);

a和b的各分量对应相乘得到一个新的向量

输出 (2.0,6.0,.0) 用于各个方向的缩放操作

static Vector3 Slerp(Vector3 from, Vector3 to, float t);

模拟日出日落效果

static Vector3 SmoothDamp(Vector3 current, Vector3 target, Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, floatdeltaTime = Time.deltaTime);

弹簧效果的阻&#; 先快后慢 速度是ref

smoothtime 从current到达target大约需要耗费的时间

deltaTime 上一次函数调用至此次调用间隔的时间间隔

The most common use is for smoothing a follow camera.

运算:

- :向量减法 反向量

!=:Very close vectors are treated as being equal.

*:向量的数乘 注意只定义了Vector3与float的乘法 double没有

/:static Vector3 operator /(Vector3 a, float d); 向量的数除

&#;:向量的加法 对应分量相加

==:This will also return true for vectors that are really close to being equal.

由此可见 Unity中的Vector3的==已经考虑了浮点比较误差 因此我们不需要像C语言那样通过epsilon的约束来比较两个浮点数

Quaternion类

用来表示旋转 没有欧拉角的Gimbal Lock(万向节死锁)问题 易插&#;计算 Unity内部旋转处理方式

四元数:

1概念 Q=[w,(x,y,z)] 一个四元数包含一个标量分量和一个三维向量分量

在3D数学中用单位四元数来表示旋转 对于旋转轴为n=(nx,ny,nz) 旋转角度为α的旋转 用四元数表示:

w=cos(α/2)

x=sin(α/2)Nx

y=sin(α/2)Ny

z=sin(α/2)Nz

简记为 Q=[cos(α/2),sin(α/2)n]

一般为了计算方便 要求n为单位矢量

静态成员变量:

identity:无旋转 (Read Only) 实验得&#;为(0,0,0,1) (x,y,z,w)

普通成员变量:

Vector3 eulerAngles 返回对应的欧拉角 旋转顺序为 z x y 即物体在Inspector显示的Rotation的转换成--范围后的结果

this[int] :获取分量 x y z w 对应 0 1 2 3

x y z w :float

构造函数

Quaternion(x,y,z,w)

成员函数

void Set(float new_x, float new_y, float new_z, float new_w);

为一个已经存在的四元数设置x,y,z,w;

void SetFromToRotation(Vector3 fromDirection, Vector3toDirection);

将当前四元数设置为一个从from旋转到to方向的旋转

如:

public Quaternion rotation = Quaternion.identity; void Start() { rotation.SetFromToRotation(new Vector3(0, 1, 0), new Vector3(0, 0, 1)); print(rotation.eulerAngles); }

应该绕x轴旋转度 所以输出(.0,0.0,0.0) 旋转轴是(1,0,0) 所以rotation被设置成(0.(cos(/2)),0,0,0.) 符合前面四元数的概念定义。

void SetLookRotation(Vector3 view, Vector3 up = Vector3.up);

设置朝向一个位置的旋转 即令物体的z轴对准view物体 x轴与up垂直 这个函数一般用于令物体面对另一物体

是 零向量 将抛出一个提示信息

up可以是零向量 表示对x轴的朝向没有要求

void ToAngleAxis(float angle, Vector3 axis);

把一个四元数表示的旋转转换成角-轴表示的形式

out参数 注意是ToAngleAxis 而非ToAxisAngle 后者已经过时

string ToString();string ToString(string format);

静态成员函数

static float Angle(Quaternion a, Quaternion b);

返回两个旋转z轴的夹角 单位是度

print(Quaternion.Angle(new Quaternion(0, 0, 0, 1), new Quaternion(0,Mathf.Sqrt(2)/2,0,Mathf.Sqrt(2)/2))); 输出为

static Quaternion AngleAxis(float angle, Vector3 axis);

返回一个绕axis轴旋转angle度的等价的Quaternion旋转

如 print(Quaternion.AngleAxis(, Vector3.up)); 输出(0.0,0.7,0.0,0.7)

static float Dot(Quaternion a, Quaternion b);

static Quaternion Euler(float x, float y, float z);

static Quaternion Euler(Vector3 euler);

由一个绕z轴z度 x轴x度 y轴y度得到的旋转转换成Quaternion

static Quaternion FromToRotation(Vector3 fromDirection, Vector3toDirection);

类&#;SetFromToRotation

static Quaternion Inverse(Quaternion rotation);

返回一个旋转的逆

static Quaternion Lerp(Quaternion from, Quaternion to, float t); 从from到to之间用t插&#;然后将结果标准化,该函数比Slerp处理速度更快 但是当from和to相隔较远时效果较差。

static Quaternion LookRotation(Vector3 forward, Vector3 upwards= Vector3.up);

类&#;SetLookRotation函数

static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta);

将旋转from旋转到to

通过一个角位移maxDegreesDelta来进行旋转 注意不会越界 单位是角度

如果角位移是负的 那么from将远离to 直到to的反向为止(为止的意思是停止运动 实验得到会在附近有轻微的震荡)

static Quaternion Slerp(Quaternion from, Quaternion to, float t);

球形差&#;

eg

运算:

!= :该函数通过判断两个Quaternion的Dot Product是否小于1.0来运作。

注意:因为quaternion能够表示两圈内的旋转() 所以即使两者的旋转效果看起来不一样 比较结果也可能返回true.

*:结合两个旋转 a*b 效果是 先旋转a 后旋转b (!= b * a)不满足交换律 类&#;于矩阵的乘法效果

==:注意:因为quaternion能够表示两圈内的旋转() 所以即使两者的旋转效果看起来一样 比较结果也可能返回false 比如旋转 度和旋转度

cos5° 不等于 cos° Dot == 1.0 则两者相等

.

unity学习之NGUI做NPC对话 首先今天我主要做了一下,用NGUI做的NPC对话,效果有点不好看,就凑合看吧。我首先用NGUI打了一个界面,然后为sprite添加了button事件,然后接下来就是

UGUI 加载图片 图片是动态加载的,然后转换为sprite赋到ugui的按钮上该文章出自【狗刨学习网】代码如下:usingUnityEngine;usingSystem.Collections;usingSystem.IO;usingUnityEngine.UI;pub

Unity3D深入浅出 - Shader基础开发 Mecanim概述:Mecanim是Unity提供第一个丰富而复杂的动画系统,提供了:针对人形角色的简易的工作流和动画创建能力Retargeting(运动重定向)功能,即把动画

标签: unity常用api

本文链接地址:https://www.jiuchutong.com/biancheng/380213.html 转载请保留说明!

上一篇:Unity3d 在 iOS 上推送( push notification ) 编写(unity3d如何发布ios教程)

下一篇:unity学习之NGUI做NPC对话(unity做app)

  • 合理避税与偷税漏税的行为区别
  • 白条技巧
  • 个人所得税当月计提下月申报
  • 申报个人所得税是按应发工资还是实发工资
  • 免征增值税发票税率
  • 固定资产折旧财务
  • 结转本月收入类账户到本年利润会计分录
  • 隔月发票可以作废吗?
  • 免抵退税额账务处理流程
  • 小规模纳税人开专票
  • 企业如果亏损应该计提缴纳所得税吗
  • 我的初级备考日记--你都没坚持,还谈什么未来
  • 长期股权投资用交印花税吗
  • 安置房视同销售需要缴纳所得税吗?
  • 固定资产报废处理流程图
  • 公账提现到个人账户多久到账
  • 资产负债表在建工程怎么填列
  • 库存商品换货的会计分录
  • 政府奖励资金如何入账
  • 其他业务成本借贷方向表示
  • 什么是应付工资金额
  • 存货跌价准备是谁的备抵账户
  • 发放福利视同销售进项税要转出吗?
  • 公司购买理财产品账务处理
  • 如何给电脑重装系统教程
  • 怎么确认旧城改造完成
  • php扩展ffmpeg教程
  • php和javaweb
  • php加密文件解密
  • 准予在企业所得税税前扣除的有
  • 定额发票过期时间是多久
  • vue 百度地图 移动端
  • php正则表达式验证网址
  • 卡拉公路
  • vue中的proxy代理
  • 所得税减免要做账吗
  • chkconfig命令参数
  • 远程调试时,gdbserver运行在调试机
  • 华为mate主题
  • 中小型科技企业所面临的普遍问题是资金缺乏
  • 社会团体收取的会费是否缴纳企业所得税
  • 类型string
  • 织梦如何给栏目增加缩略图
  • 销项负数会计分录怎么写
  • mssql备份数据库
  • 企业当期营业收入的计算
  • 应付职工薪酬包含哪些科目
  • 促销费会计分录怎么写
  • 公司租赁职工车辆账务处理
  • 公司抽奖奖品设置
  • 挂靠单位账务处理是?
  • 税控设备技术维护费
  • 小型微利企业年报填什么表
  • 所得税费用为什么不计入营业利润
  • 财务费用利息收入怎么记账
  • 税控盘服务费抵扣
  • 货拉拉平台运费没有结打不通电话怎么办
  • 如何冲减费用做账
  • 法院主要业务活动
  • 利润表中公允价值变动为贷
  • 管理费用的明细科目如何设置
  • bootcamp您的磁盘未能分区
  • centos云服务器
  • centos删除vg
  • windows8装 .NET 3.5 时出现0x800F0907错误解决方法
  • magento开发教程
  • kmsss.exe是什么
  • 项目部不足和改进
  • 原生js制作日历软件
  • ftp自动上传脚本怎么用
  • linux常用shell命令
  • javascript字符串操作函数
  • JavaScript事件 "事件对象"的注意要点
  • python自动化运维教程
  • 安卓启动器修改
  • 2020年上海税务跨区迁移很麻烦吗
  • 安徽残疾人补助过年有多少钱
  • 税务核查是什么意思
  • 签订设计合同
  • 税务师 领证
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设