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 图片。

相关推荐
不吃斋的和尚3 小时前
Unity中一个节点实现植物动态(Shader)
unity·游戏引擎
程序猿多布5 小时前
Unity 位图字体
unity
千年奇葩6 小时前
Unity shader glsl着色器特效之 模拟海面海浪效果
unity·游戏引擎·着色器
太妃糖耶8 小时前
Unity摄像机与灯光相关知识
unity·游戏引擎
007_rbq8 小时前
XUnity.AutoTranslator-Gemini——调用Google的Gemini API, 实现Unity游戏中日文文本的自动翻译
人工智能·python·游戏·机器学习·unity·github·机器翻译
万兴丶10 小时前
Unity 适用于单机游戏的红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含源码)
数据结构·unity·设计模式·c#
软件黑马王子17 小时前
Unity游戏制作中的C#基础(5)条件语句和循环语句知识点全解析
游戏·unity·c#
龚子亦1 天前
Unity结合Vuforia虚拟按键实现AR机械仿真动画效果
unity·游戏引擎·ar·数字孪生·虚拟仿真
程序猿多布1 天前
Unity Excel导表工具转Lua文件
unity·excel
avi91112 天前
[AI相关]Unity的C#代码如何简写
unity·c#·语法糖