Unity3D中常用的一些函数

转载自:http://blog.csdn.net/janpylx/article/details/7770766

这里就有点杂了,因为很多东西需要配合着项目才好解释。所以以下为我对一些函数(独立出来了)的理解。嘿嘿,一个大合集哦,可能下周还要继续写。

transform.Translate--把对象从一个地方往给定的方向前进。

例如:

transform.Translate(<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Vector3-forward.html?from=Transform" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3.forward</a> * <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html?from=Transform" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Time.deltaTime</a>);<span style="white-space: pre;">		</span>// Vector3.forward相当于(0, 0, 1). Time.deltaTime见下面。

transform.position--获取对象的坐标(x, y, z)。

例如:

transform.position = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html" class="classlink" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3</a>(0, 0, 0);<span style="white-space: pre;">	</span>// 把对象移动到(0, 0, 0)。

gameObject.GetComponent--获得,怎么说呢,类似于返回一个指向gameObject(对象)的一个组件的指针吧。

例如:

curTransform = gameObject.GetComponent(<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Transform.html" class="classlink" style="color: rgb(20, 93, 123); text-decoration: none;">Transform</a>);<span style="white-space: pre;">	</span>// curTransform一个变量,后面的语句返回Transform这个组件。

GameObject.FindWithTag --根据gameObject(对象)的标签来寻找对象。如果找到的话,就返回一个指向该对象的指针。

例如:

<span class="hl-keyword">var</span> respawn = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">GameObject.FindWithTag</a> (<span class="hl-string">"Respawn"</span>);<span style="white-space: pre;">		</span>// var respawn是在定义一个新的变量respawn,后面的语句将返回一个指向标签为Raspawn的对象的指针。

GameObject.FindGameObjectsWithTag--根据gameObject(对象)的标签来寻找一组具有相同标签的对象。如果找到的话,就返回一个指向该对象数组的指针。

例如:

<span class="hl-keyword">var</span> respawns = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">GameObject.FindGameObjectsWithTag</a> (<span class="hl-string">"Respawn"</span>);<span style="white-space: pre;">		</span><span style="font-size: 8pt;">// var respawn是在定义一个新的变量respawn,</span><span style="font-size: 8pt;">后面的语句将返回一个指向标签为Raspawn的一组对象的指针。</span>

Input.GetKeyDownInput.GetButtonDown --这两者类似,当用户按下某个特定的键后,触发事件。

例如:

<span class="hl-keyword">function</span> <span class="hl-operator">Update</span> () {
    <span class="hl-keyword">if</span> (<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Input.GetKeyDown</a> (<span class="hl-string">"space"</span>))
        print (<span class="hl-string">"space key was pressed"</span>);
}<span style="white-space: pre;">	</span>// 如果用户按下space键(空格键),则打印<span style="font-size: 8pt;">space key was pressed。<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html" target="_blank" style="color: rgb(51, 102, 153); text-decoration: none;">Input.GetButtonDown</a>类似。</span>

Input.GetAxisInput.GetAxisRaw --以名字来辨别虚拟键盘上的标识符。两者功能类似。后者限制更少。

例如:

<span class="hl-keyword">var</span> speed : <span class="hl-datatype">float</span> = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxisRaw.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Input.GetAxisRaw</a>(<span class="hl-string">"Horizontal"</span>) * <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html?from=Input" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Time.deltaTime</a>;<span style="white-space: pre;">	</span>// 接收左右或者A、D两个键的输入,控制水平方向的移动。
Time.time   --从游戏开始算起的总时间。

Time.deltaTime --每一秒的最后一帧所用的时间。

Object.Instantiate --克隆对象。

例如:

Instantiate (prefab, <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html" class="classlink" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3</a>(i * 2.0, 0, 0), <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-identity.html?from=Object" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Quaternion.identity</a>);<span style="white-space: pre;">	</span>// prefab是被克隆的对象, 第二个变量是克隆体生成的位置,第三个变量表示该生成的克隆不旋转。

function OnCollisionEnter(collision : Collision) --检测碰撞,且碰撞的双方有一方为刚体。例子见官方文档。

function OnTriggerEnter (other : Collider)  --检测碰撞,且碰撞的双方有一方为刚体(另外,被装的一方还要在开启is trigger属性)。例子见官方文档。

Destroy --摧毁对象。

例如:

Destroy (gameObject, 5);<span style="white-space: pre;">	</span>// 5秒后销毁gameObject。
Vector3   --代表一个三维坐标。

Random.Range --在一定范围内获得随机数。

例如:

 <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Random.Range</a>(-10.0, 10.0)<span style="white-space: pre;">	</span>// 获得-10.0到10.0的随机数。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值