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 (http://www.my516.com) {
things = FindObjectsOfType<GameObject>();
thing = FindObjectOfType<GameObject>();
Debug.Log("第一个" + thing.name);
for(int i = 0; i < things.Length; i++)
{
Debug.Log(things[i].name);
}
}
}
---------------------
Unity查找物体的四大主流方法及区别的更多相关文章
- Unity查找物体的子物体、孙物体
Unity查找物体下的所有物体 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- Unity查找子物体的方式-怎么查找GameObject
Unity动态查找物体 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创 ...
- Opencv距离变换distanceTransform应用——细化字符轮廓&&查找物体质心
Opencv中distanceTransform方法用于计算图像中每一个非零点距离离自己最近的零点的距离,distanceTransform的第二个Mat矩阵参数dst保存了每一个点与最近的零点的距离 ...
- Python 四大主流 Web 编程框架
Python 四大主流 Web 编程框架 目前Python的网络编程框架已经多达几十个,逐个学习它们显然不现实.但这些框架在系统架构和运行环境中有很多共通之处,本文带领读者学习基于Python网络框架 ...
- Unity 实现物体破碎效果(转)
感谢网友分享,原文地址(How to Make an Object Shatter Into Smaller Fragments in Unity),中文翻译地址(Unity实现物体破碎效果) In ...
- opencv2对读书笔记——使用均值漂移算法查找物体
一些小概念 1.反投影直方图的结果是一个概率映射,体现了已知图像内容出如今图像中特定位置的概率. 2.概率映射能够找到最初的位置,从最初的位置開始而且迭代移动,便能够找到精确的位置,这就是均值漂移算法 ...
- Unity 查找泛型List中的相同与不同数据
Unity查找泛型集合中的不同数据 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- Unity中让Update中的方法执行一次
Unity中让Update中的方法执行一次 Unity中,很多时候,代码需要放在Update中时刻监测状态,一旦状态符合,又只需要代码执行一次:其实可以通过设置控制量的方式,让代码只执行一次:方法:设 ...
- View inflate方法和LayoutInflater inflate方法的区别详解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...
随机推荐
- 【Codeforces 632D】 Longest Subsequence
[题目链接] 点击打开链接 [算法] 设取的所有数都是k的约数,则这些数的lcm必然不大于k. 对于[1, m]中的每个数,统计a中有多少个数是它的约数即可. [代码] #include<bit ...
- python+selenium高亮显示正在操作的页面元素
原文地址:https://blog.csdn.net/wxstar8/article/details/80801405 from selenium import webdriver import un ...
- atof和atoi
atof:将字串转换成浮点型数 表头文件 #include <stdlib.h> 函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而 ...
- Ruby module ---模块,组件
module 的主要目的是把不同的方法和常量分别放进不同的命名空间. module 的命名方式跟类一样首字母大写,多个单词不用下划线. 如:CircleArea module 语法 module Mo ...
- Start Developing Mac Apps -- Human Interface Design 用户界面设计
Human Interface Design It’s not enough to create an app that works. Users expect Mac apps to be powe ...
- E20180424-hm
thumb n. 拇指; (手套的) 拇指部份; trigger vt. 引发,触发; 扣…的扳机; 发射或使爆炸(武器或爆炸性弹药); n. (枪) 扳机; 起动装置,扳柄; 引发其他事 ...
- E20170426-gg
recursive adj. 回归的,递归的; removal n. 除去; 搬迁; 免职; 移走; customize vt. 定制,定做; 按规格改制;
- CSA Round #50 (Div. 2 only) Min Swaps(模拟)
传送门 题意 给出一个排列,定义\(value为\sum_{i=1}^{n-1}abs(f[i+1]-f[i])\) \(swap(a[i],a[j])(i≠j)为一次交换\),询问最少的交换次数使得 ...
- hdoj3790 【最短路】
这一题啊,其实还是很简单的~(A掉了就很简单啊~) 思路: 松弛,然后在里面维护一个小最短路~: A掉这一题,感觉松弛的理解又上了一个台阶,以及spfa的原理,最短路用到的原理就是松弛,先把图构造到最 ...
- poj2421【MST-prim+Kruskal】
水过~~~~打好基础/~~ ------prim #include <iostream> #include <stdio.h> #include <string.h> ...