Version: 2023.1

PrefabUtility.FindAllInstancesOfPrefab

切换到手册
public static GameObject[] FindAllInstancesOfPrefab (GameObject prefabRoot);
public static GameObject[] FindAllInstancesOfPrefab (GameObject prefabRoot, SceneManagement.Scene scene);

参数

prefabRoot The root GameObject of a Prefab asset.
scene The scene to search for Prefab instances. The scene you specify must be valid and loaded.

返回

GameObject[] The root GameObjects for all instances of the Prefab asset with root prefabRoot.

描述

Retrieves the root GameObjects for all instances of the Prefab asset with root prefabRoot found in all currently loaded scenes. If prefabRoot is not a valid Prefab asset root GameObject, an ArgumentException is thrown.

FindAllInstancesOfPrefab will not return nested Prefab instances.

using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using NUnit.Framework;

public class Example : MonoBehaviour { public GameObject prefabAssetRoot; private void Start() { var expectedInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot); var instances = PrefabUtility.FindAllInstancesOfPrefab(prefabAssetRoot); Assert.AreEqual(1, instances.Length); Assert.AreEqual(expectedInstance, instances[0]); } }

描述

Returns the root GameObjects for all instances of the Prefab asset with root prefabRoot found in scene. If prefabRoot is not a valid Prefab asset root GameObject, or scene is not valid and loaded, ArgumentException is thrown.

FindAllInstancesOfPrefab will not return nested Prefab instances.

using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using NUnit.Framework;

public class Example : MonoBehaviour { public GameObject prefabAssetRoot; private void Start() { var expectedInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot); var instances = PrefabUtility.FindAllInstancesOfPrefab(prefabAssetRoot, SceneManager.GetActiveScene()); Assert.AreEqual(1, instances.Length); Assert.AreEqual(expectedInstance, instances[0]); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961