So I was wondering if there is a way to "repack" sprites to be a theme. I got 1 image with all the stage sprites I need and I basically wondered if I can keep them in that 1 image and then have "links" or something to a seperate pack. I thought of doing a "Theme" class and then create instances of that to match my themes, with variables hardcoded to match. But I can't believe there isn't a better way. I hope I've explained this well enough ^^
-
It might work to load the image (resources.loadall) and sort the sprites into different containers (e.g. lists) by a prefix in the name. This would at least avoid hardcoding of all the single sprites.– Gunnar B.Dec 4, 2014 at 18:11
-
Or you create public arrays/lists and drag the sprites into them in the inspector.– Gunnar B.Dec 4, 2014 at 19:26
-
FTR if you use 2dToolkit, this is automatic - it's one of the reasons people still use it so much even though Unity has 2D now.– FattieMar 24, 2016 at 18:43
Add a comment
|
1 Answer
I ended up doing this, for those interested, and it seems to work. Though it does leave me having to code the textures in the code like, I was looking for a way to it be done in the editor/folder structure.
ThemePack t = new ThemePack(Room.Theme.Medbay);
t.walls.Add(sprites[6]);
t.floors.Add(sprites[32]);
t.floors.Add(sprites[66]);
t.floors.Add(sprites[67]);
t.floors.Add(sprites[68]);
t.floors.Add(sprites[69]);
using UnityEngine;
using System.Collections;
public class SpriteCollection {
private Sprite[] sprites;
private string[] names;
public Sprite this[int i] {
get {
return sprites[i];
}
}
public SpriteCollection(string spritesheet) {
sprites = Resources.LoadAll<Sprite>(spritesheet);
names = new string[sprites.Length];
for(int i = 0; i < names.Length; i++) {
names[i] = sprites[i].name;
}
}
public Sprite GetSprite(string name) {
return sprites[System.Array.IndexOf(names, name)];
}
}
using UnityEngine;
using System.Collections.Generic;
using Level;
public class ThemePack {
public List<Sprite> walls;
public List<Sprite> floors;
public Room.Theme name {get; set;} //Could use a string instead, but this is an enum already existing.
private ThemePack() {
}
public ThemePack(Room.Theme n, List<Sprite> w, List<Sprite> f) {
name = n;
walls = w;
floors = f;
}
public ThemePack (Room.Theme n) : this(n, new List<Sprite>(), new List<Sprite>()) {
}
public Sprite GetRandomWall() {
return walls[Random.Range(0, walls.Count - 1)];
}
public Sprite GetRandomFloor() {
return floors[Random.Range(0, floors.Count - 1)];
}
}