1
\$\begingroup\$

I'm using a sprite atlas and Unity's built-in sprite editor to tease out the individual images. Specifically, I have a single texture containing lots and lots of icons for armor, and I am assigning those to a Sprite[] field of a Component.

However, the process of manually assigning sprites to the component -- via drag and drop -- is tedious. Is there a faster way to do this? Perhaps a way I can bulk assign sprites to a component?

Screen shot of what I'm describing


Update: This is the code of the final solution. In short, the easiest thing to do is to put the texture atlas in the /Resources folder and then programmatically access the sprites by name.

private void MergeTextureAtlas(string atlasName, Dictionary<ItemSubType, List<Sprite>> spriteLookup) {
    Sprite[] atlasSprites = Resources.LoadAll<Sprite>(atlasName);
    Assert.IsNotNull(atlasSprites, atlasName);
    Assert.IsTrue(atlasSprites.Length > 0, atlasName);

    int spritesLoaded = 0;
    foreach (ItemSubType itemSubType in Enum.GetValues(typeof(ItemSubType))) {
        string subTypeFriendlyName = ItemSubTypeHelper.GetSubTypeName(itemSubType);
        foreach (Sprite atlasSprite in atlasSprites) {
            if (atlasSprite.name.Contains(subTypeFriendlyName)) {
                if (!spriteLookup.ContainsKey(itemSubType)) {
                    spriteLookup.Add(itemSubType, new List<Sprite>());
                }
                spriteLookup[itemSubType].Add(atlasSprite);
                spritesLoaded++;
            }
        }
    }

    if (spritesLoaded == 0) {
        Debug.LogWarning(String.Format(
            "Found 0 new sprites in texture atlas '{0}'.",
            atlasName));
    }
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The fastest / easiest way is to do it programmatically.

What you seem to do with weapon sprites is usually something that can be achieved via data driven programming. For that kind of stuff I usually load / set everything using JSON files. For such job JSON files content could look like :

{
   name: "axe",
   sprite_resource: "weapon_common_axe",
   damage: 5,
   speed: 12,
}

During loading times, using data driven programming, it's possible to easily set resources. Changing a sprite is as easy as opening a text file and set the correct name.

I hope it helps.

[EDIT: fyi you can load text resources using Resources.Load as TextAsset]

\$\endgroup\$
3
  • \$\begingroup\$ I am aware of how to load data from the /Resources folder, but if I did have the JSON file like you describe, how would I load the actual sprite? Currently I am assigning Sprites to a Component (manually, which sucks). Is there a way to say "from this texture atlas, get sub sprite "armor_common_23" without first associating armor_common_23 with a Component? \$\endgroup\$
    – Kobald
    Commented Sep 26, 2014 at 22:15
  • \$\begingroup\$ Never worked with atlasses so far. But Sprite[] sprites = Resources.LoadAll<Sprite>("your_atlas") seems to do the job. \$\endgroup\$
    – lvictorino
    Commented Sep 26, 2014 at 22:27
  • \$\begingroup\$ See : answers.unity3d.com/questions/576153/… or answers.unity3d.com/questions/575521/… \$\endgroup\$
    – lvictorino
    Commented Sep 26, 2014 at 22:28
2
\$\begingroup\$
  1. Do it once for one component, then copy. Once completed for one component right click the component and choose "Copy component". Then you can right click the target component and select "Paste component values" and only change the array elements that differ between the two.
  2. Do it in code. Assign the sprites as defined in a text file programmatically. You can create a custom format for this, or use something like XML. This gives the added benefit of being modifiable outside Unity, like as a user mod. Or, you can assign them based on the name of the parent object (i.e. The component is looking for weapons and so you assign all the sprites with the word "weapon" in them to that array). Do this in the Start() method.
  3. Extend the editor with a script to do this. Either with a bulk copy or the same type of script you'd use in option number two.
\$\endgroup\$
1
  • \$\begingroup\$ My vote is on option 3, unless the sprites must be loaded dynamically during runtime. \$\endgroup\$ Commented Sep 27, 2014 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .