位置: 编程技术 - 正文

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:pg_lo_read_all()的用法_PostgreSQL函数
  • 应收账款挂账多年
  • php的array_multisort()使用方法介绍
  • 委托加工物资加工费
  • 解决方案啥意思
  • 详解16型人格
  • 转让企业要交什么税
  • cdr快捷键命令大全
  • nginx怎么运行php
  • 个体工商户季报还是月报
  • 进项加计抵减会计分录怎么做
  • 个体工商户如何注销网上
  • 公司给买员工保险
  • java初学者教程
  • 企业注销需要清产核资吗
  • 帐载金额
  • 购买无形资产的入账价值包括增值税吗
  • 商家下单
  • 餐费计入哪个科目合理避税
  • 长期待摊费用做账
  • 去银行提取备用金不是会计本人需要给谁打电话
  • 营改增后增值税增加了什么征收范围
  • 注销时,其他应付款有余额,怎么冲减
  • 将购进药品销售给药品生产企业或药品经营企业的是
  • 发票免税怎么做账
  • 退回备用金在现金怎么办
  • 商业企业注销应检查哪方面的问题
  • sql中nullif
  • centos怎么扩容
  • 如何使用光盘做启动盘
  • 电脑出windows
  • win7怎么不显示桌面图标
  • mac截图后怎么编辑
  • centos6.5 minimal
  • windows注册表简单应用
  • Win10 Mobile RS2预览版14926已知问题和解决方法汇总 谨慎升级
  • camrec是什么文件
  • linux 截屏
  • linux可视化界面怎么输入代码
  • windows 8怎么样
  • Linux VPN 出现 807 错误的解决办法
  • 十大经典排序算法总结
  • jquery-file-upload 文件上传带进度条效果
  • js的文件操作
  • linux创建用户的命令是什么
  • webgl fbo
  • jquery做下拉
  • python中判断语句怎么写
  • md5加密python
  • [置顶]马粥街残酷史
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设