unity assetbundle 加载图集的所有sprite图片

 

在 Unity 中,使用 AssetBundle 加载图集(Atlas)并获取其中的所有 Sprite 图片,通常需要以下步骤:


1. 打包图集到 AssetBundle

首先,确保你的图集(Atlas)已经被打包到 AssetBundle 中。图集通常是一个包含多个 Sprite 的纹理(Texture),并且每个 Sprite 都有对应的元数据(如 UV 坐标、Pivot 等)。


2. 加载 AssetBundle

使用 AssetBundle.LoadFromFile 或 AssetBundle.LoadFromMemoryAsync 等方法加载 AssetBundle。

复制代码
using UnityEngine;
using System.Collections;

public class LoadSpriteFromAssetBundle : MonoBehaviour
{
    private AssetBundle assetBundle;

    IEnumerator Start()
    {
        string path = Application.streamingAssetsPath + "/your_assetbundle_name";
        var request = AssetBundle.LoadFromFileAsync(path);
        yield return request;

        assetBundle = request.assetBundle;
        if (assetBundle == null)
        {
            Debug.LogError("Failed to load AssetBundle");
            yield break;
        }

        // 加载图集中的所有 Sprite
        LoadSpritesFromAtlas();
    }
}
复制代码

3. 加载图集中的所有 Sprite

图集通常是一个包含多个 Sprite 的纹理(Texture)。你可以通过以下方式加载图集中的所有 Sprite:

方法 1:使用 LoadAllAssets<Sprite>

如果图集中的 Sprite 被打包为独立的资源,可以使用 LoadAllAssets<Sprite> 方法加载所有 Sprite。

复制代码
private void LoadSpritesFromAtlas()
{
    // 加载图集中的所有 Sprite
    Sprite[] sprites = assetBundle.LoadAllAssets<Sprite>();

    foreach (Sprite sprite in sprites)
    {
        Debug.Log("Loaded Sprite: " + sprite.name);
        // 可以将 Sprite 赋值给 UI Image 或其他用途
    }
}
复制代码

方法 2:加载图集纹理并手动分割

如果图集是一个单独的纹理,并且 Sprite 的元数据没有被打包到 AssetBundle 中,你需要手动加载纹理并根据 UV 坐标分割 Sprite。

复制代码
private void LoadSpritesFromAtlas()
{
    // 加载图集纹理
    Texture2D atlasTexture = assetBundle.LoadAsset<Texture2D>("atlas_texture_name");

    // 假设你知道图集中每个 Sprite 的 UV 坐标和大小
    Rect[] spriteRects = new Rect[]
    {
        new Rect(0, 0, 64, 64), // Sprite 1 的 UV 坐标和大小
        new Rect(64, 0, 64, 64), // Sprite 2 的 UV 坐标和大小
        // 添加更多 Sprite 的 UV 坐标
    };

    // 创建 Sprite
    foreach (Rect rect in spriteRects)
    {
        Sprite sprite = Sprite.Create(atlasTexture, rect, new Vector2(0.5f, 0.5f));
        Debug.Log("Created Sprite: " + sprite.name);
        // 可以将 Sprite 赋值给 UI Image 或其他用途
    }
}
复制代码

4. 卸载 AssetBundle

private void OnDestroy()
{
    if (assetBundle != null)
    {
        assetBundle.Unload(false); // false 表示不卸载从 AssetBundle 加载的资源
    }
}

注意事项

  1. 图集打包方式

    • 如果图集中的 Sprite 被打包为独立的资源,可以直接使用 LoadAllAssets<Sprite>

    • 如果图集是一个单独的纹理,需要手动分割 Sprite。

  2. AssetBundle 依赖

    • 如果图集依赖于其他资源(如材质、Shader 等),确保这些依赖资源也被正确加载。

  3. 内存管理

    • 加载 AssetBundle 后,及时卸载不再需要的资源,避免内存泄漏。

  4. 异步加载

    • 如果 AssetBundle 较大,建议使用异步加载(如 LoadFromFileAsync)以避免卡顿。


完整示例

以下是一个完整的示例代码:

复制代码
using UnityEngine;
using System.Collections;

public class LoadSpriteFromAssetBundle : MonoBehaviour
{
    private AssetBundle assetBundle;

    IEnumerator Start()
    {
        string path = Application.streamingAssetsPath + "/your_assetbundle_name";
        var request = AssetBundle.LoadFromFileAsync(path);
        yield return request;

        assetBundle = request.assetBundle;
        if (assetBundle == null)
        {
            Debug.LogError("Failed to load AssetBundle");
            yield break;
        }

        // 加载图集中的所有 Sprite
        LoadSpritesFromAtlas();
    }

    private void LoadSpritesFromAtlas()
    {
        // 加载图集中的所有 Sprite
        Sprite[] sprites = assetBundle.LoadAllAssets<Sprite>();

        foreach (Sprite sprite in sprites)
        {
            Debug.Log("Loaded Sprite: " + sprite.name);
            // 可以将 Sprite 赋值给 UI Image 或其他用途
        }
    }

    private void OnDestroy()
    {
        if (assetBundle != null)
        {
            assetBundle.Unload(false); // false 表示不卸载从 AssetBundle 加载的资源
        }
    }
}
复制代码

 

通过以上方法,你可以从 AssetBundle 中加载图集并获取其中的所有 Sprite 图片。

posted @   Flamesky  阅读(245)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2024-01-11 TCP协议详解
点击右上角即可分享
微信分享提示