通过流媒体资产加载Spritesheet / Atlas

问题描述 投票:0回答:1

我试图通过利用流媒体资产在我的Unity游戏中允许一定程度的修改。我可以导入单个精灵没问题,但我不知道如何将导入的流资源精灵设置为精灵模式:多个并将精灵切片到其子部分。

这是我现在用于导入的测试类:

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;
        }
    }
}
c# unity3d 2d
1个回答
0
投票

您不能只在SpamingAssets文件夹中删除Sprite表,并希望直接在构建中访问它。由于Sprite表格是Unity格式,因此您必须使用Unity的资源/资产API之一来访问它。有两种方法可以做到这一点:

1.使用Resources API。这意味着您必须使用Resources文件夹而不是StreamingAssets文件夹。将Sprite atlas放在Resources文件夹中,然后阅读如下:

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

2.如果要使用StreamingAssets文件夹,则必须将Sprite表构建为Assetbundle,然后使用AssetBundle API在运行时读取它。 AssetBundle.LoadAssetWithSubAssetsAssetBundle.LoadAssetWithSubAssetsAsync(推荐)函数用于加载Sprite图集。

这个post展示了如何构建AssetBundle。忽略加载部分,因为加载精灵图集与加载普通纹理不同。构建它之后,请参阅下面的加载方法。精灵地图集存储在loadedSprites变量中:

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];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.