入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2017-08-15 投稿人: 浏览次数:1225

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

然后在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 () {

    //acc.acc是我打包的assetbundle的名字,这里我本地放在了 streamingAssets下
    string uri = Application.streamingAssetsPath + "/acc.acc";
     ab = AssetBundle.LoadFromFile(uri);

//地址使用服务器给过来的地址即可
string uri = @”http://xxxxx“;
//acc.acc是我打包的assetbundle的名字
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri + “acc.acc”);
yield return request.Send();
ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;

    yield return null;
    //使用里面的资源
    //cube是你打包资源的prefab名字
    GameObject prefab = ab.LoadAsset<GameObject>("Cube");
    if (prefab!=null)
        Instantiate(prefab);

}

}

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

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像