Unity访问其他游戏对象
大部分脚本不单单控制其附加到的游戏对象。Unity脚本中有很多方法访问到其他游戏对象和游戏组件,
可以通过属性查看器指定参数的方法获取游戏对象,也可以通过 Find() 方法,获取游戏对象。
1、通过属性查看器指定参数
代码中声明public类型的游戏对象引用,在属性查看器中就会显示这个游戏对象参数,然后就可以将需要获取的游戏对象拖拽到属性查看器的相关参数位置,
具体操作时可以使用如下的C#代码,代码获取游戏对象上的 “Test” 脚本组件,然后执行doSomething方法。
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject otherObject; void Update() { Test test = otherObject.GetComponent<Test>(); test.doSomething(); } }
2、确认对象的层次关系
游戏对象在游戏组成对象列表存在父子关系,在代码中可以通过获取Transform组件来找到子对象或者父对象。具体操作时
可以使用如下代码获取游戏对象的子对象和父对象:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject otherObject; void Update() { //找到"hand"子对象并将其沿z轴每帧移动1个单位 transform .Find("hand").Translate(0,0,1); //找到其父对象并将其沿z轴每帧移动1个单位 transform.parent.Translate(0,0,1); } }
一旦成功获取到了"hand"子对象,也可以通过GetComponent方法获取“hand”对象的其他组件。
例如:有一个Test脚本挂载在子对象“hand”上
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject otherObject; void Update() { //找到子对象"hand",同时设置"Test"脚本中的变量a为2; transform .Find("hand").GetComponent<Test>().a=2; //执行doSomething() 方法 transform.Find("hand").GetComponent<Test>().doSomething(); //为hand子对象的刚体属性上加一个沿z轴的大小为2的力 transform.Find("hand").GetComponent<Rigibody>().AddForce(0,0,2); } }
也可以使用脚本来循环获取到所有的子对象,然后对子对象做某种操作,如平移、旋转等:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject otherObject; void Update() { //循环所有的子对象 foreach(Transform child in transform){ //沿y轴每帧移动5个单位 child.Translate(0,5,0); } } }
3、通过名字或者标签获取游戏对象
Unity 脚本中可以使用 FindWithTag 方法和 Find 方法来获取游戏对象,
FindWithTag 方法是获取指定标签的游戏对象
Find 方法是获取指定名字的游戏对象
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject otherObject; void Update() { //获取名称为“somename”的游戏对象 GameObject name = GameObject.Find("somename"); //沿z轴平移 name.transform.Translate(0,0,1); //获取标签为“sometag”的游戏对象 GameObject tag = GameObject.FindWithTag("sometag"); //沿z轴平移 tag.transform.Translate(0,0,1); } }
这样,通过GetComponent 方法就能得到指定游戏对象上的任意脚本或组件:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject otherObject; void Update() { //获取名称为“somename”的游戏对象 GameObject name = GameObject.Find("somename"); //调用Test脚本中的deSomething方法 name.GetComponent<Test>().doSomething(); //获取标签为“sometag”的游戏对象 GameObject tag = GameObject.FindWithTag("sometag"); //调用Test脚本中的doSomething方法 tag.GetComponent<Test>().doSomething(); } }
4、通过传递参数获取游戏对象
一些事件回调方法的参数中包含了特殊的游戏对象或组件信息,例如触发碰撞事件的Collider组件。
在OnTiggerStay 方法的参数中有一个碰撞体参数,通过这个参数能得到碰撞的刚体:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { //重写OnTriggerStay方法 void OnTriggerStay(Collider other) { //如果该游戏对象上有刚体组件 if(other.GetComponent<Rigidbody>()) { //给刚体施加一个力 other.GetComponent<Rigibody>().AddForce(0,0,2); } } }
或通过Collider组件得到这个游戏对象上挂载的“Test”脚本:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { //重写OnTriggerStay方法 void OnTriggerStay(Collider other) { //如果该游戏对象上有“Test”脚本组件 if(other.GetComponent<Test>()) { //调用脚本的doSomething方法 other.GetComponent<Test>().doSomething(); } } }
5、通过组件名获取游戏对象
Unity 脚本中可以通过FindObjectsOfType 方法和 FindObjectOfType 方法来获取指定类型的游戏对象,
而FindObjectOfType 方法获取挂载指定类型组件的第一个游戏对象:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { void Start() { //获取第一个找到的“Test”组件 Test test = FindObjectOfType<Test>(); //打印挂载“Test”组件的第一个游戏对象的名称 Debug.Log(test.gameObject.name); //获取所有的“Test”组件 Test[] test = FindObjectsOfType<Test>(); foreach(Test te in tests) { //打印挂载“Test”组件所有的游戏对象和名称 Debug.Log(te.gameObject.name); } } }