如何获得GameObject root预制件?

问题描述 投票:0回答:1

我有一个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,或者是一个接口。

主要目标是销毁作为预制实例一部分的游戏对象。为此,我首先需要找到游戏对象(已选择),它预制从预制件中解压缩然后将其销毁。

但我无法得到它的预制件。

c# unity3d
1个回答
0
投票

工作解决方案:

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);
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.