# 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?

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) {
Assert.IsNotNull(atlasSprites, atlasName);
Assert.IsTrue(atlasSprites.Length > 0, atlasName);

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

Debug.LogWarning(String.Format(
"Found 0 new sprites in texture atlas '{0}'.",
atlasName));
}
}


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.

• 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? – Kobald Sep 26 '14 at 22:15
• Never worked with atlasses so far. But Sprite[] sprites = Resources.LoadAll<Sprite>("your_atlas") seems to do the job. – lvictorino Sep 26 '14 at 22:27
• – lvictorino Sep 26 '14 at 22:28
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.
• My vote is on option 3, unless the sprites must be loaded dynamically during runtime. – user1323245 Sep 27 '14 at 3:47