0

I am trying to allow a degree of modding in my Unity game by utilizing streaming assets. I can import a single sprite no problem, but I am not sure how to set an imported streaming assets sprite to Sprite Mode: Multiple and slice that sprite into its sub parts.

Here is a test class I am using for the import right now:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelsoftGames.Tools2D;
using System.IO;

public class Sandbox : MonoBehaviour
{
    SpriteRenderer sRenderer = null;

    private void Awake()
    {
        sRenderer = GetComponent<SpriteRenderer>();
    }

    private void Start()
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(Application.streamingAssetsPath);
        FileInfo[] allFiles = directoryInfo.GetFiles("*.*");

        foreach(FileInfo file in allFiles)
            if(file.Name.Contains("Laser"))
                StartCoroutine("LoadSprite", file);
    }

    IEnumerator LoadSprite(FileInfo file)
    {
        if (file.Name.Contains("meta"))
            yield break;
        else
        {
            string fileWithoutExtension = Path.GetFileNameWithoutExtension(file.ToString());

            string finalPath;
            WWW localFile;
            Texture2D texture;

            finalPath = "file://" + file.ToString();
            localFile = new WWW(finalPath);

            Debug.Log(finalPath);

            yield return localFile;

            texture = localFile.texture;
            texture.filterMode = FilterMode.Point;
            Sprite sprite = Sprite.Create(texture as Texture2D, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 32f);
            sRenderer.sprite = sprite;
        }
    }
}
Jeremy Voss
  • 43
  • 1
  • 9

2 Answers2

0

You can't just drop a Sprite sheet in the StreamingAssets folder and expect to access it directly in a build. Since Sprite sheet is in Unity format, you have to use one of Unity's resources/asset API to access it. There are two ways to do this:

1.With the Resources API. This means you must use the Resources folder instead of the StreamingAssets folder. Put the Sprite atlas in the Resources folder then read as follow:

Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];

2.If you want to use the StreamingAssets folder, you must build the Sprite sheet as Assetbundle then use the AssetBundle API read it during run-time. The AssetBundle.LoadAssetWithSubAssets and AssetBundle.LoadAssetWithSubAssetsAsync (Recommended) functions are used to load Sprite atlas.

This post shows how to build AssetBundle. Ignore the loading part because loading sprite atlas is different from loading a normal Texture. Once you build it, see below for how to load it. The sprite atlas is stored in the loadedSprites variable:

public Image image;

string nameOfAssetBundle = "animals";
string nameOfObjectToLoad = "dog";

void Start()
{
    StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}

IEnumerator LoadAsset(string assetBundleName, string objectNameToLoad)
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundles");
    filePath = System.IO.Path.Combine(filePath, assetBundleName);

    //Load "animals" AssetBundle
    var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath);
    yield return assetBundleCreateRequest;

    AssetBundle asseBundle = assetBundleCreateRequest.assetBundle;


    //Load the "dog" Asset (Use Sprite since it's a Sprite. Use GameObject if prefab)
    AssetBundleRequest asset = asseBundle.LoadAssetWithSubAssetsAsync<Sprite>(objectNameToLoad);
    yield return asset;

    //Retrive all the Object atlas and store them in loadedSprites Sprite
    UnityEngine.Object[] loadedAsset = asset.allAssets as UnityEngine.Object[];
    Sprite[] loadedSprites = new Sprite[loadedAsset.Length];
    for (int i = 0; i < loadedSprites.Length; i++)
        loadedSprites[i] = (Sprite)loadedAsset[i];

    Debug.Log("Atlas Count: " + loadedSprites.Length);
    for (int i = 0; i < loadedSprites.Length; i++)
    {
        Debug.LogWarning(loadedSprites[i].name);

        //Do something with the loaded loadedAsset  object (Load to Image component for example) 
        image.sprite = loadedSprites[i];
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
0

Starting with the 1.17.x preview version, you can use the following syntax:

myAddressableSpriteSheet.LoadAssetAsync<Sprite[]>();

With myAddressableSpriteSheet being an AssetReferenceTexture2D. This functionality is not documented anywhere yet but was mentioned in the Unity forum.

Alex Maker
  • 1,529
  • 2
  • 19
  • 27