1.本地资源加载
1).建立Editor文件夹
2).建立StreamingAssets文件夹和其Windows的子文件夹
将下方第一个脚本放入Editor 里面
脚本一 资源打包AssetBundle的所有标签资源
using UnityEngine;
using UnityEditor;
public class Tools
{
[MenuItem("Tools/buildAB")] //编辑器扩展
public static void Building()
{
Debug.LogError("111");
string _adPath = Application.streamingAssetsPath + "\Windows";//路径
BuildPipeline.BuildAssetBundles(_adPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneLinux64);
//自动检测有AssetBundle标签的预制体,并进行资源压缩. //固定格式
}
}
资源加载
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Load2 : MonoBehaviour {
// Use this for initialization
void Start ()
{
//本地必须加入 file:// 这样子www才能进行访问
string _abpath ="file://" +Application.streamingAssetsPath + "/Windows/sp";
StartCoroutine("Creat",_abpath);
}
// Update is called once per frame
void Update ()
{
}
IEnumerator Creat(string path)
{
using (WWW lo=new WWW(path))
{
yield return lo;
if (lo != null)
{
AssetBundle ab = lo.assetBundle;
if (ab != null)
{
GameObject obj = ab.LoadAsset<GameObject>("Cube");
Instantiate(obj);
}
}
else {
Debug.LogError("加载失败");
}
}
}
2.加载依赖关系
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LoadManCtr : MonoBehaviour { // Use this for initialization public string ABName; string _abPath; string _manpath; private List<AssetBundle> ManListAB; void Start () { ManListAB = new List<AssetBundle>(); _abPath = Application.streamingAssetsPath + "/Windows/"; _manpath = Application.streamingAssetsPath + "/Windows/Windows"; //查找依赖关系 #region 加载依赖 AssetBundle _manAB = AssetBundle.LoadFromFile(_manpath); //固定格式 AssetBundleManifest manifest = _manAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); string[] dependencies = manifest.GetAllDependencies(ABName); Debug.LogError(dependencies.Length); //加载依赖 if (dependencies.Length != 0) { foreach (string s in dependencies) { Debug.Log(s); //挨个加载依赖关系 ManListAB.Add(LoadABByName(s)); //存在内存中 } } #endregion //依赖加载完毕 圆柱 立方体 AssetBundle _ab = AssetBundle.LoadFromFile(_abPath + ABName); GameObject copyObj = _ab.LoadAsset<GameObject>("Capsule"); Instantiate(copyObj); //释放ab依赖的内存 foreach (var v in ManListAB) { v.Unload(false); } _ab.Unload(false);//不从场景里面删除 //true 场景删除 AssetBundle _ab01 = AssetBundle.LoadFromFile(_abPath + ABName);//_ab.Unload(false); 没有这句话会重复加载 GameObject copyObj01 = _ab01.LoadAsset<GameObject>("Capsule"); Instantiate(copyObj01,Vector3.one,Quaternion.identity); } // Update is called once per frame void Update () { } private AssetBundle LoadABByName(string name) { return AssetBundle.LoadFromFile(_abPath + name); } }