Unity 2020.3.31
错误信息:OperationException: Destroying a GameObject inside a Prefab instance is not allowed.
错误分析:操作Prefab时,进行删除对象,需要Unpack Prefab或者打开Prefab进行操作。
解决方案:
private static bool UnpackPrefab(GameObject prefab)
{
if (!PrefabUtility.IsPartOfPrefabInstance(prefab.transform))
{
return false;
}
PrefabUtility.UnpackPrefabInstance(prefab, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
return true;
}
private static void DestroyInPrefab(string prefabPath)
{
var rootObj = PrefabUtility.LoadPrefabContents(prefabPath);
// Destroy child objects or components on rootObj
PrefabUtility.SaveAsPrefabAsset(rootObj, prefabPath);
PrefabUtility.UnloadPrefabContents(rootObj);
}