Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/515661434236047360
Add the answer.
Source Link
Kobald
  • 539
  • 3
  • 6
  • 13

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));
    }
}

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

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));
    }
}
Source Link
Kobald
  • 539
  • 3
  • 6
  • 13

Fastest way to assign sprites to components in Unity

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