using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class Package : Editor
{
[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain()
{
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//遍历所有的游戏对象
foreach (Object obj in SelectedAsset)
{
//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//StreamingAssets是只读路径,不能写入
//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies))
{
Debug.Log(obj.name + "资源打包成功");
}
else
{
Debug.Log(obj.name + "资源打包失败");
}
}
//刷新编辑器
AssetDatabase.Refresh();
}
[MenuItem("Custom Editor/Create Scene")]
static void CreateSceneALL()
{
//清空一下缓存
Caching.CleanCache();
string Path = Application.dataPath + "/StreamingAssets/MyScene.unity3d";
string[] levels = { "Assets/Scene/Level.unity" };
//打包场景
BuildPipeline.BuildPlayer(levels, Path, BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh();
}
[MenuItem("Custom Editor/Create XML All")]
static void CreateXmlAll()
{
//清空一下缓存
Caching.CleanCache();
//获取选择的目录
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
//
ArrayList List = new ArrayList();
GetAllXmlList(path, List,true);
GetAllDirList(path, List);
SetBuildAsset(path, List);
}
//递归函数
public static void GetAllDirList(string strBaseDir , ArrayList List)
{
DirectoryInfo di = new DirectoryInfo(strBaseDir);
DirectoryInfo[] diA = di.GetDirectories();
for (int i = 0; i < diA.Length; i++)
{
string locPath = diA[i].FullName.Replace("\\", "/");
int indexTemp = locPath.LastIndexOf("/");
//Debug.Log(locPath);
//locPath = locPath.Substring(indexTemp + 1);
GetAllXmlList(locPath,List,false);
//注意:递归了
GetAllDirList(diA[i].FullName,List);
}
}
//打包文件
public static void GetAllXmlList(string path, ArrayList List , bool isOnec)
{
//打包文件
if (path.Length != 0)
{
//取出根目录
if (isOnec == true)
{
path = path.Replace("Assets/", "");
}
//得到选择下的全部目录
string[] fileEntries = null;
if (isOnec == true)
{
fileEntries = Directory.GetFiles(Application.dataPath + "/" + path, "*.xml");
}
else
{
fileEntries = Directory.GetFiles(path, "*.xml");
}
//遍历全部目录
foreach (string fileName in fileEntries)
{
//把目录里的反斜杠变成斜杠
string filePath = fileName.Replace("\\", "/");
int index = filePath.LastIndexOf("/");
//得到目录名称
filePath = filePath.Substring(index);
string localPath = null;
//设置本地路径
if (isOnec == true)
{
localPath = "Assets/" + path + filePath;
}
else
{
localPath = "Assets" + path + filePath;
}
//把目录里的反斜杠变成斜杠
localPath = localPath.Replace("\\", "/");
localPath = localPath.Replace(Application.dataPath, "");
//Debug.Log("本地路径:" + localPath);
//得到打包对象
Object obj = AssetDatabase.LoadMainAssetAtPath(localPath);
//如果打包对象不为空
if (obj != null)
{
//把对象名改成路径名
int num = localPath.IndexOf("/", 7);
string temp = localPath.Substring(num);
temp = temp.Substring(1, temp.Length - 1);
temp = temp.Replace("/", "###");
temp = temp.Replace(".", "$$$");
obj.name = temp;
//Debug.Log("打包改名成功:" + obj.name);
//加入到链表
List.Add(obj);
//Object[] tp = new Object[1];
//tp[0] = obj;
////设置打包
//string targetPath = Application.dataPath + "/StreamingAssets/" + "config.unity3d" + ".assetbundle";
//BuildPipeline.BuildAssetBundle(null, tp, targetPath, BuildAssetBundleOptions.CompleteAssets);
//Debug.Log(tp[0].name);
}
}
}
}
/// <summary>
/// 设置打包目录
/// </summary>
/// <param name="path">打包名称</param>
/// <param name="List">打包对象</param>
public static void SetBuildAsset(string path, ArrayList List )
{
////取出根目录
path = path.Replace("Assets/", "");
////设置打包
string targetPath = Application.dataPath + "/StreamingAssets/" + path + ".assetbundle";
//Debug.Log("Building bundle at: " + targetPath);
Object[] temp = new Object[List.Count];
string[] arr = new string[List.Count];
for (int i = 0; i < temp.Length; i++)
{
temp[i] = (Object)List[i];
arr[i] = temp[i].name;
Debug.Log("@@@@@@@@@@" + temp[i].name);
}
//从激活的选择编译资源文件
//BuildPipeline.BuildAssetBundle(null, temp, targetPath, BuildAssetBundleOptions.CompleteAssets);
BuildPipeline.BuildAssetBundleExplicitAssetNames(temp, arr, targetPath, BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows64);
//刷新编辑器
AssetDatabase.Refresh();
targetPath = "file://" + targetPath;
WWW tempwww = new WWW(targetPath);
string tempstring = "GameAssetBundleInventory$$$xml";
TextAsset textAsset = tempwww.assetBundle.Load(tempstring, typeof(TextAsset)) as TextAsset;
if(textAsset != null)
{
Debug.Log(textAsset.ToString());
}
}
}
unity Package
最新推荐文章于 2024-04-23 17:40:02 发布