unity 关于assetbundle 打包和加载的方法

先说打包assetbundle 的方法:
首先将prefab相关的材质和贴图均打入同一个包中,在inspector面板的最下面 ,有个assetbundle,将名字写好即可

有任何问题可以添加 QQ群 207019099

然后在project下建立Editor文件夹,创建一个脚本并写代码如下
[MenuItem(“Window/Build AssetBundles”)]
static void BuildAllAssetBundles()
{
string dir = “AssetBundle”;
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
}

解释一下最后一个参数,这里指的是在pc端使用,如果在别的平台的话,改变下参数。

好了,打包说完了

下面是加载的部分,我用宏定义做了关于打包和加载的本地加载和服务器加载的方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Load : MonoBehaviour {
AssetBundle ab;
// Use this for initialization
IEnumerator Start () {

#if UNITY_EDITOR
//acc.acc是我打包的assetbundle的名字,这里我本地放在了 streamingAssets下
string uri = Application.streamingAssetsPath + “/acc.acc”;
ab = AssetBundle.LoadFromFile(uri);
#else
//地址使用服务器给过来的地址即可
string uri = @“http://xxxxx”;
//acc.acc是我打包的assetbundle的名字
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri + “acc.acc”);
yield return request.Send();
ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
#endif
yield return null;
//使用里面的资源
//cube是你打包资源的prefab名字
GameObject prefab = ab.LoadAsset(“Cube”);
if (prefab!=null)
Instantiate(prefab);

}

}

这里所做的只是一个简单的加载的例子,有需要自行扩展

猜你喜欢

转载自blog.csdn.net/gaofei12300/article/details/77192159