- Home /
How does one get sprites into a static class?
I have a set of tiles. Each of these tiles has a number attached called the sprite index. When reading these tiles to place them into the world, I take that number and use it to look up a sprite from a scriptable object which holds an array of sprites, called a sprite pallet. I need to access a pallet from the static class that holds the map generation. I also need to be able to set that pallet beforehand and to put sprites in it somehow. I can do one or the other, but not both. I either have a palate I cant use or a sad empty palate that holds no sprites
I have tried making a mono behavior that has an instance variable that is hooked up with get/set into a static class with an array of pallets but It does not show up in the editor, making it rather hard to assign pallets to.
Does anyone know a good way of getting premade, persistent objects, be they scriptable or image-based, into a static class?
I don't know how much the code will help with this question, but here it is, just in case
[Serializable]
public class Map
{
//map data
public int sizeX;
public int sizeY;
public int spritePallate;
public float[,] heightMap;
public bool[,] walkableMap;
public int[,] spriteIndexMap;
}
public static class MapReader
{
public static Tile[,] GeneratePhysicalMap(Map map)
{
DestroyPhysicalMapTiles();
if (map == null)
{
map = new Map(backupMapSize.x, backupMapSize.y);
}
tileParent = new GameObject("Tile Parent").transform;
tiles = new Tile[map.sizeX,map.sizeY];
Vector2 mapHalfHeight = new Vector2(map.sizeX / 2.5f, map.sizeY / 2.5f);
for (int x = 0; x < map.sizeX; x++)
{
for (int y = 0; y < map.sizeY; y++)
{
Tile tile = map.SetTileProperties(x, y);
GameObject gameObjectTile = InstantateTileGameObject(mapHalfHeight, tile);
gameObjectTile.AddComponent<TileBehaviour>().tile = tile;
tiles[x, y] = tile;
gameObjectTile.AddComponent<BoxCollider>().size = new Vector3(1, 1, .2f);
gameObjectTile.AddComponent<SpriteRenderer>().sprite = GetSpriteFromIndexAndPallete(tile.spriteIndex,/*ERROR*/);
}
}
return tiles;
}
public static Sprite GetSpriteFromIndexAndPallete(int spriteIndex, SpritePallate spritePallate)
{
return spritePallate.sprites[spriteIndex];
}
}
[CreateAssetMenu(fileName = "Pallate", menuName = "SpritePallate", order = 0)]
public class SpritePallate : ScriptableObject
{
public int ID = 0;
public Sprite[] sprites;
}
Answer by jkpenner · Jun 25, 2020 at 11:04 PM
If you're calling GeneratePhysicalMap from a GameObject, you could make the SpritePallate a parameter of the GeneratePhysicalMap method. The GameObject could then store a reference to a SpritePallate and pass it to the method when it is called.
public static Tile[,] GeneratePhysicalMap(Map map, SpritePallate spritePallate)
That will work for me for now. Thank you for such a swift reply! It makes me feel a little silly for not thinking of it sooner.
Your answer
Follow this Question
Related Questions
How do I have an EditorWindow save it's data inbetween opening and closing Unity? C# 5 Answers
Can I active some static function in ScriptableObject? 0 Answers
Scriptableobject sprites 0 Answers
How can i use static function in ScriptableObject? 3 Answers
Unity : Singleton ScriptableObjects resets after play 1 Answer