using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System;
public class qfAssetBunldes_Builder : Editor
{
static List
static List
static List
static string assetsPath = "Assets/StreamingAssets/";
//微软平台
[MenuItem("qfAssetBunldes Editor/Create AssetBunldes Windows")]
static void CreateAssetBunldes_Windows()
{
CreateAssetBunldes(BuildTarget.StandaloneWindows, "win");
}
//安卓平台
[MenuItem("qfAssetBunldes Editor/Create AssetBunldes Android")]
static void CreateAssetBunldes_Android()
{
CreateAssetBunldes(BuildTarget.Android, "story1");
}
//苹果平台
[MenuItem("qfAssetBunldes Editor/Create AssetBunldes IOS")]
static void CreateAssetBunldes_IOS()
{
CreateAssetBunldes(BuildTarget.iOS, "ios");
}
//所有平台
[MenuItem("qfAssetBunldes Editor/Create AssetBunldes ALL Platform")]
static void CreateAssetBunldes_ALL_Platform()
{
CreateAssetBunldes_Windows();
CreateAssetBunldes_Android();
CreateAssetBunldes_IOS();
}
//所有打包资源名称
[MenuItem("qfAssetBunldes Editor/Clear All AssetBunldesName")]
static void ClearAssetBunldes()
{
string[] step = AssetDatabase.GetAllAssetBundleNames();
for (int i = 0; i < step.Length; i++)
{
AssetDatabase.RemoveAssetBundleName(step[i], true);
}
}
[MenuItem("qfAssetBunldes Editor/Set AssetbundleName")]
private static void SetAssetBundleNames()
{
string tempSelectedFolder = "";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets))
{
tempSelectedFolder = AssetDatabase.GetAssetPath(obj);
FileInfo fi = new FileInfo(tempSelectedFolder);
if (fi.Exists)
{
if (!fi.FullName.EndsWith(".meta"))
{
AssetImporter tempAImporter = AssetImporter.GetAtPath(tempSelectedFolder);
string root = System.IO.Path.GetDirectoryName(tempSelectedFolder);
string name = System.IO.Path.GetFileNameWithoutExtension(tempSelectedFolder);
tempSelectedFolder = root + @"/" + name;
tempSelectedFolder = tempSelectedFolder.Replace(@"\", " / ");
tempSelectedFolder = tempSelectedFolder.Replace(" ", "");
//tempSelectedFolder = tempSelectedFolder.Substring(tempSelectedFolder.IndexOf("Assets/") + 7);
tempSelectedFolder = tempSelectedFolder.Replace("Assets/","ziyuan/");
tempAImporter.assetBundleName = tempSelectedFolder;
}
}
}
}
///
/// 生成绑定素材
///
static void CreateAssetBunldes(BuildTarget platform, string plarform_dir_name)
{
string dir_path = PathUtil.getDataPath();
string dir_path_platform = dir_path + "/" + plarform_dir_name;
//System.IO.Directory.CreateDirectory(dir_path);
System.IO.Directory.CreateDirectory(dir_path_platform);
Debug.Log("输出文件路径" + dir_path + "--" + dir_path_platform);
BuildPipeline.BuildAssetBundles(dir_path_platform, BuildAssetBundleOptions.None, platform);
//CreatTxt(dir_path);
BuildFileIndex();
}
private static void BuildFileIndex()
{
string resPath = AppDataPath + "/StreamingAssets/";
///----------------------创建文件列表-----------------------
string newFilePath = resPath + "/files.txt";
if (File.Exists(newFilePath)) File.Delete(newFilePath);
paths.Clear(); files.Clear();
Recursive(resPath);
FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
StreamWriter sw = new StreamWriter(fs);
//开始写入版本号
sw.WriteLine("1.0.0");
for (int i = 0; i < files.Count; i++)
{
string file = files[i];
string ext = Path.GetExtension(file);
if (file.EndsWith(".meta") || file.Contains(".DS_Store")) continue;
string md5 = Util.md5file(file);
string value = file.Replace(resPath, string.Empty);
sw.WriteLine(value + "|" + md5);
}
sw.Close(); fs.Close();
}
///
/// 数据目录
///
static string AppDataPath
{
get { return Application.dataPath.ToLower(); }
}
///
/// 遍历目录及其子目录
///
static void Recursive(string path)
{
string[] names = Directory.GetFiles(path);
string[] dirs = Directory.GetDirectories(path);
foreach (string filename in names)
{
string ext = Path.GetExtension(filename);
if (ext.Equals(".meta")) continue;
files.Add(filename.Replace('\\', '/'));
}
foreach (string dir in dirs)
{
paths.Add(dir.Replace('\\', '/'));
Recursive(dir);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class Util
{
///
/// 计算文件的MD5值
///
public static string md5file(string file)
{
try
{
FileStream fs = new FileStream(file, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fs);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("md5file() fail, error:" + ex.Message);
}
}
}