using UnityEditor; using UnityEngine; /// /// Creates a prefab from a selected game object. /// class CreatePrefabFromSelected { const string menuName = "GameObject/Create Prefab From Selected"; /// /// Adds a menu named "Create Prefab From Selected" to the GameObject menu. /// [MenuItem (menuName)] static void CreatePrefabMenu () { GameObject go = Selection.activeGameObject; string name = go.name; Object prefab = EditorUtility.CreateEmptyPrefab("Assets/" + name + ".prefab"); EditorUtility.ReplacePrefab(go, prefab); AssetDatabase.Refresh(); } /// /// Validates the menu. /// The item will be disabled if no game object is selected. /// /// True if the menu item is valid. [MenuItem (menuName, true)] static bool ValidateCreatePrefabMenu () { return Selection.activeGameObject != null; } }