Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active May 30, 2020 01:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mminer/975358 to your computer and use it in GitHub Desktop.
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;
}
}
@zornified
Copy link

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