Version: 2023.1

PrefabUtility.GetCorrespondingObjectFromSourceAtPath

切换到手册
public static TObject GetCorrespondingObjectFromSourceAtPath (TObject componentOrGameObject, string assetPath);

参数

componentOrGameObject 从其查找相应对象的对象。
assetPath 从其获取相应对象的预制件资源的资源路径。

返回

TObject 对应对象或 null。

描述

Retrieves the corresponding object of the given object from a given Prefab Asset path.

Use this method to find the corresponding asset the source was instantiated from, if but only if it is within the prefab asset at the specified path.

If you don't have the path to pass in, you can use GetCorrespondingObjectFromSource instead.

This method returns null if the given object does not have a corresponding object within the prefab asset at the specified path.

For example, in the diagram shown below, prefab asset "A" contains a child nested prefab "B", which contains a child nested prefab "C".



In this example scenario, when the GameObject "C (Instance)" is passed in as the source to this method, and "Assets/A.prefab" is passed as the path, this method returns the object "C (Nested Prefab)" from the prefab asset "A".

The example script below adds a menu item to the editor, which launches a simple wizard that allows you to test the results of this method.

using UnityEditor;
using UnityEngine;
public class AssetSourceTestWizard : ScriptableWizard
{
    public GameObject instance;
    public string path;

[MenuItem("Test/Asset Source Test Wizard")] static void CreateWizard() { ScriptableWizard.DisplayWizard<AssetSourceTestWizard>("Asset Source Test Wizard", "Do Test"); }

void OnWizardCreate() { var o1 = PrefabUtility.GetCorrespondingObjectFromSourceAtPath(instance, path); Debug.Log("Corresponding object from source: " + o1.name); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961