Best practice for importing sprites

Right now, to import a sprite (Texture 2D asset), I use Resources.LoadAll(“path/to/resource”). That works just fine, but I’ve noticed something odd, and it’s got me wondering if I should be using some other technique.

The problem:

// This file is a spritesheet of all of my enemies (and their respective alternate sprites for animations) in the game
List<Sprite> enemySprites = Resources.LoadAll<Sprite>("path/to/enemySprites").ToList();
// I figured ToList() would make a clone of this list, but I guess not a deep clone... I've also tried Select, but no dice
List<Sprite> enemy1 = enemySprites.Where(x => x.name.contains(enemyName)).ToList();
foreach (Sprite sprite in enemy1) {
  // This actually overwrites the sprite's name in the Resources.LoadAll call if I try to access it again
  sprite.name = sprite.name.Replace(enemyName, "");
}

The code above works on the first time, but any subsequent time has an empty list for enemy1 (because the name has been overwritten in the foreach). This happens even if I stop play mode and enter back into it. I believe it has something to do with how resources are cached (and how in my actual game, I’ve got this tied to a static property), but I’ve searched all over, and I can’t find how to force clear the cached resources… tried GC.Collect, AssetDatabase.Refresh, and other commands that I’ve forgotten now. The only way to get this to go back to working for the first time is closing Unity Editor altogether and reopening it.

Having said all of that, should I not be loading sprites this way, and instead maybe try to make them a material, and apply it to a mesh? I’m fairly new to all of this, so I’d appreciate any guidance, and I apologize if this is a dupe thread… my searching failed me.

It might be a good to know why you’re changing the sprite names. Is this necessary? Is there a workaround you could use instead?

If you want to create a copy of the sprite itself (if that’s the kind of deep clone you want) you should check out Sprite.Create Unity - Scripting API: Sprite.Create

You may also be interested in Asset Bundles / Addressables, which are Unity’s recommended ways to manage resources: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn Officially Unity says “never use” Resources.Load - though in practice many people still use it for a variety of reasons.

I was just being too lazy… my enemies’ sprite sheet has every single enemy, but maybe I should split it to a sprite sheet per enemy. I was naming my enemies with their actions, like “Enemy1WalkDown,” but I have an enum with the possible animations, like “WalkDown,” so I was trying to strip “Enemy1” from each sprite in the list, so I could Enum.TryParse it… instead, I’d probably be better off doing “Enemy1” as an actual sprite file, and within there, each splice is “WalkDown,” “WalkRight,” etc.

Yeah, I saw that, and it seemed like that’d be pretty inefficient.

I think this is what I’m looking for… I saw all over that everyone’s using Resource.Load, but then I saw one comment about how that’s not what Unity recommends. I think I’ll have to take some time to understand how to implement this. Thanks!