Unity如何制作AB包,并在服务器加载。

翻了翻如何打AB包,并在加载服务器,没有找到合适的文章。靠人不如靠己,自己动手丰衣足食。Unity版本2018.1.1

1、制作AB包

直接上代码

using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// 放在Editor文件夹下
/// </summary>
public class SetAssetsbundle  {
    [MenuItem("Bulid/BulidAssetBundles")]
	static void BulidAllAssetBundles()
    {
      //路径这里我放在了streamingAssets下,不要忘记创建该文件夹
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
       
    }
}

2、服务器加载

直接代码

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

public class LoadAB : MonoBehaviour {

    AssetBundle ab;
    // Use this for initialization
    IEnumerator Start()
    {

//地址使用服务器给过来的地址即可
        string uri = @"xxxxxxxxxxxxxx";
        //cube.bundle是我打包的assetbundle的名字
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri + "/cube.bundle");
        yield return request.SendWebRequest();
        ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        yield return null;
        //使用里面的资源
        //cube是你打包资源的prefab名字
        GameObject prefab = ab.LoadAsset<GameObject>("Cube");
        if (prefab != null)
            Instantiate(prefab,Vector3.zero,Quaternion.identity);
    }

}

3、效果图

服务器位置
在这里插入图片描述
unity加载效果
在这里插入图片描述
不要忘记吧加载脚本挂到场景中噢。。。。。欢迎交流。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值