Skip to content

Instantly share code, notes, and snippets.

@keenanwoodall
Created August 3, 2019 18:21
Show Gist options
  • Select an option

  • Save keenanwoodall/4c5f28aa3b4861e655dac1d688388bbc to your computer and use it in GitHub Desktop.

Select an option

Save keenanwoodall/4c5f28aa3b4861e655dac1d688388bbc to your computer and use it in GitHub Desktop.

Revisions

  1. keenanwoodall created this gist Aug 3, 2019.
    78 changes: 78 additions & 0 deletions PrefabUtilityMenuItems.cs
    Original 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 πŸ‘");
    }
    }