Unity生成AB包和加载AB包
作者:互联网
unity生成AB包
生产AB包,编辑器脚本放在Editor文件夹下(切记)
using UnityEngine; using UnityEditor; using System.IO; public class BuildAssetBundle : MonoBehaviour { [MenuItem("Tools/打AB包")] public static void BuildAB() { string dir = Application.streamingAssetsPath; //定义AB包路径:工程目录下的StreamingAssets if (Directory.Exists(dir) == false)//如果不存在文件夹,那么新建一个 { Directory.CreateDirectory(dir); } BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.WebGL); AssetDatabase.Refresh(); Debug.LogError("打包完成"); } }
1.先把你需要的对象变成预制体,选中预制体,给预制体添加AssetBundle(如下图)
2.设置完成点击Tools/打AB包 等待即可
完成之后文件夹会出现 如下图文件
3.然后把第一个文件复制到自己的服务器即可
加载AB包
1.两种方式脚本如下
using System.Collections; using UnityEngine; using UnityEngine.Networking; public class Load_AB : MonoBehaviour { private void Start() { StartCoroutine(LoadWWW()); StartCoroutine(FromWebRequest()); } private IEnumerator FromWebRequest() { string path = @"http://192.168.1.80:8088/unity/model.ab"; UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path); yield return webRequest.SendWebRequest(); AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(webRequest); //object obj = assetBundle.LoadAsset("Image"); //Instantiate((GameObject)obj, FindObjectOfType<Canvas>().transform); object[] objects = assetBundle.LoadAllAssets(); foreach (var item in objects) { Instantiate((GameObject)item, FindObjectOfType<Canvas>().transform); } yield return null; } private WWW Loadab; private IEnumerator LoadWWW() { Loadab = new WWW("http://192.168.1.80:8088/unity/model.ab"); // 路径可以根据自己需求来修改 yield return Loadab; if (Loadab.error != null) { Debug.Log(Loadab.error); } else { object[] objects = Loadab.assetBundle.LoadAllAssets(); foreach (var item in objects) { Instantiate((GameObject)item, FindObjectOfType<Canvas>().transform); } } } }
如果没有服务器可以配置一个本地服务器 (iis管理器)
本地搜索iis打开如下图
右键网站
标签:Loadab,item,Unity,AB,assetBundle,using,dir,加载 来源: https://www.cnblogs.com/qq2351194611/p/16113500.html