同上,可忽略此篇文章。。。
很早之前就写了这个管理器,还是4.X的版本,由于项目还未采用AB,所以这个管理器有名无实,只是用来做了配置的更新。
忽略这个吧。。。。
有时间会对Unity5的AB打包进行分享。。。
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class AssetBundleManager : EditorWindow
{
public static AssetBundleManager window;
public static UnityEditor.BuildTarget buildTarget = BuildTarget.StandaloneWindows;
[MenuItem("RYGN/AssetBundle/AssetBundle For Windows", false, 1)]
static void ExecuteWindows()
{
if (window == null)
{
window = (AssetBundleManager)GetWindow(typeof(AssetBundleManager));
}
buildTarget = UnityEditor.BuildTarget.StandaloneWindows;
window.Show();
}
[MenuItem("RYGN/AssetBundle/AssetBundle For IPhone",false,2)]
static void ExecuteIPhone()
{
if (window == null)
{
window = (AssetBundleManager)GetWindow(typeof(AssetBundleManager));
}
buildTarget = UnityEditor.BuildTarget.iPhone;
window.Show();
}
[MenuItem("RYGN/AssetBundle/AssetBundle For Android", false, 3)]
static void ExecuteAndorid()
{
if (window == null)
{
window = (AssetBundleManager)GetWindow(typeof(AssetBundleManager));
}
buildTarget = UnityEditor.BuildTarget.Android;
window.Show();
}
void OnGUI()
{
if (GUI.Button(new Rect(10f, 10f, 200f, 50f), "(1)CreateAssetBundle"))
{
CreateAssetBundle.Execute(buildTarget);
EditorUtility.DisplayDialog("", "Step (1) Completed", "OK");
}
if (GUI.Button(new Rect(10f, 70f, 200f, 50f), "(2)Generate MD5"))
{
CreatMD5WithSize.Execute(buildTarget);
//CreateMD5List.Execute(buildTarget);
EditorUtility.DisplayDialog("", "Step (2) Completed", "OK");
}
//if (GUI.Button(new Rect(10f, 130f, 200f, 50f), "(3)Compare MD5"))
//{
// CampareMD5ToGenerateVersionNum.Execute(buildTarget);
// EditorUtility.DisplayDialog("", "Step (3) Completed", "OK");
//}
//if (GUI.Button(new Rect(10f, 190f, 200f, 50f), "(4)Build VersionNum.xml"))
//{
// CreateAssetBundleForXmlVersion.Execute(buildTarget);
// EditorUtility.DisplayDialog("", "Step (4) Completed", "OK");
//}
}
public static string GetPlatformPath(UnityEditor.BuildTarget target)
{
string savePath = "";
switch (target)
{
case BuildTarget.StandaloneWindows:
savePath = "Assets/StreamingAssets/AssetBundle/Windows/";
break;
case BuildTarget.iPhone:
savePath = "Assets/StreamingAssets/AssetBundle/IOS/";
break;
case BuildTarget.Android:
savePath = "Assets/StreamingAssets/AssetBundle/Android/";
break;
}
//创建目录
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
return savePath;
}
public static string GetPlatformName(UnityEditor.BuildTarget target)
{
string platform = "";
switch (target)
{
case BuildTarget.StandaloneWindows:
platform = "Windows";
break;
case BuildTarget.iPhone:
platform = "IOS";
break;
case BuildTarget.Android:
platform = "Android";
break;
}
return platform;
}
}
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class CreateAssetBundle {
public static void Execute(UnityEditor.BuildTarget target)
{
string SavePath = AssetBundleManager.GetPlatformPath(target);
Object[] selectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in selectedAsset)
{
string path = AssetDatabase.GetAssetPath(obj);
path = SavePath + ConvertToAssetBundleName(path);
path = path.Substring(0, path.LastIndexOf('.'));
path += ".assetbundle";
//path = + ConvertToAssetBundleName(path);
//path = SavePath + path.Substring(0, path.LastIndexOf('.'));
//path += ".assetbundle";
BuildPipeline.BuildAssetBundle(obj, null, path,
BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle, target);
AssetDatabase.Refresh();
}
}
static string ConvertToAssetBundleName(string ResName)
{
return ResName.Replace('/', '.');
}
}
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
public class CreatMD5WithSize : MonoBehaviour {
public static void Execute(UnityEditor.BuildTarget target)
{
string platform = AssetBundleManager.GetPlatformName(target);
Execute(platform);
AssetDatabase.Refresh();
}
public static void Execute(string platform)
{
Dictionary<string, List<string>> DicFileMD5 = new Dictionary<string, List<string>>();
MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();
string dir = System.IO.Path.Combine(Application.dataPath, "StreamingAssets/AssetBundle/" + platform);
foreach (string filePath in Directory.GetFiles(dir))
{
if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".xml"))
continue;
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] hash = md5Generator.ComputeHash(file);
List<string> tempList = new List<string>();
string strMD5 = System.BitConverter.ToString(hash);
string size = file.Length.ToString();
tempList.Add(strMD5);
tempList.Add(size);
file.Close();
string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);
if (DicFileMD5.ContainsKey(key) == false)
DicFileMD5.Add(key, tempList);
else
Debug.LogWarning("<Two File has the same name> name = " + filePath);
}
string savePath = System.IO.Path.Combine(Application.dataPath, "StreamingAssets/AssetBundle/") + platform + "/VersionNum";
if (Directory.Exists(savePath) == false)
Directory.CreateDirectory(savePath);
// 删除前一版的old数据
if (File.Exists(savePath + "/VersionMD5-old.xml"))
{
System.IO.File.Delete(savePath + "/VersionMD5-old.xml");
}
// 如果之前的版本存在,则将其名字改为VersionMD5-old.xml
//if (File.Exists(savePath + "/VersionMD5.xml"))
//{
// System.IO.File.Move(savePath + "/VersionMD5.xml", savePath + "/VersionMD5-old.xml");
//}
XmlDocument XmlDoc = new XmlDocument();
XmlElement XmlRoot = XmlDoc.CreateElement("Files");
XmlDoc.AppendChild(XmlRoot);
foreach (KeyValuePair<string, List<string>> pair in DicFileMD5)
{
XmlElement xmlElem = XmlDoc.CreateElement("File");
XmlRoot.AppendChild(xmlElem);
xmlElem.SetAttribute("FileName", pair.Key);
xmlElem.SetAttribute("MD5", pair.Value[0]);
xmlElem.SetAttribute("Size", pair.Value[1]);
}
读取旧版本的MD5
//Dictionary<string, string> dicOldMD5 = ReadMD5File(savePath + "/VersionMD5-old.xml");
VersionMD5-old中有,而VersionMD5中没有的信息,手动添加到VersionMD5
//foreach (KeyValuePair<string, string> pair in dicOldMD5)
//{
// if (DicFileMD5.ContainsKey(pair.Key) == false)
// DicFileMD5.Add(pair.Key, pair.Value);
//}
XmlDoc.Save(savePath + "/VersionMD5.xml");
XmlDoc = null;
}
static Dictionary<string, List<string>> ReadMD5File(string fileName)
{
Dictionary<string, List<string>> DicMD5 = new Dictionary<string, List<string>>();
// 如果文件不存在,则直接返回
if (System.IO.File.Exists(fileName) == false)
return DicMD5;
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(fileName);
XmlElement XmlRoot = XmlDoc.DocumentElement;
foreach (XmlNode node in XmlRoot.ChildNodes)
{
if ((node is XmlElement) == false)
continue;
List<string> tempList = new List<string>();
string file = (node as XmlElement).GetAttribute("FileName");
string md5 = (node as XmlElement).GetAttribute("MD5");
string size = (node as XmlElement).GetAttribute("Size");
tempList.Add(md5);
tempList.Add(size);
if (DicMD5.ContainsKey(file) == false)
{
DicMD5.Add(file, tempList);
}
}
XmlRoot = null;
XmlDoc = null;
return DicMD5;
}
}