Get sprites from a Texture2D

Hi,

Im using UnityEdtior to get sliced sprites from a texture 2D. This texture is a tileset and each tile is used to build like a RPG map. I sliced all texture on Sprite Editor from Unity. The code below works properly and take me a sprites array, however it cannot be used to compile for mobiles because UnityEditor codes cannot be package.

//texture is an instance of Texture2D
var texturePath = UnityEditor.AssetDatabase.GetAssetPath(texture);
//get all generated sprites from a sliced textured.
var sprites = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(texturePath).OfType<Sprite>().ToArray();

How can I accomplish it without have to use UnityEditor? I need it to build maps on the fly.
Thanks!

Idk much about IOS games so idk if this’ll work but i’ll recommend it anyways.

Just make your public sprite an array like this.

public Sprite[] TileImages;

Then when ever you click at the tile you want in Gameplay mode or how ever you wish to change the tiles. This should happen.

Tiles.GetComponent<SpriteRenderer>().sprite = TileImages[4]

Simply change the number of the TileImages array and bam. You’re set.