Hey guys, I have a number of sprites and I want to add them to the scene. When I select them all and drag them to the hierarchy, Unity will try to automatically create a sprite animation for me. This is really handy when I want the animation, but in this case I just want each sprite placed in the scene separately with one drag. So is there any way I can do this?
Bump
Had the same need to instantiate multiple sprites instead of creating an animation, couldnât see any other way around it. I figure itâs only a matter of time before they enable this in a more intuitive fashion but for now you can use the attached editor script (just place inside of an âEditorâ folder).
Will add a tabbed window option in âWindow \ Sprite Instantiator.â You can then select a parent from the scene and 1 or more sprites from your assets and click the button.
Hope it becomes obsolete soon : D
Update: Unity now supports Alt-dragging sprites into the scene to instantiate them instead of creating an animation which, thankfully, renders my script obsolete. But I still updated it to make it slightly more useful: You can select the parent texture object instead of the actual sprite object, and if you select things one by one you they should be instantiated in the order you selected them in (selecting multiple sprites at once does not guarantee the order in which they are instantiated).
1946744â263997âSpriteInstantiator.cs (3.19 KB)
This script doesnât appear to be working. It wonât allow me to add the sprites from asset folder to the window in any way and the âInstantiate Spritesâ button is greyed.
Not sure if this is because of an editor API change in 5.0 or what?
It should still work - verify that your image assets have their Texture Type set to Sprite (2D and UI) and that you are selecting actual sprite objects (contained as dropdown items inside the image asset once the proper Texture Type is set).
Ah you have to select the sprite in dropdown and then the button appears. I was trying to drag and drop!
thanks! HUGE time saver for my current project (main character is over 100 sprites so any time the artists change things itâs a long process for me)
Youâre my hero for today!
Awesome. I wasnât sure if I was clear enough in my last post. By all means feel free to modify the script if you find a better/more intuitive approach.
Cheers!
On Mac you can add the sprites to the scene while holding the âaltâ button to prevent creating animation.
thank you 21n0, that is also working on windows.
Thank you Roman! 2017 and the script is working.
Because this thead is quite old, here is the Code in Case the download link from Roland1234 gets lost:
//SpriteInstantiator.cs - Place inside and "Editor" folder.
//Roman Issa (Roland1234) - 2015/2/2
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class SpriteInstantiator : EditorWindow
{
[MenuItem("Window/Sprite Instantiator")]
public static void ShowWindow()
{
GetWindow<SpriteInstantiator>("Sprite Instantiator").ShowTab();
}
private List<Sprite> _sprites = new List<Sprite>();
private Transform _parent;
private void OnEnable()
{
UpdateSelection();
}
private void OnDisable()
{
_sprites.Clear();
_parent = null;
}
private void OnSelectionChange()
{
UpdateSelection();
}
private void OnInspectorUpdate()
{
Repaint();
}
private void OnGUI()
{
var labelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 80.0f;
var enabled = GUI.enabled;
GUI.enabled = _sprites.Count > 0;
if(GUILayout.Button(string.Format("Instantiate Sprites[{0}]", _sprites.Count), GUILayout.ExpandWidth(true)))
{
Selection.objects = _sprites.Select(s =>
{
var newSprite = new GameObject(s.name ?? "sprite", typeof(SpriteRenderer)).GetComponent<SpriteRenderer>();
newSprite.sprite = s;
newSprite.transform.position = GetInstantiatePosition();
newSprite.transform.parent = _parent;
return newSprite.gameObject;
}).ToArray();
}
GUI.enabled = enabled;
EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("x", GUILayout.Width(20.0f)))
{
_parent = null;
}
_parent = (Transform)EditorGUILayout.ObjectField("Parent", _parent, typeof(Transform), true);
EditorGUILayout.EndHorizontal();
EditorGUIUtility.labelWidth = labelWidth;
}
private Vector3 GetInstantiatePosition()
{
if(SceneView.currentDrawingSceneView != null && SceneView.currentDrawingSceneView.camera != null)
{
var pos = SceneView.currentDrawingSceneView.camera.transform.position;
pos.z = 0.0f;
return pos;
}
return Vector3.zero;
}
private void UpdateSelection()
{
if(Selection.activeTransform != null)
{
_parent = Selection.activeTransform;
}
_sprites = Selection.objects.OfType<Sprite>().ToList();
}
}
Guys I tried the script that Roland1234 posted. But it does not allow me to select sprites in the dropdown. It shows the Assets tab as empty. Whereas my sprites do have Texture Type as Sprite .
I also tried modifying the script as posted by Deniz2014, but this also didnât work for me. Same scenario happens âno items are listed in Assets tab drop downâ.
I am working on Unity 5.4.1 Is that the issue?
Please suggest with an option. I have 6600 sprites to work with
Solution posted by 21n0 worked well on windows. But the sprites didnât come in sequence i.e. 1,2,3,4⌠the way they are available in Assets folder. After i drag drop, they jumbled up in abnormal sequence i.e. 3,1,4,2âŚ
Please suggest if I am doing something wrong here?
Alt-dragging (as 21n0 points out) should be the preferred method now. The script I uploaded still works, but you have to use it properly: With the Sprite Instantiator window open you first select an optional parent in the scene that will contain the sprites, you then select the sprites themselves from your Project window (Window / Project (Ctrl+5)) - youâll see the Sprite Instantiator update how many sprites it will add to the scene when you do this properly.
Iâm not sure if thereâs any way to guarantee the order in which they appear, but Iâve updated the script to attempt and preserve the order in which they are selected in. This means you have to select things one by one in the order you want. Or, if you Shift-select multiple sprites at once, the script will attempt to add the sprites to the selection sorted by file path (which should be the default order they appear in the Project window). I donât know how useful that might be to you, but itâs what I could figure out at the moment.
I hope this makes sense to you, itâs kind of difficult to convey all this in text.
Cheers!
Thanks Roland1234. I understood what you said. I found a solution that worked for me:
I made a public array in script. Select all sprites in project. drag drop sprites in array at once, as shown in this link:
This also assigns the sprites in jumbled manner. Now here I added my script which takes these sprites and assign them in proper sequence in another array. So once my new array is achieved, i use this new array in my game.
Hold down Alt when dragging
thanks, it is very useful and simple when not have to create a script