脚本要放在Edtior文件夹
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ConvertPrefabsToGameObjectsEditor : EditorWindow
{
[MenuItem("Tools/取消所有预制体")]
public static void ShowWindow()
{
GetWindow<ConvertPrefabsToGameObjectsEditor>("Convert Prefabs to Game Objects");
}
private void OnGUI()
{
if (GUILayout.Button("Convert Prefabs to Game Objects"))
{
ConvertPrefabsToGameObjects();
}
}
private void ConvertPrefabsToGameObjects()
{
Scene currentScene = SceneManager.GetActiveScene();
GameObject[] rootObjects = currentScene.GetRootGameObjects();
foreach (GameObject rootObj in rootObjects)
{
// 遍历场景中的所有对象
TraverseHierarchy(rootObj);
}
}
private void TraverseHierarchy(GameObject obj)
{
// 检查该对象是否是预制体的实例
if (PrefabUtility.IsPartOfPrefabInstance(obj))
{
// 将预制体实例转换为普通游戏对象
PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
Debug.Log("Prefab instance converted to game object: " + obj.name);
}
// 递归遍历子对象
for (int i = 0; i < obj.transform.childCount; i++)
{
TraverseHierarchy(obj.transform.GetChild(i).gameObject);
}
}
}