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

相关推荐
枫景Maple3 小时前
简单了解一下Unity的MaterialPropertyBlock
unity·游戏引擎
wkm9568 小时前
Unity程序嵌入Qt后点击UI按钮Button没有反应
qt·ui·unity
启诚科技14 小时前
虚拟现实--->unity学习
学习·unity
WarPigs15 小时前
Unity声音管理系统笔记
笔记·unity·音频
Kermit202316 小时前
unity一个图片的物体,会有透明的效果
unity
咩咩觉主17 小时前
Unity 一个丝滑的3D下--XY轴2D平台跳跃--控制器模板(FSM)
3d·unity·游戏引擎
avi91111 天前
Unity打包崩溃SRP-URP-管线的问题:Shader::SRPBatcherInfoSetup()
unity·android studio·调试·crash·崩溃
徐子竣1 天前
Unity编辑器功能及拓展(2) —Gizmos编辑器绘制功能
unity·编辑器·游戏引擎
Tatalaluola1 天前
【Unity】 鼠标拖动物体移动速度跟不上鼠标,会掉落
学习·unity·c#·游戏引擎
徐子竣1 天前
Unity编辑器功能及拓展(1) —特殊的Editor文件夹
unity·编辑器·游戏引擎