Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active May 30, 2020 01:19
Show Gist options
  • Select an option

  • Save mminer/975358 to your computer and use it in GitHub Desktop.

Select an option

Save mminer/975358 to your computer and use it in GitHub Desktop.
Unity editor script to create prefab from selected game object.
using UnityEditor;
using UnityEngine;
/// <summary>
/// Creates a prefab from a selected game object.
/// </summary>
class CreatePrefabFromSelected
{
const string menuName = "GameObject/Create Prefab From Selected";
/// <summary>
/// Adds a menu named "Create Prefab From Selected" to the GameObject menu.
/// </summary>
[MenuItem(menuName)]
static void CreatePrefabMenu ()
{
var go = Selection.activeGameObject;
var prefab = EditorUtility.CreateEmptyPrefab("Assets/" + go.name + ".prefab");
EditorUtility.ReplacePrefab(go, prefab);
AssetDatabase.Refresh();
}
/// <summary>
/// Validates the menu.
/// The item will be disabled if no game object is selected.
/// </summary>
/// <returns>True if the menu item is valid.</returns>
[MenuItem(menuName, true)]
static bool ValidateCreatePrefabMenu ()
{
return Selection.activeGameObject != null;
}
}
@jareguo

jareguo commented May 30, 2014

Copy link
Copy Markdown

PrefabUtility.CreatePrefab

@mminer

mminer commented Nov 20, 2014

Copy link
Copy Markdown
Author

@jarequo: Indeed, this is redundant now. I'm unsure if PrefabUtility.CreatePrefab didn't exist when I wrote this script, or if I simply missed it.

@zornified

Copy link
Copy Markdown

it didn't exist when you wrote this article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment