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

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


1. 打包图集到 AssetBundle

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


2. 加载 AssetBundle

使用 AssetBundle.LoadFromFileAssetBundle.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 图片。

相关推荐
qq_5982117579 小时前
Unity.UGUI DrawCall合批笔记
笔记·unity·游戏引擎
Tech Synapse18 小时前
Unity ML-Agents实战指南:构建多技能游戏AI训练系统
人工智能·游戏·unity
咩咩觉主1 天前
c#数据结构 线性表篇 非常用线性集合总结
开发语言·数据结构·unity·c#·游戏引擎·程序框架
浅陌sss2 天前
Unity中AssetBundle使用整理(一)
unity·游戏引擎
KhalilRuan2 天前
Unity-Shader详解-其五
unity·游戏引擎
惊鸿醉3 天前
Unity C# 中的 反射 小记
unity·c#·游戏引擎
Yuze_Neko3 天前
C#异步Task,await,async和Unity同步协程
开发语言·unity·c#
七月.末3 天前
【Unity】Unity中修改网格的大小和倾斜网格
unity·游戏引擎
蔗理苦3 天前
2025-05-07 Unity 网络基础7——TCP异步通信
网络·tcp/ip·unity·异步通信
FAREWELL000753 天前
Unity基础学习(十)Camera组件
学习·unity·c#·游戏引擎