Batch Convert Unity Scene into Prefab
Created on: 14 Jan 24 10:50 +0700 by Son Nguyen Hoang in English
Quick Snippet to export game object into prefab
Motivation: You have 100 Scene, each with multiple game objects. Some game object should be exported into a prefab. How can you do it quick?
Below is code snippet for quick reference
public class Tool: EditorWindow
{
[MenuItem("Tool/Create Prefab", false, -10)]
private static void CreatePrefab(UnityEditor.MenuCommand command)
{
GameObject selectedOb = (GameObject)command.context;
Transform selectedTransform = selectedOb.transform;
GameObject rootObject = selectedTransform.root.gameObject;
string path = EditorUtility.SaveFilePanel("Save Prefab", "Assets/MyPrefabs", rootObject.name, "prefab");
if (!string.IsNullOrEmpty(path))
{
path = FileUtil.GetProjectRelativePath(path);
GameObject obj = selectedOb;
PrefabUtility.SaveAsPrefabAssetAndConnect(obj, path, InteractionMode.UserAction);
}
}
private static string _path_to_all_level_scenes = "";
private static string _path_to_all_bosslevel_scenes = "";
private static string _path_to_level_prefab_dest = "";
private static string _path_to_bosslevel_prefab_dest = "";
[MenuItem("Tool/Create Prefab(s)", false, -10)]
private static void CreateAllLevelPrefabs()
{
string[] files = Directory.GetFiles(_path_to_all_level_scenes, "*.unity");
foreach (string file in files)
{
// string sceneName = Path.GetFileNameWithoutExtension(file);
ProcessSceneToPrefab(file, _path_to_level_prefab_dest);
}
}
[MenuItem("Tool/Create Prefab(s) - Boss Level", false, -10)]
private static void CreateAllBossLevelPrefabs()
{
string[] files = Directory.GetFiles(_path_to_all_bosslevel_scenes, "*.unity");
foreach (string file in files)
{
// string sceneName = Path.GetFileNameWithoutExtension(file);
ProcessSceneToPrefab(file, _path_to_bosslevel_prefab_dest);
}
}
private static void ProcessSceneToPrefab(string path_to_scene, string destination)
{
int count = EditorSceneManager.sceneCount;
for (int i = 0; i < count; i++)
{
Scene sceneI = SceneManager.GetSceneAt(i);
Debug.LogError("Remove scene: " + sceneI.name);
if (sceneI.buildIndex != SceneManager.GetActiveScene().buildIndex)
{
EditorSceneManager.UnloadSceneAsync(sceneI);
}
}
Scene scene = EditorSceneManager.OpenScene(path_to_scene, OpenSceneMode.Single);
//var allObjects =
//GameObject myGameObject = GameObject.Find("GameController");
//if (myGameObject != null)
//{
// string prefabPath = destination + "/" + scene.name + ".prefab";
// PrefabUtility.SaveAsPrefabAsset(myGameObject, prefabPath);
//}
GameObject root = new GameObject("PrefabRoot");
var allGameObjectsInScene = GameObject.FindObjectsOfType<Transform>();
foreach (var t in allGameObjectsInScene)
{
if (t.name == "Directional Light")
continue;
if (t.name == "Canvas")
continue;
if (t.name == "Main Camera")
continue;
if (t.name == "AudioManager")
continue;
if (t.parent == null)
{
t.parent = root.transform;
continue;
}
}
// Save the prefab
string path = destination + "/" + scene.name + ".prefab";
if (!string.IsNullOrEmpty(path))
{
// path = FileUtil.GetProjectRelativePath(path);
// PrefabUtility.SaveAsPrefabAssetAndConnect(root, path, InteractionMode.UserAction);
PrefabUtility.SaveAsPrefabAsset(root, path);
}
}
}