Coder Social home page Coder Social logo

Comments (6)

loodakrawa avatar loodakrawa commented on August 11, 2024

This answer assumes you're using the Unity plugin.

I suggest the most obvious approach would be modifying the SpriterImporter, more specifically the part where the assets get loaded (provided you can get a Sprite reference from your atlas):

https://github.com/loodakrawa/SpriterDotNet/blob/master/SpriterDotNet.Unity/Assets/SpriterDotNet/SpriterImporter.cs#L227

Also, I'll close this as it is not an actual issue (although we can continue this conversation). In the future, it would be better to ask such questions on the forum thread and open actual issues here.

from spriterdotnet.

treefortress avatar treefortress commented on August 11, 2024

Thanks for the pointer. I was able to get this working fairly easily. Here's a quick example of anyone else needs to do this:

private static T LoadContent<T>(string path) where T : UnityEngine.Object
        {
            T asset;

            //Try and load from TexturePacker
            var atlasPaths = new List<string> {
                "Assets/_Internal/Textures/Enemies/Enemies",
                "Assets/_Internal/Textures/Followers/Followers"
            };
            var imageSuffix = ".png";
            var splitPath = path.Split('/').ToList();
            var spriteNames = new List<string>();
            while (splitPath.Count > 0) {
                spriteNames.Add(string.Join("-", splitPath.ToArray()));
                splitPath.RemoveAt(0);
            }
            foreach (var p in atlasPaths) {
                var sprites = AssetDatabase.LoadAllAssetsAtPath(p + imageSuffix).OfType<Sprite>().ToList();
                var material = AssetDatabase.LoadAssetAtPath<Material>(p + ".mat");
                //Check each possible match, across each sprite
                foreach (var name in spriteNames) {
                    foreach (var s in sprites) {
                        if ((s.name + imageSuffix) == name) {
                            currentMaterial = material;
                            return s as T;
                        }
                    }
                }
            }

            asset = AssetDatabase.LoadAssetAtPath<T>(path);
            if (asset == null) Debug.Log("Missing Asset: " + path);

            return asset;
        }

The only trick, for our use case (Normal Map support) is that we still need to apply the matching material. So we store off currentMaterial in the LoadContent() function. and then apply it in CreateSprites()

 private static void CreateSprites(){
...
    var r = child.AddComponent<SpriteRenderer>();
    if (currentMaterial != null) {
        r.material = currentMaterial;
    }
}

from spriterdotnet.

treefortress avatar treefortress commented on August 11, 2024

Hmm, looks like I spoke too soon. Everything works great, until I close the editor and re-open. The scriptable object seems to lose all references to the sprites, and is just full of null entries...

from spriterdotnet.

treefortress avatar treefortress commented on August 11, 2024

Ok, figured it out. For some reason, the asset does not get marked as dirty, so the new sprites are not saved. Just had to add this to CreateSpriterData():
EditorUtility.SetDirty(data);

from spriterdotnet.

loodakrawa avatar loodakrawa commented on August 11, 2024

I'm glad you managed to get it working. Based on your code, I don't see a way of making this work in a generic way. Also, I'm not happy with the library user needing to change the importer to customise the behaviour. Probably something like a Chain of Command would be good here. Having a default way of getting assets and allowing users to customise this behaviour by plugging in custom importers. Something similar as with the parsers:

https://github.com/loodakrawa/SpriterDotNet/blob/master/SpriterDotNet/SpriterParser.cs#L28

What do you think?

from spriterdotnet.

treefortress avatar treefortress commented on August 11, 2024

Yep this sounds ideal, as long as we can plugin somehow, and swap the images with images from another texture in Unity, be it 2dToolkit or TexturePacker, or NGUI, they all work pretty much the same, they pack the sprites for you, and the end result is just a standard Unity Texture w/ sub sprites.

from spriterdotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.