当前位置:网站首页>Unity脚本的基础语法(4)-访问其他游戏对象
Unity脚本的基础语法(4)-访问其他游戏对象
2022-06-30 12:18:00 【ht_game】
大部分脚本不只控制附加到其上的游戏对象,Unity脚本中很多方法访问其他的游戏对象和游戏组件。可以通过属性面板指定参数的方法来获取游戏对象,也可以通过Find方法来获取游戏对象。
通过属性面板指定参数
代码中声明public类型的游戏对象引用,在属性面板就会显示该游戏对象参数,然后就可以将需要获取的游戏对象拖拽到属性面板的相关参数位置。
public GameObject otherObject
void Update()
{
Test test=otherObject.GetComponent<Test>();//获取Test脚本组件
}
确定对象的层次关系
游戏对象在游戏组成对象列表中存在父子关系,在代码中可以通过获取Transform组件来找到子对象或者父对象
public class Find : MonoBehaviour {
void Update()
{
transform.Find("Son").Translate(0, 0, 1 * Time.deltaTime, Space.Self);//找到子对象“Son”,让其移动
transform.parent.Translate(0,0,1*Time.deltaTime,Space.Self);//找到父对象,让其移动
}
}
在获得子对象,就可以通过GetComponent方法获取子对象的其他组件,也可以直接调用GetComponentInChildren与GetComponentInParent方法获取父对象和子对象上的组件
例如
public class Find2 : MonoBehaviour {
void Update () {
transform.GetComponentInChildren<Text>().text = "123";
}
}
也可以使用脚本来循环获取到所有的子对象,然后对子对象做某种操作
public class Find2 : MonoBehaviour {
void Update()
{
foreach (Transform child in transform)//循环获取所有的子对象
{
child.GetComponent<Text>().text = "白色";
}
}
}
通过名字或标签获取游戏对象
Unity脚本中可以使用FindWithTag方法和Find方法来获取游戏对象,FindWithTag方法获取指定标签的游戏对象,Find方法获取指定名字的游戏对象。
GameObject name=GameObject.Find("Somename");
GameObject tag=GameObject.FindWithTag("Sometag");
通过传递参数来获取游戏对象
一些事件回调方法的参数包含特殊的游戏对象或组件信息,如触发碰撞事件的Collider组件。在OnTriggerStay方法的参数中有一个碰撞体参数,通过这个参数能得到碰撞的刚体.
void OnTriggerStay(Collider other)
{
if(other.GetComponent<Rigidbody>())//如果该游戏对象上有刚体组件
{
other.GetComponent<Rigidbody>().AddForcr(0,0,2);//给刚体施加一个力
}
}
通过组件名称获取游戏对象
Unity脚本可通过FindObjectsOfType方法和FindObjectOfType方法来找到挂载特定类型组件的游戏对象。FindObjectsOfType方法可以获取所有挂载指定类型组件的游戏对象
FindObjectOfType方法可以获取挂载指定类型组件的第一游戏对象
Test test=FindObjectOfType<Test>();//获取第一个找到的Test组件
Test[] tests=FindObjectsOfType<Test>();//获取所有的Test组件
边栏推荐
- 浅谈 JMeter 运行原理
- 90. (cesium chapter) cesium high level listening events
- Splitting e-commerce systems into micro services
- 【OpenGL】OpenGL Examples
- Analysis of smart jiangcai login in Jiangxi University of Finance and Economics
- kubeedge的核心理念
- MySQL built-in functions
- 【一天学awk】数组的使用
- Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
- JMeter's performance test process and performance test focus
猜你喜欢

Qt读写Excel--QXlsx工作表显示/隐藏状态设置4

【 surprise】 la vitesse de téléchargement de Thunderbolt n'est pas aussi rapide que celle de la machine virtuelle

7 lightweight and easy-to-use tools to relieve pressure and improve efficiency for developers, and help enterprises' agile cloud launch | wonderful review of techo day

Sarsa notes

Google refutes rumors and gives up tensorflow. It's still alive!

Hisilicon 3559 sample parsing: Venc

Introduction to new features of ES6
![[target tracking] |pytracking configuring win to compile prroi_ pool. pyd](/img/ac/1e443164e57c4f34ddd1078de9f0d2.png)
[target tracking] |pytracking configuring win to compile prroi_ pool. pyd

Hisilicon 3559 universal platform construction: introduction to YUV format

Android development interview real question advanced version (with answer analysis)
随机推荐
Hisilicon 3559 developing common sense reserves: a complete explanation of related terms
Introduction to sub source code updating: mid May: uniques NFT module and nomination pool
Idea has a new artifact, a set of code to adapt to multiple terminals!
黑马笔记---List系列集合与泛型
Google refutes rumors and gives up tensorflow. It's still alive!
Building of Hisilicon 3559 universal platform: obtaining the modified code of data frame
MySQL判断执行条件为NULL时,返回0,出错问题解决 Incorrect parameter count in the call to native function ‘ISNULL‘,
【一天学awk】基础中的基础
Q-learning notes
Commands for redis basic operations
Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
Hisilicon 3559 sample parsing: Venc
Pinda general permission system (day 7~day 8)
60 divine vs Code plug-ins!!
浅谈 JMeter 运行原理
QT implementation dynamic navigation bar
FlinkSQL自定义UDATF实现TopN
Analysis of the whole process of common tilt data processing in SuperMap idesktop
Four ways for flinksql to customize udtf
Introduction to the novelty of substrat source code: indexing of call calls and fully completing the materialization of storage layer