CreatePrefabFromSelected

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(C# - CreatePrefabFromSelected.cs)
(C# - CreatePrefabFromSelected.cs Procedural Mesh Version)
Line 123: Line 123:
 
== C# - CreatePrefabFromSelected.cs Procedural Mesh Version==
 
== C# - CreatePrefabFromSelected.cs Procedural Mesh Version==
  
Small update to add Mesh-Saving for procedural mesh prefabs: use on objects that have created a mesh... it saves the created mesh inside the prefab.
+
Small update to add Mesh-Saving for procedural mesh prefabs: use on objects that have created a mesh... it saves the created mesh to disk and inside the prefab, instead of losing it.
  
 
Requires that you have a folder called Assets/savedMesh
 
Requires that you have a folder called Assets/savedMesh

Revision as of 21:38, 5 April 2014

Author: Matthew Miner (matthew@matthewminer.com)

Contents

Description

Creates a prefab containing the contents of the currently selected game object.

Usage

Place the script inside the Editor folder. Select a GameObject and choose 'GameObject > Create Prefab From Selection'. A new prefab will be created in the root directory bearing the same name as the GameObject.

Faults:

To Do

  • Currently the script replaces any existing prefab with the same name; more desirable would be to append a number to the filename of the new prefab
  • The selected GameObject should become an instance of the newly created prefab

C# - CreatePrefabFromSelected.cs

using UnityEditor;
using UnityEngine;
 
class CreatePrefabFromSelected
{
	const string menuTitle = "GameObject/Create Prefab From Selected";
 
	/// <summary>
	/// Creates a prefab from the selected game object.
	/// </summary>
	[MenuItem (menuTitle)]
	static void CreatePrefab ()
	{
		GameObject obj = Selection.activeGameObject;
		string name = obj.name;
 
		Object prefab = EditorUtility.CreateEmptyPrefab("Assets/" + name + ".prefab");
		EditorUtility.ReplacePrefab(obj, prefab);
		AssetDatabase.Refresh();
	}
 
	/// <summary>
	/// Validates the menu.
	/// </summary>
	/// <remarks>The item will be disabled if no game object is selected.</remarks>
	[MenuItem (menuTitle, true)]
	static bool ValidateCreatePrefab ()
	{
		return Selection.activeGameObject != null;
	}
}

C# - Updated CreatePrefabFromSelected.cs

I decided to update this script in can anybody is intewrested. It now will give a popup if the prefab already exits and you can choose to overwrite the prefab or cancel. It also replaces the selected gameObject with the new prefab.

  • Cameron Bonde

- Updated to accept multiple selected objects and not use /prefabs folder (without it it deletes the GO without creating prefab)

- Fixed the script to make sure the new object have the same parent as the old one in the scene. --Blikstad 06:11, 20 May 2012 (PDT)

using UnityEditor;
using UnityEngine;
using System.Collections;
 
class CreatePrefabFromSelected : ScriptableObject
{
    const string menuTitle = "GameObject/Create Prefab From Selected";
 
    /// <summary>
    /// Creates a prefab from the selected game object.
    /// </summary>
    [MenuItem(menuTitle)]
    static void CreatePrefab()
    {
        GameObject[] obj = Selection.gameObjects;
 
        foreach (GameObject go in obj)
        {
            string name = go.name;
            string localPath = "Assets/" + name + ".prefab";
 
            if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))
            {
                if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No"))
                {
                    createNew(go, localPath);
                }
            }
            else
            {
                createNew(go, localPath);
            }
 
        }
    }
 
    static void createNew(GameObject obj, string localPath)
    {
        Object prefab = PrefabUtility.CreatePrefab (localPath, obj);
        EditorUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
        AssetDatabase.Refresh();
    }
 
    /// <summary>
    /// Validates the menu.
    /// </summary>
    /// <remarks>The item will be disabled if no game object is selected.</remarks>
    [MenuItem(menuTitle, true)]
    static bool ValidateCreatePrefab()
    {
        return Selection.activeGameObject != null;
    }
}

C# - CreatePrefabFromSelected.cs Procedural Mesh Version

Small update to add Mesh-Saving for procedural mesh prefabs: use on objects that have created a mesh... it saves the created mesh to disk and inside the prefab, instead of losing it.

Requires that you have a folder called Assets/savedMesh


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;
 
		Mesh m1 = go.GetComponent<MeshFilter>().mesh;//update line1
		AssetDatabase.CreateAsset(m1, "Assets/savedMesh/" + go.name +"_M" + ".asset"); // update line2
 
 
		var prefab = EditorUtility.CreateEmptyPrefab("Assets/savedMesh/" + 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;
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox