Unity editor script to create prefab from selected game object.
This file contains 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 characters
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; | |
} | |
} |
@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.
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
PrefabUtility.CreatePrefab