AssetBundleBrowser和assetbundle在游戏中的使用

1导入assetBundleBrowser(商店可下)

2通过Window —> assetBundleBrowser

AssetBundleBrowser和assetbundle在游戏中的使用_第1张图片

拖入资源,创建assetbundle

 

image01,image02分别使用了同一张图片资源,将这两张图做成预制体,然后做成ab包

image01   和02   同时使用一张相同的图片,打出来的两个ab包分别是24k

AssetBundleBrowser和assetbundle在游戏中的使用_第2张图片

将image01,02两个使用的通篇单独打一个包,这个时候,01,02预制体ab包分别是3k,图片资源的ab包是23k

AssetBundleBrowser和assetbundle在游戏中的使用_第3张图片

而这个时候,01和02代表的ab包有了依赖,都有对于图片资源的依赖

AssetBundleBrowser和assetbundle在游戏中的使用_第4张图片

当存在AssetBundle有互相依赖的情况下,要首先加载依赖的AB包资源,然后在加载自己的资源,否则会导致自己的资源不完整

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

public class abLoder : MonoBehaviour
{
    string rootpath;
    Transform parent;
    public Image testImg;
    public AudioSource audio;
    public VideoPlayer videoPlayer;
    // Use this for initialization
    void Start()
    {
        parent = GameObject.Find("Canvas").transform;
        rootpath = Path.Combine(Application.dataPath, "../AssetBundles/StandaloneWindows/");
        GameObject go = LoadGameObject("Image");
        Instantiate(go, parent);

        testImg.sprite = LoadSprite("chuansong");

        audio.clip = LoadAudioClip("TestAudioClip");
        audio.Play();
    }
    AssetBundle mainAB;
    AssetBundleManifest mainfest;
    /// 
    /// 当ab包存在依赖,那么首先要加载依赖的AB包,然后才加载需要的资源
    /// 
    /// 指的是需要加载的资源所在的ab包
    public void GetAllDependencies(string ABName)
    {
        if (mainAB == null)
        {
            mainAB = AssetBundle.LoadFromFile(rootpath + "StandaloneWindows");
        }
        if (mainfest == null)
        {
            mainfest = mainAB.LoadAsset("AssetBundleManifest");
            string[] dependences = mainfest.GetAllDependencies(ABName);
            int count = dependences.Length;
            if (count != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    AssetBundle ab = AssetBundle.LoadFromFile(rootpath + dependences[i]);
                    ab.LoadAllAssets();
                }
            }
        }
    }

    public GameObject LoadGameObject(string ABName)
    {
        AssetBundle ab = AssetBundle.LoadFromFile(rootpath + ABName);
        //从里边获取资源
        GameObject go = ab.LoadAsset(ABName + ".prefab");
        return go;
    }

    public Sprite LoadSprite(string iconName, string extendName = ".png")
    {
        AssetBundle ab = AssetBundle.LoadFromFile(rootpath + iconName);
        Sprite icon = ab.LoadAsset(iconName + extendName);
        return icon;
    }

    public AudioClip LoadAudioClip(string clipName, string extendName = ".wav")
    {
        AssetBundle ab = AssetBundle.LoadFromFile(rootpath + clipName);
        AudioClip clip = ab.LoadAsset(clipName + extendName);
        return clip;

    }
 
}

 

 

 

 

 

你可能感兴趣的:(AssetBundle)