Skip to content

Instantly share code, notes, and snippets.

@twobob
Last active April 13, 2017 00:32
Show Gist options
  • Save twobob/deff9e20b94b6c80839426bdfbce4ac7 to your computer and use it in GitHub Desktop.
Save twobob/deff9e20b94b6c80839426bdfbce4ac7 to your computer and use it in GitHub Desktop.
Editor script to save slected items as prefabs (fixed Directory Seperator on windows)
using UnityEditor;
using UnityEngine;
using System.Collections;
using System;
using System.IO;
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()
{
var objs = Selection.gameObjects;
string pathBase = EditorUtility.SaveFolderPanel ("Choose save folder", "Assets", "");
if (! String.IsNullOrEmpty (pathBase)) {
pathBase=pathBase.Remove(0,pathBase.IndexOf("Assets"))+Path.DirectorySeparatorChar;
foreach (var go in objs) {
String localPath = pathBase + go.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)
{
localPath = localPath.Replace('\\','/');
UnityEngine.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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment