热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Unity资源的查找

Object.DestroystaticfunctionDestroy(obj:Object,t:float0.0F):void;

Object.Destroy

static function Destroy(obj: Object, t: float = 0.0F): void;
 
Description

Removes a gameobject, component or asset.

The object  obj will be destroyed now or if a time is specified  t seconds from now. If  obj is a  Component it will remove the component from the GameObject and destroy it. If  objis a  GameObject it will destroy the  GameObject, all its components and all transform children of the GameObject. Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.
 
 
 
 
Resources.FindObjectsOfTypeAll
static Object[] FindObjectsOfTypeAll(Type type);
static T[] FindObjectsOfTypeAll ();
Parameters
type Type of the class to match while searching.
Returns
Object[] An array of objects whose class is  type or is derived from  type.
Description

Returns a list of all objects of Type type.

 

This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be  extra careful the way you handle the returned objects.
 
可以用来查找场景中被disable了的物件。
Contrary to Object.FindObjectsOfType this function will also list disabled objects.
 
Please note that this function is very slow and is not recommended to be used every frame.
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void OnGUI() {
        GUILayout.Label("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length);
        GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length);
        GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length);
        GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length);
        GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length);
        GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length);
        GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length);
    }
}
import System.Collections.Generic;
 

// This script finds all the objects in scene, excluding prefabs:
function GetAllObjectsInScene(): List. {
    var objectsInScene: List. = new List.();
    
    for (var go: GameObject in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]) {
        //对于一些不可编辑的资源,以及在场景中但隐藏,不保存,不删除的资源,例如mesh, texture,shader。跳过不处理。
if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave) continue; var assetPath: String = AssetDatabase.GetAssetPath(go.transform.root.gameObject);
     // 如果是可编辑的但又找到了对应的AssetDataBase路径,则应该是Prefab。 需验证
if (!String.IsNullOrEmpty(assetPath)) continue; objectsInScene.Add(go); } return objectsInScene; }

 

 

 

Component.GetComponentsInChildren

GameObject.GetComponentsInChildren

可以用来查找场景中被disable了的物件。
Component[] GetComponentsInChildren(Type t, bool includeInactive = false);
T[] GetComponentsInChildren(bool includeInactive);
T[]  GetComponentsInChildren ();
Parameters

t The type of Component to retrieve.
includeInactive Should inactive Components be included in the found set?
Description

Returns all components of Type type in the GameObject or any of its children.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    public Component[] hingeJoints;
    void Example() {
        hingeJoints = GetComponentsInChildren();
        foreach (HingeJoint joint in hingeJoints) {
            joint.useSpring = false;
        }
    }
}
GetComponent Returns the component of Type type if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function.
GetComponentInChildren Returns the component of Type type in the GameObject or any of its children using depth first search.
GetComponents Returns all components of Type type in the GameObject.
GetComponentsInChildren Returns all components of Type type in the GameObject or any of its children.

 

 

 

GameObject.Find

static function Find(name: string): GameObject;
Description

Finds a game object by name and returns it.

根据路径或名称查找场景物件

If no game object with  name can be found, null is returned. If name contains a '/' character it will traverse the hierarchy like a path name. This function only returns active gameobjects.
For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag(这个相对快一些?).
    var hand : GameObject;
    // This will return the game object named Hand in the scene.
    hand = GameObject.Find("Hand");
// This will return the game object named Hand. // Hand must not have a parent in the hierarchy view! // 绝对路径查找 hand = GameObject.Find("/Hand");
// This will return the game object named Hand, // which is a child of Arm -> Monster. // Monster must not have a parent in the hierarchy view! // 绝对路径查找 hand = GameObject.Find("MonsterArm/Hand"); // 应该是 hand = GameObject.Find("/Monster/Arm/Hand")吧?待验证
// This will return the game object named Hand, // which is a child of Arm -> Monster. // Monster may have a parent. // 相对路径查找 hand = GameObject.Find("MonsterArmHand"); // 应该是hand = GameObject.Find("Monster/Arm/Hand")吧?待验证

 

This function is most useful to automatically connect references to other objects at load time, eg. inside MonoBehaviour.Awake or MonoBehaviour.Start. You should avoid calling this function every frame eg. MonoBehaviour.Update for performance reasons. A common pattern is to assign a game object to a variable inside MonoBehaviour.Start. And use the variable in MonoBehaviour.Update.
	// Find the hand inside Start and rotate it every frame
	private var hand : GameObject;
	function Start () {
		hand = GameObject.Find("MonsterArm/Hand");
	}
	
	function Update () {
		hand.transform.Rotate(0, 100 * Time.deltaTime, 0);
	}
Find Finds a game object by name and returns it.
FindGameObjectsWithTag Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTag Returns one active GameObject tagged tag. Returns null if no GameObject was found.

 

 

 

Object.FindObjectsOfType

static function FindObjectsOfType(type: Type): Object[];
Description

Returns a list of all active loaded objects of Type type.

类型查找Object, 由于是指定了Type的,除非指定的Type是GameObject或者Resource Object,一般该函数都是用来查找返回Component?

It will return no assets (meshes, textures, prefabs, ...) or inactive objects.(无法查找资源以及被disable了的Object
Please note that this function is very slow. It is not recommended to use this function every frame. In most cases you can use the singleton pattern instead.
    // When clicking on the object, it will disable all springs on all 
    // hinges in the scene.
    function OnMouseDown () {
        var hinges : HingeJoint[] = FindObjectsOfType(HingeJoint) as HingeJoint[];
        for (var hinge : HingeJoint in hinges) {
            hinge.useSpring = false;
        }
    }
FindObjectOfType Returns the first active loaded object of Type type.
FindObjectsOfType Returns a list of all active loaded objects of Type type.

 

 

 


推荐阅读
  • 本文介绍了如何使用OpenXML按页码访问文档内容,以及在处理分页符和XML元素时的一些挑战。同时,还讨论了基于页面的引用框架的局限性和超越基于页面的引用框架的方法。最后,给出了一个使用C#的示例代码来按页码访问OpenXML内容的方法。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了一种在PHP中对二维数组根据某个字段进行排序的方法,以年龄字段为例,按照倒序的方式进行排序,并给出了具体的代码实现。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • MySQL多表数据库操作方法及子查询详解
    本文详细介绍了MySQL数据库的多表操作方法,包括增删改和单表查询,同时还解释了子查询的概念和用法。文章通过示例和步骤说明了如何进行数据的插入、删除和更新操作,以及如何执行单表查询和使用聚合函数进行统计。对于需要对MySQL数据库进行操作的读者来说,本文是一个非常实用的参考资料。 ... [详细]
  • 本文介绍了Foundation框架中一些常用的结构体和类,包括表示范围作用的NSRange结构体的创建方式,处理几何图形的数据类型NSPoint和NSSize,以及由点和大小复合而成的矩形数据类型NSRect。同时还介绍了创建这些数据类型的方法,以及字符串类NSString的使用方法。 ... [详细]
  • 本文整理了Java中com.evernote.android.job.JobRequest.getTransientExtras()方法的一些代码示例,展示了 ... [详细]
  • 使用Spring AOP实现切面编程的步骤和注意事项
    本文介绍了使用Spring AOP实现切面编程的步骤和注意事项。首先解释了@EnableAspectJAutoProxy、@Aspect、@Pointcut等注解的作用,并介绍了实现AOP功能的方法。然后详细介绍了创建切面、编写测试代码的过程,并展示了测试结果。接着讲解了关于环绕通知的使用方法,并修改了FirstTangent类以添加环绕通知方法。最后介绍了利用AOP拦截注解的方法,只需修改全局切入点即可实现。使用Spring AOP进行切面编程可以方便地实现对代码的增强和拦截。 ... [详细]
author-avatar
醉翁cx布衣
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有