using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class Package : Editor
{
[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain()
{
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in SelectedAsset)
{
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("/");
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, "");
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;
List.Add(obj);
}
}
}
}
public static void SetBuildAsset(string path, ArrayList List )
{
path = path.Replace("Assets/", "");
string targetPath = Application.dataPath + "/StreamingAssets/" + path + ".assetbundle";
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.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());
}
}
}