Unity AssetBundle一键命名并打包
代码
将脚本放在项目里Editor文件夹下,设置好要打包的预制体的文件夹路径和打包出来的资源包路径,就可以一键完成预制体的命名和打包
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
/// <summary>
/// 先指定要打包的文件夹,点击后会把整个文件夹里东西自动命名,并打包指定到另一个文件夹
/// </summary>
public class AssetbundlePerfabs : Editor
{
public static string sourcePath = Application.dataPath + "/_Project/Public Resource/Model";//要打包的资源目录
const string AssetBundlesOutputPath = "D:/VMNEW/Trunk/Resources/Model32";//打包完的指定目录
[MenuItem("Tools/AssetBundle/Build")]
public static void BuildAssetBundle()
{
ClearAssetBundlesName();
Pack(sourcePath);
string outputPath = AssetBundlesOutputPath;
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
AssetDatabase.Refresh();
}
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames().Length;
Debug.Log(length);
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
}
for (int j = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
}
length = AssetDatabase.GetAllAssetBundleNames().Length;
}
static void Pack(string source)
{
DirectoryInfo folder = new DirectoryInfo(source);
FileSystemInfo[] files = folder.GetFileSystemInfos();
int length = files.Length;
for (int i = 0; i < length; i++)
{
if (files[i] is DirectoryInfo)
{
Pack(files[i].FullName);
}
else
{
if (!files[i].Name.EndsWith(".meta"))
{
file(files[i].FullName);
//Debug.Log(files[i].FullName);
}
}
}
}
static void file(string source)
{
string _source = Replace(source);//替换"\\"为"/"
string _assetPath = "Assets" + _source.Substring(Application.dataPath.Length);
string _assetPath2 = _source.Substring(Application.dataPath.Length + 1);
AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);
string assetName = _assetPath2.Substring(_assetPath2.IndexOf("/") + 1);
string a = assetName.Substring(assetName.IndexOf("/") + 1);
string b = a.Substring(a.IndexOf("/") + 1);
b = b.Replace(Path.GetExtension(b), ".assetbundle");
assetImporter.assetBundleName = b;
Debug.Log(b);
}
static string Replace(string s)
{
return s.Replace("\\", "/");
}
//public class Platform
//{
// public static string GetPlatformFolder(BuildTarget target)
// {
// switch (target)
// {
// case BuildTarget.Android:
// return "Android";
// case BuildTarget.iOS:
// return "IOS";
// case BuildTarget.StandaloneWindows64:
// return "Windows";
// default:
// return null;
// }
// }
//}
}本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!