如何获得GameObject根预制件? [英] How can I get a GameObject root prefab?
问题描述
我有一个EditorWindow脚本.
I have a EditorWindow script.
内部OnGUI:
for (var i = selection.Count - 1; i >= 0; --i)
{
var selected = selection[i];
if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(selected))
{
var root = selected.GetComponentInParent(typeof(GameObject));
PrefabUtility.UnpackPrefabInstance(selected, PrefabUnpackMode.Completely, UnityEditor.InteractionMode.AutomatedAction);
}
}
我想找到选定的一部分的预制件. 但这给了我一个例外:
I want to find the prefab the selected is part of. But this give me exception:
ArgumentException:GetComponent要求所请求的组件'GameObject'是从MonoBehaviour或Component派生的,或者是一个接口.
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
主要目标是销毁作为预制实例一部分的游戏对象. 为此,我需要先找到选定的游戏对象,然后将其从预制件中拆开,然后销毁.
The main goal is destroy a gameobject that is a part of a prefab instance. For that I need first to find the gameobject(selected) it's prefab the unpack it from the prefab and then to destroy it.
但是我无法得到它的预制件.
But I can't get the prefab of it.
推荐答案
有效的解决方案:
for (var i = selection.Count - 1; i >= 0; --i)
{
var selected = selection[i];
if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(selected))
{
var root = PrefabUtility.GetOutermostPrefabInstanceRoot(selected);
PrefabUtility.UnpackPrefabInstance(root, PrefabUnpackMode.Completely, UnityEditor.InteractionMode.AutomatedAction);
}
}
这篇关于如何获得GameObject根预制件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!