翻了翻如何打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加载效果
不要忘记吧加载脚本挂到场景中噢。。。。。欢迎交流。