Unity资源的查找

Object.Destroy

static function Destroy(objObjectt: 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<T> ();
Parameters
typeType 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.<GameObject> {
    var objectsInScene: List.<GameObject> = new List.<GameObject>();
    
    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<T>(bool includeInactive);
T[]  GetComponentsInChildren<T> ();
Parameters

tThe type of Component to retrieve.
includeInactiveShould 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<HingeJoint>();
        foreach (HingeJoint joint in hingeJoints) {
            joint.useSpring = false;
        }
    }
}
GetComponentReturns 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.
GetComponentInChildrenReturns the component of Type type in the GameObject or any of its children using depth first search.
GetComponentsReturns all components of Type type in the GameObject.
GetComponentsInChildrenReturns 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);
	}
FindFinds a game object by name and returns it.
FindGameObjectsWithTagReturns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTagReturns 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;
        }
    }
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsOfTypeReturns a list of all active loaded objects of Type type.

 

 

 

转载于:https://www.cnblogs.com/realtimepixels/p/3652154.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值