首先要在文件夹内创建Editor文件夹,把脚本CreateAssetBundle.cs放进去。脚本如下
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CreateAssetBundle : MonoBehaviour {
///
/// 将选中的预制分别打包
///
[MenuItem(“开始资源打包/Create AssetBundles By themselves”)]
static void CreateAssetBundleThemelves()
{
//获取要打包的对象(在Project视图中)
Object[] selects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//遍历选中的对象
foreach (Object obj in selects)
{
//这里建立一个本地测试
//注意本地测试中可以是任意的文件,但是到了移动平台只能读取路径StreamingAssets里面的
//StreamingAssets是只读路径,不能写入
string targetPath = “D:\virtual mirror\FBX\” + obj.name + “.assetbundle”;//输出
到的文件的后缀名是assetbundle和unity都可以
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
BuildAssetBundleOptions.CollectDependencies))
{
Debug.Log(obj.name + "is packed successfully!");
}
else
{
Debug.Log(obj.name + "is packed failly!");
}
}
//刷新编辑器(不写的话要手动刷新,否则打包的资源不能及时在Project视图内显示)
AssetDatabase.Refresh();
}
}
打包场景的代码如下:
[MenuItem(“Custom Editor/Create Scene”)]
static void CreateSceneALL ()
{
//清空一下缓存
Caching.CleanCache();
string Path = Application.dataPath + “/MyScene.unity3d”;
string []levels = {“Assets/Level.unity”};
//打包场景
BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh ();
}
读取的Assetbundle的脚本如下:
using UnityEngine;
using System.IO;
using System.Collections;
public class ScenesManager : MonoBehaviour {
private string DownUrl;
private string FBXname;
private string PrizeURL;
// public static readonly string DownUrl =
//#if UNITY_ANDROID
// “jar:file://” + Application.dataPath + “!/assets/”;
//#elif UNITY_IPHONE
// Application.dataPath + “/Raw/”;
//#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
// “file://” + System.Environment.CurrentDirectory + “\weiyi(hou).assetbundle”;
//#else
// string.Empty;
//#endif
void Awake()
{
PrizeURL = “D:\virtual mirror\FBXname\FBXname.txt”; //自动获取当前文件在系统中的
路径
FBXname = ReadFile(PrizeURL, 4, 1); //第一行信息
}
void Start () {
DownUrl = "file://D:\\virtual mirror\\FBX\\" + FBXname + ".assetbundle";
//Debug.Log("FBXname=" + FBXname);
//Debug.Log("DownUrl=" + DownUrl);
}
void Update () {
DownLoadPrefab();
}
string ReadFile(string FileName, int linenumber, int line)
{
string[] strs = File.ReadAllLines(FileName); //读取所有行信息
if (linenumber == 0)
{
return "";
}
else
{
return strs[linenumber - line];
//返回特定一行的信息
}
}
void DownLoadPrefab()
{
if (Input.GetKeyDown(KeyCode.Q))
{
StartCoroutine(DownPrefabMethod1(DownUrl));
}
}
IEnumerator DownPrefabMethod1(string Url)
{
WWW www = new WWW(Url);
yield return www;
GameObject ClonePrefab = Instantiate(www.assetBundle).LoadAsset(FBXname, typeof
(GameObject)) as GameObject;
Instantiate(ClonePrefab);
www.assetBundle.Unload(false);
}
}
操作步骤为:
1、提前把要打包的文件做成预设prefab;
2、选中需要打包的预设prefab,点击Unity界面上方工具栏按钮‘开始资源打包/Create AssetBundles By
themselves’,此时选中的文件会自动打包成assetbundle文件在你所设置的路径中。
3、在TXT文档中写入了assetbundle包名称,脚本中会自动读取对应行数的名称而进行解包,解包完成后可执
行相关操作。最后不要忘记加载完assetbundle后要及时释放: www.assetBundle.Unload(false); 以免再
次提取文件时出错。
这只是打包单一文件的方法,后续会增加多个文件打包到一个assetbundle中,读取指定文件的方法。
今天下午的功夫就用来研究这个了,不过总算不辜所托,小伙伴们有更高的方法欢迎在下方评论区提出。