Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to copy the result of Sprite Editor from one atlas to another???

Discussion in '2D' started by Vugar-Naib, Sep 27, 2015.

  1. Vugar-Naib

    Vugar-Naib

    Joined:
    Aug 28, 2015
    Posts:
    8
    How to copy the result of Sprite Editor from one atlas to another similar atlas with the same names and Pivot coordinates? (see Figure)
     

    Attached Files:

  2. Kuan

    Kuan

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    87
    Hey Vugar,

    I afraid there is no way to do that now. To speed up the pivot setup, you can define the pivot before you slice your texture. Then you can have the same pivot for all the sliced sprite in one atlas. But not across 2 different atlas.
     
  3. Deleted User

    Deleted User

    Guest

    You can't directly from Unity, but can trough a custom Editor script. Just made one as I need this kind of feature too:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections.Generic;
    5.  
    6. //Copy and paste atlas settings to another atlas editor
    7. public class AtlasCopyPasteEditor : EditorWindow
    8. {
    9.  
    10.    public Texture2D copyFrom;           //Sprite Atlas to copy from settings
    11.    public Texture2D pasteTo;           //Sprite atlas where to paste settings
    12.  
    13.    private Sprite[] _sprites;           //Collection of sprites from source texture for faster referencing
    14.  
    15.    [MenuItem ("Window/Atlas CopyPaste Editor")]
    16.    static void Init()
    17.    {  
    18.      // Window Set-Up
    19.      AtlasCopyPasteEditor window = EditorWindow.GetWindow(typeof(AtlasCopyPasteEditor), false, "Atlas Editor", true) as AtlasCopyPasteEditor;
    20.      window.minSize = new Vector2(260, 170); window.maxSize = new Vector2(260, 170);
    21.      window.Show();
    22.    }
    23.  
    24.    //Show UI
    25.    void OnGUI ()
    26.    {
    27.  
    28.      copyFrom = (Texture2D)EditorGUILayout.ObjectField ("Copy From", copyFrom, typeof(Texture2D), true);
    29.      pasteTo = (Texture2D)EditorGUILayout.ObjectField ("Paste To", pasteTo, typeof(Texture2D), true);
    30.  
    31.      EditorGUILayout.Space ();
    32.  
    33.      if(GUILayout.Button ("Copy Paste"))
    34.      {
    35.        if(copyFrom != null && pasteTo != null)
    36.          CopyPaste ();
    37.        else
    38.          Debug.LogWarning("Forgot to set the textures?");
    39.      }
    40.  
    41.      Repaint ();
    42.    }
    43.  
    44.    //Do the copy paste
    45.    private void CopyPaste ()
    46.    {
    47.      if(copyFrom.width != pasteTo.width || copyFrom.height != pasteTo.height)
    48.      {
    49.        //Better a warning if textures doesn't match than a crash or error
    50.        Debug.LogWarning("Unable to proceed, textures size doesn't match.");
    51.        return;
    52.      }
    53.  
    54.      if(!IsAtlas (copyFrom))
    55.      {
    56.        Debug.LogWarning ("Unable to proceed, the source texture is not a sprite atlas.");
    57.        return;
    58.      }
    59.  
    60.      //Proceed to read all sprites from CopyFrom texture and reassign to a TextureImporter for the end result
    61.      UnityEngine.Object[] _objects = AssetDatabase.LoadAllAssetRepresentationsAtPath (AssetDatabase.GetAssetPath (copyFrom));
    62.      
    63.      if(_objects != null && _objects.Length > 0)
    64.        _sprites = new Sprite[_objects.Length];
    65.  
    66.      for (int i = 0; i < _objects.Length; i++)
    67.        _sprites [i] = _objects [i] as Sprite;
    68.  
    69.      //Get Texture Importer of pasteTo texture for assigning sprite variables (pixxelsToUnit is not counted)
    70.      string _path = AssetDatabase.GetAssetPath(pasteTo);
    71.      TextureImporter _importer = AssetImporter.GetAtPath(_path) as TextureImporter;
    72.    //   _importer.isReadable = true;
    73.  
    74.      //Force settings to Sprite and Multiple just to be sure
    75.      _importer.textureType = TextureImporterType.Sprite;
    76.      _importer.spriteImportMode = SpriteImportMode.Multiple;
    77.  
    78.      //Reassigning to new atlas
    79.      List<SpriteMetaData> _data = new List<SpriteMetaData>();
    80.  
    81.      for(int i = 0; i < _objects.Length;  i++)
    82.      {
    83.        SpriteMetaData _meta = new SpriteMetaData();
    84.        _meta.pivot = _sprites[i].pivot;
    85.        _meta.name = _sprites[i].name;
    86.        _meta.rect = _sprites[i].rect;
    87.        _meta.border = _sprites[i].border;
    88.        
    89.        _data.Add(_meta);
    90.      }
    91.  
    92.      //Add MetaData back to spriteshet from List to Array
    93.      _importer.spritesheet = _data.ToArray();
    94.  
    95.      //Rebuild asset
    96.      AssetDatabase.ImportAsset(_path, ImportAssetOptions.ForceUpdate);
    97.    }
    98.  
    99.    //Check that the texture is an actual atlas and not a normal texture
    100.    private bool IsAtlas (Texture2D tex)
    101.    {
    102.      string _path = AssetDatabase.GetAssetPath(tex);
    103.      TextureImporter _importer = AssetImporter.GetAtPath(_path) as TextureImporter;
    104.  
    105.      return _importer.textureType == TextureImporterType.Sprite && _importer.spriteImportMode == SpriteImportMode.Multiple;
    106.    }
    107. }
    108.  
    Just put the script in the Editor folder and you should see the tool under Window/Atlas CopyPaste Editor on the top menu of Unity.

    You will get some warning if something goes wrong, like unmatching texture sizes or the source texture not beign a Multiple sprites atlas. For the rest feel free to modify as you see fit.
     
  4. Vugar-Naib

    Vugar-Naib

    Joined:
    Aug 28, 2015
    Posts:
    8
    Thank you very much Neurological!!! Necessarily use, and share experiences!!!
    Cheers!!! =)
     
    holliebuckets likes this.
  5. Vugar-Naib

    Vugar-Naib

    Joined:
    Aug 28, 2015
    Posts:
    8
    Neurological!!! You are best!!! Thanks dude!!! =)
     

    Attached Files:

    holliebuckets likes this.
  6. Deleted User

    Deleted User

    Guest

    You are welcome, glad it helped.
     
  7. Loomabox

    Loomabox

    Joined:
    Nov 4, 2015
    Posts:
    46
    Really cool tool!!! Neurological bravo!!! =)
    By the way I have earned after I put it also in the my unity Project > Assets > Scripts folder.
     
  8. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    924
    Still useful and working in 19/02/2017, thanks @Neurological ! :p:p:p
     
  9. WOTAGaming

    WOTAGaming

    Joined:
    Mar 7, 2018
    Posts:
    1
    TY @Neurological
    One tip - destination texture have to be a Default Texture.

    Hmm. Somehow pivots are not copy and stay centered.
    Even if I turn pivots from pixels to relative 0..1
     
    Last edited: Jan 10, 2021
  10. mikeamer007

    mikeamer007

    Joined:
    Jan 3, 2014
    Posts:
    1
    Here the fix for the pivot and all the other data.
     

    Attached Files:

  11. Seatown

    Seatown

    Joined:
    Oct 12, 2018
    Posts:
    2
    Amazing stuff, this thread saves me so much time. Thank you, Thank you!
     
  12. arora2deepak

    arora2deepak

    Joined:
    Apr 22, 2021
    Posts:
    1
    Last edited: Apr 25, 2021
  13. AnOrdinarySandwich

    AnOrdinarySandwich

    Joined:
    Feb 9, 2017
    Posts:
    9
    There's another option for duplicating sprite editor settings for sprites which will have the exact same settings. It doesn't require any additional scripting, but it takes a little time for renaming maintenance.
    1) Outside of unity (or in Unity), add only one of the sprites to your project.
    2) In Unity, configure the sprite and its editor settings as you desire them all to be.
    3) In Unity, duplicate that sprite and rename the duplicates as the names of all additional sprites desired (so its still just a duplicate but its name matches that of another sprite to be added).
    4) Outside of unity, replace each of those duplicate sprites with the actual image for the name (so each duplicate in step 3 will be replaced the actual image).
    5) Give Unity focus. Now, all your sprites have the exact same sprite editor settings!

    This is the technique I use when adding a bunch and its handy.
     
    timofey_iam likes this.
  14. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    664
    Thanks!!!
     
  15. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    269
    attention!
    never use #10 version
    this will cause a very huge bug that the new sprite will have same internalID(in meta file) with old sprite and none spriteID(meta file too).
    this will cause missing sprite.

    #3 version editor is great without any problem.
     
    hippogames likes this.
  16. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
    I was using this and now I've got ruined references after migrating from 2020 to 2021 =((((
     
  17. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228