Skip to content

Instantly share code, notes, and snippets.

@keenanwoodall
Created August 3, 2019 18:21
Show Gist options
  • Save keenanwoodall/4c5f28aa3b4861e655dac1d688388bbc to your computer and use it in GitHub Desktop.
Save keenanwoodall/4c5f28aa3b4861e655dac1d688388bbc to your computer and use it in GitHub Desktop.
Makes creating more than one prefab at once possible
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 👍");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment