Created
August 3, 2019 18:21
-
-
Save keenanwoodall/4c5f28aa3b4861e655dac1d688388bbc to your computer and use it in GitHub Desktop.
Revisions
-
keenanwoodall created this gist
Aug 3, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ using UnityEngine; using UnityEditor; public static class PrefabUtilityMenuItems { [MenuItem("Tools/Prefab Utility/Create Prefabs From Selection")] public static void CreateFromSelection() { var selections = Selection.gameObjects; if (selections == null || selections.Length == 0) { EditorUtility.DisplayDialog("Prefab Creation Failed", "No game objects selected", "Gotcha π"); return; } var folderPath = EditorUtility.SaveFolderPanel("Save prefabs to folder", string.Empty, string.Empty); if (string.IsNullOrEmpty(folderPath)) return; var creationAttempts = 0; var successfulCreationAttempts = 0; foreach (var selection in selections) { creationAttempts++; var success = false; PrefabUtility.SaveAsPrefabAssetAndConnect(selection.gameObject, $"{folderPath}/{selection.name}.prefab", InteractionMode.UserAction, out success); if (success) successfulCreationAttempts++; } EditorUtility.DisplayDialog("Prefab Creation Finished", $"{successfulCreationAttempts} out of {creationAttempts} saved as prefabs.", "Ok good to know π"); } [MenuItem("Tools/Prefab Utility/Create Prefabs From Selection's Children")] public static void CreateFromSelectionsChildren() { var selections = Selection.gameObjects; if (selections == null || selections.Length == 0) { EditorUtility.DisplayDialog("Prefab Creation Failed", "No game objects selected", "Gotcha π"); return; } var totalChildren = 0; foreach (var selection in selections) foreach (Transform child in selection.transform) totalChildren++; if (EditorUtility.DisplayDialogComplex ( title: "Prefab Creation", message: $"Are you sure you want to create {totalChildren} prefabs?", ok: "Yea", cancel: "Actually no", alt: string.Empty ) != 0) return; var folderPath = EditorUtility.SaveFolderPanel("Save prefabs to folder", string.Empty, string.Empty); if (string.IsNullOrEmpty(folderPath)) return; var creationAttempts = 0; var successfulCreationAttempts = 0; foreach (var selection in selections) { foreach (Transform child in selection.transform) { creationAttempts++; var success = false; PrefabUtility.SaveAsPrefabAssetAndConnect(child.gameObject, $"{folderPath}/{child.name}.prefab", InteractionMode.UserAction, out success); if (success) successfulCreationAttempts++; } } EditorUtility.DisplayDialog("Prefab Creation Finished", $"{successfulCreationAttempts} out of {creationAttempts} children saved as prefabs.", "Ok good to know π"); } }