Create prefabs from sprites faster?

Is there a way to create prefabs from multiple sprites faster? The current process now is:

  1. Drag a sprite from Project view to Hierarchy
  2. Drag it back to the Project view

It’s quite painful to do with 207 sprites that I want to make prefabs into, and I have maaany more to do. :slight_smile:

It would be cool to right-click a sprite in Project view and click “Create prefab”, which would do it, so i could just select all sprites and do it in one batch. Shouldn’t this be possible with an editor script?

Also it would be very helpful to save to a different folder, but I guess the script could handle that too.

Another thing that would help would be if I could drag multiple sprites into the scene, but it just wants to make an animation. Is there any way to just add all sprites?

Thank you for any ideas!

This script works like a charm, and also adds a right-click to the project view, to convert one or many items.
You might have to change the newPath replace method to your likings, but I have sprites and prefabs in the same structure except Graphics/Prefabs like this:
newPath Assets/_Game/Prefabs/Base/Decor/floor_chair_03.prefab

Hope it helps someone! :slight_smile:

        [MenuItem("Assets/Convert to Prefab")]
        static void SpriteToPrefab() {
            foreach (Object o in Selection.objects)  {
                if (o is Texture2D) {
                    var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(AssetDatabase.GetAssetPath(o));
                    var oldPath = AssetDatabase.GetAssetPath(o);
                    var newPath = oldPath.Replace("Graphics", "Prefabs").Replace(".png",".prefab");
                    var go = new GameObject();
                    var sr = go.AddComponent<SpriteRenderer>();
                    sr.sprite = sprite;
                    PrefabUtility.SaveAsPrefabAsset(go, newPath);
                    DestroyImmediate(go);
                } else {
                    Debug.LogError("This is not a sprite");
                    Debug.Log(o.GetType());
                }
            }
        }
2 Likes

Very helpful. Using AI I’ve managed to make this work for me with slight changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO; // added this to use Path.ChangeExtension method
public class ImageToPrefab2 : MonoBehaviour
{
[MenuItem(“Assets/Convert to Prefab”)]
static void SpriteToPrefab() {
foreach (Object o in Selection.objects) {
if (o is Texture2D) {
var sprite = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(o));
var oldPath = AssetDatabase.GetAssetPath(o);
var newPath = Path.ChangeExtension(oldPath, “.prefab”); // changed this to use Path.ChangeExtension method
newPath = newPath.Replace(“Images”, “Prefabs”); // changed this to replace Images with Prefabs
var go = new GameObject();
var sr = go.AddComponent();
sr.sprite = sprite;
if (!AssetDatabase.IsValidFolder(“Assets/Prefabs”)) { // added this to check if the folder exists
AssetDatabase.CreateFolder(“Assets”, “Prefabs”); // added this to create the folder if it does not exist
}
PrefabUtility.SaveAsPrefabAsset(go, newPath);
DestroyImmediate(go);
} else {
Debug.LogError(“This is not a sprite”);
Debug.Log(o.GetType());
}
}
}
}