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. Dismiss Notice

Question Create SpriteLibraryAsset by Scripts

Discussion in '2D' started by ChronoWalker, Sep 11, 2021.

  1. ChronoWalker

    ChronoWalker

    Joined:
    Sep 24, 2017
    Posts:
    6
    Hello, I'm trying to create a SpriteLibraryAsset pipeline, since creating one by assigning sprites to each category is really time consuming. It would be a lot more easier if I can just create sprite library assets and add sprites into the category according to sprite name or folder name it belongs to.

    Here's what I tried:
    Code (CSharp):
    1. public class SpriteLibraryAssetFactory : EditorWindow
    2. {
    3.     [MenuItem("Window/Example")]
    4.     public static void ShowWindow()
    5.     {
    6.         GetWindow<SpriteLibraryAssetFactory>();
    7.     }
    8.  
    9.     private void OnGUI()
    10.     {
    11.         if (GUILayout.Button("Create"))
    12.         {
    13.             var sprites = ResourcesFolderLoader.LoadAllSpritesInDefault();
    14.            
    15.             var asset = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
    16.             foreach (var sprite in sprites)
    17.             {
    18.                 asset.AddCategoryLabel(sprite, "test", "test2");
    19.             }
    20.             AssetDatabase.CreateAsset(asset, "Assets/test.spriteLib");
    21.             AssetDatabase.SaveAssets();
    22.            
    23.         }
    24.     }
    25.  
    26. }
    27.  
    If I try to use AssetDatabase.CreateAsset(so, "Assets/test.asset"); to generate ScriptableObject, this is fine. And since SpriteLibraryAsset is derived from ScriptableObject, I suppose this should work. However, I encountered these error:

    Question now is what is the correct way to create SpriteLibraryAsset by code. Any documentation, examples or explanations is appreciated! Thanks.
     
    tonytopper and Gaidzin like this.
  2. adunster

    adunster

    Joined:
    Jul 5, 2021
    Posts:
    4
    Did you get anywhere with this? I'm trying to get started on a similar project myself.
     
  3. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Currently we do not support creating or modifying a Sprite Library Asset during edit time.
    SpriteLibraryAsset.AddCategoryLabel
    is there so that you can add a Sprite into the SpriteLibraryAsset during runtime.

    What are you trying to solve by creating a Sprite Library Asset at edit time? Could you describe how our current flow doesn't work for you?
     
  4. adunster

    adunster

    Joined:
    Jul 5, 2021
    Posts:
    4
    My current setup for a top down RPG has a sprite library asset used to have matching animations across a variety of animations. The player character sprites, the clothing/armor sprites, the weapon sprites, the npc sprites, all each have their own sprite library asset with identical categories and all use the same animator, so I only have to set up a type of animation once. I expect to have quite a few different character sprites and a much higher quantity of different npcs, enemies, different kinds of armor and clothes, etc, and while I can manually create a sprite library asset for each one with a preset, adding a couple dozen sprite slices to the correct slots in the library is time consuming and error prone. Ideally, I'd like to have an automated workflow such that I can drop a sprite image into a watched directory, and on import have it automatically sliced and create a new library (if one doesn't exist) and add its sprites to the correct categories in the correct order, based on a consistent slicing layout and filename conventions. Then I can just create a new instance of a prefab and apply the different library to it and all the animations just work.
     
    SudoCat, BaspooGameDev and Gaidzin like this.
  5. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Thank you for walking me through how you work with your project and your needs in how to make it more convenient to iterate on. I'll take this feedback with me when we are planning future work for the 2D Animation package.
     
    adunster likes this.
  6. BaspooGameDev

    BaspooGameDev

    Joined:
    Oct 3, 2016
    Posts:
    11
    :) me too ....
     
  7. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    @BaspooGameDev does your setup and requirements match that of adunster? or do you have some other setup/requirements we should know about?
     
  8. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    I am also trying to use AddCategoryLabel to create, expand, and evolve libraries as new assets are imported. We are trying to use naming conventions to specify which library and category a sprite goes into so we can then use sprite swapping for different eyes, hairstyles, clothing, etc.

    We are navigating a ton of complexity here. For instance, when a hairstyle changes I need to swap many sprites at once since I have several views of character, front, back, side, etc. And each hairstyle might have a foreground a background sprite with each view.

    I keep encountering things within Unity that just seem to be developed in a way that doesn't anticipate an iterative, prototyping mindset. If I can do it in the editor, I should be able to do it in code, right? I find it REALLY odd that this isn't the case here.
     
    dmitry_veshchikov likes this.
  9. dmitry_veshchikov

    dmitry_veshchikov

    Joined:
    May 15, 2021
    Posts:
    1
    I too have this issue, and wish there was a way to save the SpriteLibraryAsset created in the edit-time to my Assets folder.

    I have many different player character sprites, which use the same animation. So in order not to drag and drop >100 individual sprites, I wanted to create a script that runs during edit-time, which creates the SpriteLibraryAsset for each character, however even if there was functionality to save it in edit-time, I can't find the "Main Library" attribute, which would allow animations to be reused...

    Would greatly appreciate if there was code support for saving the SpriteLibraryAsset in edit-time into Assets folder, as well as setting the "Main Library" attribute through code.
     
    tonytopper likes this.
  10. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Thank you @tonytopper and @dmitry_veshchikov, it is great to know your use cases so we can more easily evaluate it against other feature requests that come in to us. I'll add these items to our list of suggestions for future improvements to the 2D feature set.
     
    tonytopper likes this.
  11. ScionOfDesign

    ScionOfDesign

    Joined:
    Oct 6, 2016
    Posts:
    82
    This is also a feature that would make our lives easier. I am currently building a rig generator to streamline the rig creation process for our artists and designers and would like to automatically generate and fill the sprite libraries based on a regex match of sprite assets.

    We've already modified the 2D animation package (the new warning about an invalid signature in packages should be able to be disabled) to add in things like skeleton inheritance (another feature that would be useful) and expose a lot of the useful internals to the normal editor.

    The new asset mapped to .spriteLib is an internal SpriteLibrarySourceAsset, so in the meantime (if you're willing to use reflection or modify the package) you can use that. The SpriteLibUpgrader has some code showing how to serialize a new SpriteLibrarySourceAsset.
     
  12. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    So in the past few days I've been talking with my colleagues about the history and API changes of Sprite Library and its friends. I'll write down some insights here to correct some of my previous statements and also to update our collective perception of the feature.

    So in Unity we have two types of assets, "source assets" such as .psd, .png and fbx; and "runtime assets" such as Texture, Sprite and Mesh. When a source asset is imported into Unity, an importer associated with the file ending of the source asset takes over to read the content of the file and its meta file, and generates one or more runtime assets. For a .png it looks something like this:
    A .png is added into a Unity project -> Texture Importer reads the .png and its .meta file -> A Texture asset (and sometimes Sprite assets) is generated.

    In Unity 2021.1, we updated Sprite Library Asset to follow the same design. We introduced the .spriteLib as Sprite Library's source asset, and kept the Sprite Library Asset as its runtime asset.

    The benefit of this design mainly comes down to decoupling the source asset from the runtime asset. With this decoupling, we are able to:
    - Modify the data without touching the source asset data (e.g. apply compression and remove unnecessary data not needed in runtime).
    - Allowing different source assets to generate the same runtime assets (e.g. .png, .psd and .tga all have a Texture generated on import by the TextureImporter).
    - Allowing the same source assets to generate different runtime assets (e.g. a .psd can be processed by the TextureImporter or the PSD Importer, which will generate a different set of assets).

    One common API structure these pipelines share is that Unity tends to not have an API in place to generate source assets, but has an API in place to generate runtime assets. E.g. a Texture can be generated by creating a new Texture2D object, a Mesh can be generated by creating a new Mesh object and a Sprite can be generated by using the Sprite.Create method. The same thing goes for a Sprite Library Asset. Here is some code to showcase how you could create a new Sprite Library Asset and save it on disk:

    Code (CSharp):
    1. public static class SpriteLibCreator
    2. {
    3.     [MenuItem("Tools/Create Sprite Lib")]
    4.     static void CreateLib()
    5.     {
    6.         const string spriteLibName = "MySpriteLib.asset";
    7.            
    8.         var spriteLib = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
    9.         spriteLib.AddCategoryLabel(null, "Cat", "Label");
    10.            
    11.         AssetDatabase.CreateAsset(spriteLib, "Assets/" + spriteLibName);
    12.     }
    13. }
    You could also go more advanced, and create your own source asset with its unique file ending and create a custom importer for it. This would allow you to author Sprite Library data outside (or inside) of Unity and then generate it exactly how you would like it.

    During my dive into this area, I saw that
    SpriteLibraryAsset.CreateAsset
    is internal. To follow the same design as the other pipelines, I believe we should make this a public method to ease the way a Sprite Library Asset can be generated.

    Apart from this change, are there any other changes to the Sprite Library Asset API that you would like to see?
     
    tonytopper and assertor like this.
  13. ScionOfDesign

    ScionOfDesign

    Joined:
    Oct 6, 2016
    Posts:
    82
    I understand the pattern you're going for, but the difference between the SpriteLibrarySourceAsset's and, say, a model or a PNG is that SpriteLibrarySourceAsset is unique to Unity. There are plenty of ways to create, edit, modify and import PNG's, or even models, in code. Simply moving them into the project directory and updating the asset database lets you load/reload them. Effectively, modifying these types of source assets is not 'internal' functionality. Unity does not need to provide an API for these files because plenty of API's already exist.

    To only way to achieve a similar result with the SpriteLibrarySourceAsset (without breaking open the internals as we did) is to save a new file based on an existing template. The example code you gave creates an asset that prompts the user to "Upgrade" to the new type of SpriteLibrary when it is clicked on. This experience is not ideal.

    SpriteLibrarySourceAsset is itself a ScriptableObject, ScriptableObjects are most easily edited through Unity's codebase, so keeping it internal with no API to create or edit instances of this source asset is artificially limiting in ways that the other types of source assets are not.

    If what you're ultimately trying to do is totally prevent users from creating SpriteLibrarySourceAsset's at runtime, then a simple editor-only API with SpriteLibraryAsset that lets users stage and then "Commit" changes to it would work. This is essentially what your editor code already does when upgrading assets.
     
  14. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Thank you for your feedback.

    Yes, this text should be updated so that it doesn't seem like a user is doing something wrong by having these .asset files in their project.

    If we zero in on what you are doing with the sprite libraries in edit time, @Filiecs, apart from the label being displayed on a Sprite Library Asset, what issues do you have with the path I outlined in my post?
     
  15. ScionOfDesign

    ScionOfDesign

    Joined:
    Oct 6, 2016
    Posts:
    82
    Functionality-wise? Not much, other than the fact that the user may give up whatever performance benefits there are of using SpriteLibrarySourceAsset instead.

    But at its core it seems very arbitrary and inconsistent that there needs to be a difference between user-generated Sprite Libraries from code and Libraries generated with a click of a button in the Unity Editor. Maybe I just have not encountered them yet, but I am unaware of any other asset in Unity behaving the same way. (Having the source asset be a ScriptableObject and preventing the user from creating said ScriptableObject).

    If we ever decided to release an asset store package of the powerful rig generator we are building, this would just be another annoying issue to work-around using reflection or some other unnecessarily hacky method.
     
  16. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    I might not have explained the relationship between the Sprite Library Source Asset (.spriteLib) and Sprite Library Asset clearly in my previous post.

    The Sprite Library Source Asset class is the code representation of the .spriteLib file. Once a .spriteLib file is imported into a Unity project, the Sprite Library Source Asset Importer opens the file (and its meta file), reads its content and generates a Sprite Library Asset. The Sprite Library Asset is then used in runtime for any Sprite Swap operation the project is performing. This means that the runtime performance is identical if you start off with a .spriteLib file (containing Sprite Library Source Asset data) or an .asset file (containing Sprite Library Asset data).

    Hopefully this clears things up a bit more.
    Thanks again for your feedback!
     
  17. ScionOfDesign

    ScionOfDesign

    Joined:
    Oct 6, 2016
    Posts:
    82
    That makes the proposed solution better, but also begs the question of why SpriteLibrarySourceAsset's were even needed in the first place?
    If SpriteLibrarySourceAsset led to increased performance and functionality, then the user should have the ability to create them. In my opinion, this includes the behavior of not committing any changes immediately. (Speaking of, can you even permanently modify a SpriteLibrarySourceAsset-backed SpriteLibrary with the existing API? Or will no changes ever be written to disk?).
    If SpriteLibrarySourceAsset doesn't lead to increased performance and functionality, then why does it exist except to cause confusion when users try to create a SpriteLibrary asset?

    Is it simply to conform to a design pattern used within Unity that may lead to changes in some future update? Is it to prevent unintended modification of the serialized asset?

    I can definitely see there being a lot of user confusion between libraries saved as .asset and libraries saved as .spriteLib. Especially if you are working on a large team with some artists creating libraries from the editor and others using a custom tool to generate them.
     
  18. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    There are multiple reasons for this design choice, some of which I highlighted in my previous post:
    Another reason is of course to conform with Unity's import pipeline. This makes current and future work easier to perform.

    Not sure what you mean with a SpriteLibrarySourceAsset-backed SpriteLibrary. Again, if a change is made to the Sprite Library Source Asset and a reimport of the file is trigged, the Sprite Library Source Asset Importer will generate a new Sprite Library Asset based on the content of the Sprite Library Source Asset (much like if you make a change inside the Sprite Editor and press Apply, the TextureImporter will generate updated Sprite assets). The APIs to modify the Sprite Library Source Asset is currently internal.

    All in all, it is never our intention to create confusing APIs, we are constantly trying to make sure we have simple yet extensible APIs for our feature sets. For this feature set, it does indeed seem like there is quite some confusion. Moving forward, what would help us is to understand your real use cases, and where the current feature set/API offerings doesn't work/is confusing. This way we can gather insights from multiple sources and make sure we provide the best solution for most users.
     
  19. ScionOfDesign

    ScionOfDesign

    Joined:
    Oct 6, 2016
    Posts:
    82
    If you save a SpriteLibrary like you showcased in your SpriteLibCreator code, it this is what it looks like in the editor:
    upload_2022-2-18_16-27-9.png
    Notice how it is simply a "Sprite Library Asset". It is not a "SpriteLibrarySource Asset imported as a SpriteLibraryAsset" like what happens when you create a new SpriteLibrary through the menu, as seen here:
    upload_2022-2-18_16-29-33.png

    The regular "SpriteLibraryAsset" does not have the "Main Library/Override" functionality of the SpriteLibrarySourceAssets either.

    We want to be able to create a tool that generates sprite library assets and fills them up with sprites at editor time. We want the Sprite Libraries created through our code to be no different than the sprite libraries created by clicking the menu in the editor. We want to be able to do this without breaking into the Animation package and forcing it to give us access to its internals.
     
  20. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Thanks for the feedback, @Filiecs. I'll add this to our internal list of feedback for the 2d feature sets.
     
  21. George-C983

    George-C983

    Joined:
    May 12, 2013
    Posts:
    1
    Sorry to necro this post, but just to add to the already large number of users discussing this, I have over 300 individual Textures all with identical signatures (12 sub-asset sprites with identical dimensions). All sprites should have an associated Sprite Library Asset that is populated with each sprite so that a Sprite Resolver can be used to animate these characters without the need to create new animations for each and every sprite.

    To speed up the creation, all I'm looking to do is once I click a button in a custom editor I have created, it takes the selected Texture2D that matches all of the above conditions, creates a new Sprite Library Asset, sets the main library asset to one I have set up, populates each category with the corresponding sprites and saves it. From the outset it sounds like a simple task and one that I would have expected to have been possible given the moddability of the Unity Editor.

    I understand the need to have separation between the runtime assets and the source assets/importers, but I'm not sure I follow this arbitrary internal decision to lock off the entire class and make it impossible to create workflow, time saving automation tools, because after reading through this topic all that has become apparent to me is that the official response is, "get used to it, enjoy the monotonous task of manually creating child library assets for each of your sprites!"
     
  22. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Hello @George-C983,
    Thank you for chiming in. I think you are adding a bit too much of your own flavour when saying that the official word is "to get used to it". That hasn't been stated anywhere in this thread.

    We are getting many feature requests and improvement suggestions for all of the different tools and APIs in the editor and runtime. We cannot implement everything, as some feedback are polar opposite of other pieces of feedback, some are very user specific, etc. So what's important for us is to understand what the user is trying to do. Once we know this, we can research different ways we can make the user achieve this goal, to align with our own design thoughts and other pieces of feedback. This is why threads like these are important. We get to hear what users are trying to achieve, but have a hard time realizing. We can share our insight onto why the feature/tool is in the state it is currently in. Sometimes additional info on how something works is all that is needed in order to resolve the issue. Sometimes we see that a feature/tool is lacking in some aspect.

    Going back to your post, thank you for letting us know how you would like to use the Sprite Library Source Assets. It is great to know that more users would like to have the ability to create Sprite Library Source Assets through an API. We'll take your feedback and log it as well, @George-C983

    Finally I have some news that will make it into an official post any time now, in Unity 2022.2 we have created a new editor to make it easier to work with Sprite Library Source Assets. If you are feeling adventurous, do give Unity 2022.2 and 2D Animation 9.0.0-pre.1 a spin, and let us know your thoughts! A word of caution though, this editor is currently in an alpha state.
     
    SudoCat likes this.
  23. GameChangerNow

    GameChangerNow

    Joined:
    Nov 26, 2018
    Posts:
    3
    So the reason why many people want this interface is because:
    Imagine we have hundred PSB files, each contain a character which has same body parts with same names and share a same skeleton. So if we could have this interface, then we don't have to add those sprites manually and copy all the same names, that my friend, would save countless time.

    So we would love to have this interface for save the change to SpriteLibraryAsset Source File by editor scripts.

    Here is what I have tried this afternoon(Wish I know that sooner so I don't have to spent who afternoon try to find the solution...:():
    So after calling
    Code (CSharp):
    1. SpriteLibraryAsset.AddCategoryLabel
    and
    Code (CSharp):
    1. SpriteLibrary.AddOverride
    and then calling:

    Code (CSharp):
    1.         void SaveSpriteLibraryAsset(SpriteLibraryAsset spriteLibraryAsset)
    2.         {
    3.             EditorUtility.SetDirty(spriteLibraryAsset);
    4.             AssetDatabase.SaveAssets();
    5.             AssetDatabase.Refresh();
    6.         }
    7.         void SaveOnDisk(SpriteLibraryAsset spriteLibraryAsset)
    8.         {
    9.             // MyScriptableObject : ScriptableObject
    10.             SpriteLibraryAsset asset = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
    11.             AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath("Assets/" + "1.asset"));
    12.             AssetDatabase.SaveAssets();
    13.             AssetDatabase.Refresh();
    14.             EditorUtility.SetDirty(asset);
    15.         }
    And SpriteLibraryAsset Source File is not modified, now I know why, because it's not supported.....

    Anyway, have a good day sir, hope this kind of interface could be added in the future update. Cheers!:D
     
  24. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Just so we are on the same page, if your goal was to create and edit a Sprite Library Asset, you can do that using the following code:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.U2D.Animation;
    4.  
    5. public static class SpriteLibraryUtilities
    6. {
    7.     [MenuItem("Tmp/Create Sprite Lib")]
    8.     static void CreateSpriteLib()
    9.     {
    10.         var asset = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
    11.         AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath("Assets/" + "1.asset"));
    12.         AssetDatabase.SaveAssets();
    13.         AssetDatabase.Refresh();
    14.         EditorUtility.SetDirty(asset);      
    15.     }
    16.    
    17.     [MenuItem("Tmp/Add category to Sprite Lib")]
    18.     static void AddCatToSpriteLib()
    19.     {
    20.         var objs = Selection.objects;
    21.         foreach (var obj in objs)
    22.         {
    23.             if (obj is SpriteLibraryAsset spriteLib)
    24.             {
    25.                 var randomCategory = GUID.Generate().ToString();
    26.                 var randomLabel = GUID.Generate().ToString();
    27.                 spriteLib.AddCategoryLabel(null, randomCategory, randomLabel);
    28.                
    29.                 EditorUtility.SetDirty(spriteLib);
    30.                 AssetDatabase.SaveAssets();
    31.                 AssetDatabase.Refresh();
    32.             }
    33.         }
    34.     }
    35. }
    36.  
    But valid note on wanting to create Sprite Library Source Assets from an editor script. I have noted down all your feedback so that we can take it into account when planning future 2D Animation iterations. Thanks for the feedback!
     
    SudoCat likes this.
  25. BlitzkriegGames

    BlitzkriegGames

    Joined:
    Oct 16, 2016
    Posts:
    6
    @Ted_Wikman
    Does this actually work though? If I use this code but to add sprites to an existing category in an existing sprite library asset, the UI in the inspector doesn't refresh to show the sprite is there. I can see it is there (temporarily) in code though. But as soon as I restart Unity the sprite is gone as if I never added it via code.

    Code (CSharp):
    1. spriteLibrary.AddCategoryLabel(sprite, category, id);
    2. spriteLibrary.AddCategoryLabel(icon, category, $"{id}_icon");
    3. EditorUtility.SetDirty(spriteLibrary);
    4. AssetDatabase.SaveAssets();
    5. AssetDatabase.Refresh();
     
    Last edited: Oct 24, 2022
  26. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    So I just tried the script out again, and it works as intended. Could you walk me through your steps?
    Also, do note that this script works on Sprite Library Assets (.asset) and not on Sprite Library Source Assets (.spritelib).
     
  27. BlitzkriegGames

    BlitzkriegGames

    Joined:
    Oct 16, 2016
    Posts:
    6
    I guess I don't know the difference other than the prefix. I just created them using Assets > Create > 2D > Sprite Library Asset. That creates a .spritelib so I guess I'm using the wrong one? How do I create the .asset and will it let me do the same thing as the .spritelib?

    Or am I just using the wrong type in code?
    Code (CSharp):
    1. var spriteLibrary = Resources.Load<SpriteLibraryAsset>($"{category}SpriteLibrary");
     
    Last edited: Oct 25, 2022
  28. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Further up this thread I explain the differences:
    In the script I shared, there are two static method, one to create a Sprite Library Asset, and one to add Categories to a selected Sprite Library Asset.
     
  29. BlitzkriegGames

    BlitzkriegGames

    Joined:
    Oct 16, 2016
    Posts:
    6
    So what can I do to make this work? If I create a sprite library asset through the inspector it creates the .spritelib file, not an asset file. I have multiple sprite libraries that all contain a decent number of sprites. Hopefully I don't have to redo them all.
     
  30. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    I would urge you to read the thread above, as your question has already been asked by other users.
    But the summary is: right now we do not have a public API to modify a Sprite Library Source Asset (.spritelib). We understand that this is something developers would like to do, so we have added it to our backlog for things to consider in future versions. Until then, your only options are to either modify the 2D Animation package, to give public access to the classes and methods you wish to use, or use the Sprite Library Assets (.asset).
     
  31. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    Can we modify .spriteLib files with a script yet?

    If the Sprite Library Editor exists we should be able to do this. But I don't see any available APIs. I need this.

    The difference between these two asset types is still not apparent in the Editor. Only by being involved in this thread would someone know this. .spriteLib files are even labeled "(Sprite Library Asset)" in the Editor.

    A Sprite Library Asset (.asset) doesn't feel like other .asset files. When I change an asset I expect it to persist in the AssetDatabase. I guess it does, but it only feels to be persisting in that Sprite Library component. Also, assets are supposed to live in the Project Window, but I only see the .spriteLib asset there.

    This is REALLY CONFUSING and frustrating.
     
  32. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    @tonytopper, I understand that it might be confusing, as the 2D Animation package was initially using .asset and later switched to .spriteLib Sprite Library Assets.

    We added a Sprite Library Editor to help users to create and author their sprite libraries. However, as mentioned above, currenly we do not have any APIs that would allow you to create Sprite Library (.spriteLib) assets.

    To help you out with this, I've written a script that allows you to save a Sprite Library Asset as .spriteLib to your project. You can find it here. Please keep in mind that this is not an official solution, and didn't go through our QA process and is provided "as is".

    After adding it to your project (Editor folder), you should be able to call:
    Code (CSharp):
    1. var savedAsset = SpriteLibraryAssetHelper.SaveAsSpriteLibrarySourceAsset(myAssetToSave, "Assets/MySpriteLibrary.spriteLib");
    If you need to create a Sprite Library that "inherits" entries from another library, you should be able to call:
    Code (CSharp):
    1. var savedAsset = SpriteLibraryAssetHelper.SaveAsSpriteLibrarySourceAsset(myAssetToSave, "Assets/MySpriteLibrary.spriteLib", mainLibrary);
    Please let me know if this is helpful.
     
    tonytopper likes this.
  33. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    Thanks so much!

    I'll give this a try. This could be a nice stopgap.

    Also worth noting is that scripted Sprite Library component changes persist through Domain Reloads for me but not through Editor closing and opening. This is another bizarre behavior for me. This will be less of an issue once I can save a .spriteLib file but I still may want to have differences in the local Sprite Library component.

    This system really could use another round of improvement soon.
     
  34. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    Also, whoever created this framework should really get some mentorship on developer UX and/or API design. Either that or be given the time to focus on DevX more. Several obvious improvements if you take the effort/focus to polish this.
     
  35. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    I can reproduce that behaviour. To make the editor detect the change, you'd have to call EditorUtility.SetDirty() on your Sprite Library component after your modifications. Let me know if it helps.
     
  36. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    I do agree that this system would benefit from several improvements, and we are aware of it. However, very often there are areas where we can deliver more value and decide to allocate our development resources there. It's also important to note, that this system was designed for non-coders and the APIs were not the main path for creating sprite swaps.

    Please, keep the constructive feedback coming. I'm registering it and sharing it with the rest of the team so that we can direct our efforts effectively in the future.
     
    tonytopper likes this.
  37. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    Hmm ... looks like I am currently calling SetDirty on my Sprite Libary Asset (.asset) and not the Sprite Library component.

    I'll give the Sprite Library a try, thank you.
     
    MarekUnity likes this.
  38. C0lonnello

    C0lonnello

    Joined:
    Nov 7, 2022
    Posts:
    24
    I'm answering in the thread because I'm searching for quite the same thing here:

    That is exactly what I'm searching for, since at the moment I can do all of this using Sprite Library but I have to drag n drop every single frame on at a time. It would be good just to be able to select multiple of them and drag them in a category in order to swap every sprite coming from the "master/template" library with the ones I selected.
     
    switchCTRL likes this.
  39. switchCTRL

    switchCTRL

    Joined:
    Dec 19, 2021
    Posts:
    4
    This is also my use case. I have to drag each sprite one by one in the editor which takes forever. Also the list view in the "Labels" view is partially broken and does not detect changes if it's in list mode. I tried the last two days to write a replacement where I can drag multiple sprites at once (40 labels -> 40 sprites and they would assign one by one) and now I found this thread sayin one cannot save a spritelib through code ;(
    It would be really nice if we can either drag multiple sprites or modify the libs in code.
     
    Last edited: Jul 2, 2023
  40. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    While the helper class above is a usable stopgap, I am finding it to be far from ideal.

    We need a better API for this, people need to be able to selectively save one Sprite at a time to the source asset via script.

    This feature set needs a good bit more evolution in my opinion.

    For example, look at this UI for the Sprite Library component.

    upload_2023-8-22_10-7-37.png
     
    MuntyMcFly likes this.
  41. choijaeyoung

    choijaeyoung

    Joined:
    Feb 8, 2020
    Posts:
    12
    Is 2D Animation's "modify Sprite Library Asset through code" something being worked on or currently available (9.0.4)? I don't mind manually creating the sprite library asset scriptable object, but I would really appreciate if I can modify the contents of the sprite library asset through code.
     
    tonytopper likes this.