我有一个EditorWindow脚本。
在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);
}
}
我想找到被选中的预制件。但这给了我例外:
ArgumentException:GetComponent要求所请求的组件“GameObject”派生自MonoBehaviour或Component,或者是一个接口。
主要目标是销毁作为预制实例一部分的游戏对象。为此,我首先需要找到游戏对象(已选择),它预制从预制件中解压缩然后将其销毁。
但我无法得到它的预制件。
工作解决方案:
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);
}
}