Unity查找物体的四大主流方法及区别

GameObject.Find()
优点:

使用简单方便
不会因为重名而报错,同时查找的是自上而下的第一个物体
缺点

不能查找被隐藏的物体,否则出现“空引用异常”,这是很多新人在查找出现空引用bug的原因。
全局查找(遍历查找),查找效率低,很消耗性能。
代码演示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameObjectFind : MonoBehaviour {

private GameObject thing;

void Start () {

thing = GameObject.Find("C4");
thing.name = "thing";

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Transform.Find(),通过Transform组件查找子物体。
用这个方法查找物体时,根节点一定要处于“显示”状态,不能被隐藏。
用它查找孙物体及孙孙物体,一定要使用“绝对路径”,否则出现“空引用异常”。
代码演示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformFind : MonoBehaviour {

private Transform m_Transform;
private GameObject one;
private GameObject two;

void Start () {

m_Transform = gameObject.GetComponent<Transform>();
one = m_Transform.Find("D2").gameObject;
two = m_Transform.Find("D2/D3").gameObject;

Debug.Log(one.name);
Debug.Log(two.name);

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
GameObject.FindGameObjectWithTag()和GameObject.FindGameObjectsWithTag(),通过Tag标签查找物体。
GameObject.FindGameObjectsWithTag():通过Tag标签查找到一组物体,返回一个数组。
GameObject.FindGameObjectWithTag():查找到这类tag标签,自上而下第一个物体。

代码演示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TagFind : MonoBehaviour {

private GameObject thing;
private GameObject[] things;
void Start () {

things = GameObject.FindGameObjectsWithTag("Player");
thing = GameObject.FindGameObjectWithTag("Player");

Debug.Log(things.Length);
Debug.Log(thing.name);

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FindObjectsOfType()
FindObjectsOfTypeAll():返回指定类型的对象列表。

代码演示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FindObjectOfType : MonoBehaviour {

private GameObject[] things;
private GameObject thing;

void Start () {

things = FindObjectsOfType<GameObject>();
thing = FindObjectOfType<GameObject>();

Debug.Log("第一个" + thing.name);
for(int i = 0; i < things.Length; i++)
{
Debug.Log(things[i].name);
}
}
}
---------------------

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

javaScript. Dom 基本操作

DOM节点查找jsdocument.getElementById()//通过id查找document.getElementsByTagName()//通过标签名document.getElementsByName()//通过name名查找document.getElementsByClassName("类名")//通过类名获取元素对象documen

linux find 命令查找文件和文件夹

查找目录:find/(查找范围)name'查找关键字'typed查找文件:find/(查找范围)name查找关键字print详解:find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。