Search Unity

How to prevent creating animation when dragging multiple sprites into the hierarchy?

Discussion in 'Editor & General Support' started by AdamLiu, Nov 22, 2014.

  1. AdamLiu

    AdamLiu

    Joined:
    Mar 19, 2014
    Posts:
    71
    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?
     
    RuneShiStorm likes this.
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
  3. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    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).
     

    Attached Files:

    Last edited: Jan 25, 2018
  4. jammingames

    jammingames

    Joined:
    Nov 17, 2013
    Posts:
    8
    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?
     
  5. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    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).
     
  6. jammingames

    jammingames

    Joined:
    Nov 17, 2013
    Posts:
    8
    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!
     
  7. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    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!
     
    andrewow likes this.
  8. 21n0

    21n0

    Joined:
    Jan 12, 2016
    Posts:
    1
    On Mac you can add the sprites to the scene while holding the 'alt' button to prevent creating animation.
     
    WayneJP, MattDavis, Tivaly and 9 others like this.
  9. blueknee

    blueknee

    Joined:
    Apr 5, 2014
    Posts:
    8
    thank you 21n0, that is also working on windows.
     
    liadk likes this.
  10. Deniz2014

    Deniz2014

    Joined:
    Apr 27, 2014
    Posts:
    8
    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:

    Code (CSharp):
    1. //SpriteInstantiator.cs - Place inside and "Editor" folder.
    2. //Roman Issa (Roland1234) - 2015/2/2
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEditor;
    6. using UnityEngine;
    7.  
    8. public class SpriteInstantiator : EditorWindow
    9. {
    10.     [MenuItem("Window/Sprite Instantiator")]
    11.     public static void ShowWindow()
    12.     {
    13.         GetWindow<SpriteInstantiator>("Sprite Instantiator").ShowTab();
    14.     }
    15.  
    16.     private List<Sprite> _sprites = new List<Sprite>();
    17.     private Transform _parent;
    18.  
    19.     private void OnEnable()
    20.     {
    21.         UpdateSelection();
    22.     }
    23.  
    24.     private void OnDisable()
    25.     {
    26.         _sprites.Clear();
    27.         _parent = null;
    28.     }
    29.  
    30.     private void OnSelectionChange()
    31.     {
    32.         UpdateSelection();
    33.     }
    34.  
    35.     private void OnInspectorUpdate()
    36.     {
    37.         Repaint();
    38.     }
    39.  
    40.     private void OnGUI()
    41.     {
    42.         var labelWidth = EditorGUIUtility.labelWidth;
    43.         EditorGUIUtility.labelWidth = 80.0f;
    44.      
    45.         var enabled = GUI.enabled;
    46.         GUI.enabled = _sprites.Count > 0;
    47.         if(GUILayout.Button(string.Format("Instantiate Sprites[{0}]", _sprites.Count), GUILayout.ExpandWidth(true)))
    48.         {
    49.             Selection.objects = _sprites.Select(s =>
    50.             {
    51.                 var newSprite = new GameObject(s.name ?? "sprite", typeof(SpriteRenderer)).GetComponent<SpriteRenderer>();
    52.                 newSprite.sprite = s;
    53.                 newSprite.transform.position = GetInstantiatePosition();
    54.                 newSprite.transform.parent = _parent;
    55.                 return newSprite.gameObject;
    56.             }).ToArray();
    57.         }
    58.         GUI.enabled = enabled;
    59.  
    60.         EditorGUILayout.BeginHorizontal();
    61.         if(GUILayout.Button("x", GUILayout.Width(20.0f)))
    62.         {
    63.             _parent = null;
    64.         }
    65.         _parent = (Transform)EditorGUILayout.ObjectField("Parent", _parent, typeof(Transform), true);
    66.         EditorGUILayout.EndHorizontal();
    67.  
    68.         EditorGUIUtility.labelWidth = labelWidth;
    69.     }
    70.  
    71.     private Vector3 GetInstantiatePosition()
    72.     {
    73.         if(SceneView.currentDrawingSceneView != null && SceneView.currentDrawingSceneView.camera != null)
    74.         {
    75.             var pos = SceneView.currentDrawingSceneView.camera.transform.position;
    76.             pos.z = 0.0f;
    77.             return pos;
    78.         }
    79.  
    80.         return Vector3.zero;
    81.     }
    82.  
    83.     private void UpdateSelection()
    84.     {
    85.         if(Selection.activeTransform != null)
    86.         {
    87.             _parent = Selection.activeTransform;
    88.         }
    89.         _sprites = Selection.objects.OfType<Sprite>().ToList();
    90.     }
    91. }
     
    kural_varman likes this.
  11. TarunL

    TarunL

    Joined:
    Jan 2, 2014
    Posts:
    17
    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 :(
     
  12. TarunL

    TarunL

    Joined:
    Jan 2, 2014
    Posts:
    17
    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?
     
  13. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    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!
     
    Mashimaro7 and almosr like this.
  14. TarunL

    TarunL

    Joined:
    Jan 2, 2014
    Posts:
    17
    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:
    https://answers.unity.com/questions/798312/fastest-way-to-assign-sprites-to-components-in-uni.html

    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.
     
  15. INKEDOUT

    INKEDOUT

    Joined:
    Sep 21, 2015
    Posts:
    1
    Hold down Alt when dragging ;)
     
  16. manhcuongit1010

    manhcuongit1010

    Joined:
    Apr 7, 2022
    Posts:
    1
    thanks, it is very useful and simple when not have to create a script