Created
January 14, 2024 03:53
-
-
Save sonnguyen9800/858041853be28d5691cea160d3e76a2c to your computer and use it in GitHub Desktop.
Batch Convert Unity Scene into Prefab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment