unity创建和加载AssetBundle

时间:2022-12-18 13:31:50

先说一下为什么要使用AssetBundle吧,以前做东西一直忽略这个问题,现在认为这个步骤很重要,代码是次要的,决策和为什么这样搞才是关键。

一句话概括吧,AssetBundle实现了资源与服务分离,方便做热更新。

一、创建AssetBundle

两步:1.设置AssetBundleName;2.调用BuildPipeline.BuildAssetBundles。详细过程如下:

设置AssetBundleName有两种方式,分为手动和代码

先讲手动,找到你需要被打包的文件,然后如下图

unity创建和加载AssetBundle

方式2,代码设置这个AssetBundleName,代码如下,创建bundle的代码也一起贴上了:

using UnityEngine;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif public class BuildBundleMenu : MonoBehaviour { public static string sourcePathPrefab = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Prefabs/";
public static string sourcePathMater = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Materials/"; #if UNITY_EDITOR
[MenuItem( "Example/Build Asset Bundles" )]
static void BuildABs( )
{ ClearAssetBundlesName (); FindAllFile (sourcePathPrefab);
FindAllFile (sourcePathMater); // Put the bundles in a folder called "ABs" within the Assets folder.
BuildPipeline.BuildAssetBundles( "Assets/ABs", BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneOSXIntel64);
} /// <summary>
/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
/// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
/// </summary>
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames ().Length;
Debug.Log (length);
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
} for (int j = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);
}
length = AssetDatabase.GetAllAssetBundleNames ().Length;
Debug.Log (length);
} /// <summary>
/// 遍历文件夹里面的所有文件夹和文件
/// </summary>
/// <param name="source"></param>
static void FindAllFile(string source)
{
DirectoryInfo folder = new DirectoryInfo (source);
FileSystemInfo[] files = folder.GetFileSystemInfos ();
int length = files.Length;
for (int i = 0; i < length; i++) {
if(files[i] is DirectoryInfo)
{
FindAllFile(files[i].FullName);
}
else
{
if(!files[i].Name.EndsWith(".meta"))
{
SetAssetName (files[i].FullName);
}
}
}
} /// <summary>
/// 为需要打包的文件设置assetName.
/// </summary>
/// <param name="source"></param>
static void SetAssetName(string source)
{
string _assetPath = "Assets" + source.Substring (Application.dataPath.Length);
//string _assetPath2 = source.Substring (Application.dataPath.Length + 1);
//在代码中给资源设置AssetBundleName
AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath);
//string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1);
//assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");
//assetImporter.assetBundleName = assetName;
assetImporter.assetBundleName = "Weather.unity3d";
}
#endif
}

  菜单栏会出现这个,点一下就可以了,bundle创建完成。注意打bundle前要现在Assets目录下新建一个ABs文件夹,打的bundle都在这个文件夹里面。

unity创建和加载AssetBundle

注:只要设置了AssetBundleName的,都会进行打包,不论在什么目录下。

二、bundle的加载

直接贴代码吧

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class LoadAssetbundle : MonoBehaviour { private string pathurl = "";
// Use this for initialization
void Start () {
string pathPrefab = "file://" + Application.dataPath + "/ABs/weather.unity3d"; StartCoroutine (LoadALLGameObject(pathPrefab));
} //读取全部资源
private IEnumerator LoadALLGameObject(string path)
{ WWW bundle = new WWW(path); yield return bundle; //通过Prefab的名称把他们都读取出来
Object obj0 = bundle.assetBundle.LoadAsset("CloudStorm_02_8x8-64.prefab"); //加载到游戏中
yield return Instantiate(obj0);
bundle.assetBundle.Unload(false);
}
}