查看: 1551|回复: 2
打印 上一主题 下一主题

[其他] Unity的访问其他游戏物体

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-11-29 05:11:08 |只看该作者 |倒序浏览

多数高级的游戏代码并不仅仅控制单独的游戏对象. Unity脚本有很多方法去查找和访问他们的游戏对象和组件.下面我们假设一个脚本OtherScript.js附于场景中的一个游戏对象上.

  1. using UnityEngine;
  2. using System.Collections;

  3. public class example : MonoBehaviour {
  4.     void Update() {
  5.         otherScript = GetComponent<OtherScript>();
  6.         otherScript.DoSomething();
  7.     }
  8. }
复制代码

1. 通过检视面板指定参数.

你能通过检视面板为一些对象类型设置变量值:

  1. using UnityEngine;
  2. using System.Collections;

  3. public class example : MonoBehaviour {
  4.     public Transform target;
  5.     void Update() {
  6.         target.Translate(0, 1, 0);
  7.     }
  8. }
复制代码

你也可以把参数显示在检视面板.随后你可以拖拽游戏对象OtherScript到检视面板中的target位置.

  1. using UnityEngine;
  2. using System.Collections;

  3. public class example : MonoBehaviour {
  4.     public OtherScript target;
  5.     void Update() {
  6.         target.foo = 2;
  7.         target.DoSomething("Hello");
  8.     }
  9. }
复制代码

2. 确定对象的层次关系

你能通过游戏对象的 Transform组件去找到它的子对象或父对象:

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4.     public void Awake() {
    5.         transform.Find("Hand").Translate(0, 1, 0);
    6.     }
    7. }
    复制代码

一旦你在层次视图找到transform,你便能用 GetComponent获取其他脚本.

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4.     public void Awake() {
    5.         transform.Find("Hand").GetComponent<OtherScript>().foo = 2;
    6.         transform.Find("Hand").GetComponent<OtherScript>().DoSomething("Hello");
    7.         transform.Find("Hand").rigidbody.AddForce(0, 10, 0);
    8.     }
    9. }
    复制代码

你能循环到所有的子对象:

  1. using UnityEngine;
  2. using System.Collections;

  3. public class example : MonoBehaviour {
  4.     public void Awake() {
  5.             foreach (Transform child in transform) {
  6.             child.Translate(0, 10, 0);
  7.         }
  8.     }
  9. }
复制代码

3.指定名字或标签.

你能用确定的标签搜索对象,使用 GameObject.FindWithTag和 GameObject.FindGameObjectsWithTag.使用 GameObject.Find通过名字获得游戏对象.

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4.     void Start() {
    5.         GameObject go = GameObject.Find("SomeGuy");
    6.         go.transform.Translate(0, 1, 0);
    7.         GameObject player = GameObject.FindWithTag("Player");
    8.         player.transform.Translate(0, 1, 0);
    9.     }
    10. }
    复制代码

你可以用GetComponent获得指定游戏对象上的任意脚本或组件.

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4.     void Start() {
    5.         GameObject go = GameObject.Find("SomeGuy");
    6.         go.GetComponent<OtherScript>().DoSomething();
    7.         GameObject player = GameObject.FindWithTag("Player");
    8.         player.GetComponent<OtherScript>().DoSomething();
    9.     }
    10. }
    复制代码

一些特殊对象,比如主摄像机,用快捷方式 Camera.main.


4. 传递参数

一些事件包含详细的消息信息.例如,触发事件传递碰撞对象的 Collider组件到处理函数.

OnTriggerStay给我们一个碰撞体参数.通过这个碰撞体我们能得到它的刚体.

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4.     void OnTriggerStay(Collider other) {
    5.         if (other.rigidbody)
    6.             other.rigidbody.AddForce(0, 2, 0);

    7.     }
    8. }
    复制代码

或者我们可以通过collider得到这个物体的任何组件.

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4. void OnTriggerStay(Collider other) {
    5.     if (other.GetComponent<OtherScript>())
    6.         other.GetComponent<OtherScript>().DoSomething();

    7.     }
    8. }
    复制代码

注意, 在上面的例子中使用后缀的方式访问其他变量.同样,你能访问到碰撞对象包含的任意组件。


5. 某个类型的脚本

找到某个类型的对象或脚本可以用 Object.FindObjectsOfType或获得某个类型的第一个对象使用 Object.FindObjectOfType.

    1. using UnityEngine;
    2. using System.Collections;

    3. public class example : MonoBehaviour {
    4.     void Start() {
    5.         OtherScript other = FindObjectOfType(typeof(OtherScript));
    6.         other.DoSomething();
    7.     }
    8. }
    复制代码


分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

0

主题

2

听众

1346

积分

助理设计师

Rank: 4

纳金币
505
精华
0
沙发
发表于 2014-11-29 14:39:19 |只看该作者
代码真实用 ! 长知识了 ! 谢谢指导 !
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-11-29 17:08:49 |只看该作者
good code
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 19:44 , Processed in 0.088761 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部