178 lines
6.3 KiB
C#
178 lines
6.3 KiB
C#
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ZXKFrameworkEditor
|
|
{
|
|
public enum BuildAbTable
|
|
{
|
|
PC,
|
|
Android,
|
|
IOS,
|
|
WebGL
|
|
}
|
|
|
|
public enum BuildAbPath
|
|
{
|
|
StreamingAssets,
|
|
PersistentData,
|
|
}
|
|
|
|
public class ProgectBuildAssetbundleData
|
|
{
|
|
public string progectName = "";
|
|
}
|
|
|
|
//工程打成AB包
|
|
public class ProgectBuildAssetbundle : EditorWindow
|
|
{
|
|
ProgectBuildAssetbundleData loProgectBuildAssetbundleData = new ProgectBuildAssetbundleData();
|
|
private bool isLoad = false;
|
|
|
|
[UnityEditor.MenuItem("ZXKFramework/AssetBundle/ProgectBuildAssetbundle")]
|
|
static void ExceltoJson()
|
|
{
|
|
ProgectBuildAssetbundle toJson = (ProgectBuildAssetbundle)EditorWindow.GetWindow(typeof(ProgectBuildAssetbundle), true, "ProgectBuildAssetbundle");
|
|
toJson.Show();
|
|
}
|
|
|
|
BuildAbTable mBuildAbTable;
|
|
BuildAbPath mBuildAbPath;
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (!isLoad)
|
|
{
|
|
isLoad = true;
|
|
Load();
|
|
}
|
|
|
|
GUILayout.Label("------工程打成AB包", EditorStyles.boldLabel);
|
|
|
|
GUILayout.BeginHorizontal();
|
|
GUILayout.Label("项目名称(必填)");
|
|
loProgectBuildAssetbundleData.progectName = GUILayout.TextField(loProgectBuildAssetbundleData.progectName, GUILayout.Width(300));
|
|
GUILayout.EndHorizontal();
|
|
|
|
mBuildAbTable = (BuildAbTable)EditorGUILayout.EnumPopup("打包类型:", mBuildAbTable);
|
|
mBuildAbPath = (BuildAbPath)EditorGUILayout.EnumPopup("打包位置:", mBuildAbPath);
|
|
|
|
if (GUILayout.Button("开始打包"))
|
|
{
|
|
string loPath = "";
|
|
switch (mBuildAbPath)
|
|
{
|
|
case BuildAbPath.StreamingAssets:
|
|
loPath += Application.streamingAssetsPath + "/";
|
|
break;
|
|
case BuildAbPath.PersistentData:
|
|
loPath += Application.persistentDataPath + "/";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
loPath += loProgectBuildAssetbundleData.progectName + "/Assetbundle/";
|
|
loPath += mBuildAbTable.ToString() + "/";
|
|
BuildTarget mBuildTarget = BuildTarget.StandaloneWindows;
|
|
switch (mBuildAbTable)
|
|
{
|
|
case BuildAbTable.PC:
|
|
mBuildTarget = BuildTarget.StandaloneWindows;
|
|
break;
|
|
case BuildAbTable.Android:
|
|
mBuildTarget = BuildTarget.Android;
|
|
break;
|
|
case BuildAbTable.IOS:
|
|
mBuildTarget = BuildTarget.iOS;
|
|
break;
|
|
case BuildAbTable.WebGL:
|
|
mBuildTarget = BuildTarget.WebGL;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
Build(loPath, mBuildTarget);
|
|
}
|
|
}
|
|
|
|
public void Build(string cPath, BuildTarget target)
|
|
{
|
|
if (string.IsNullOrEmpty(loProgectBuildAssetbundleData.progectName)) return;
|
|
Save();
|
|
|
|
Debug.Log($"打包位置 {cPath} 打包类型 {target}");
|
|
|
|
//去掉名称
|
|
string[] oldABNames = AssetDatabase.GetAllAssetBundleNames();
|
|
for (int i = 0; i < oldABNames.Length; i++)
|
|
{
|
|
AssetDatabase.RemoveAssetBundleName(oldABNames[i], true);
|
|
}
|
|
|
|
//EditorTools.DeleteAllFile(cPath);//清空资源
|
|
EditorTools.CreateDirectory(cPath);
|
|
|
|
//StreamingAssets 进行整体复制
|
|
//string streamingAssetsDataPath = cPath + "_StreamingAssets/";
|
|
//EditorTools.CreateDirectory(streamingAssetsDataPath);
|
|
//EditorTools.CopyDirIntoDestDirectory(Application.streamingAssetsPath, streamingAssetsDataPath, true);
|
|
|
|
//设置特殊文件夹Ab包名_Scenes
|
|
string loPath = Application.dataPath + "/Resources/ABLoad/";
|
|
Debug.Log("SetABNameDic:" + loPath);
|
|
SetABNameDic(loPath);
|
|
|
|
//开始打包
|
|
BuildPipeline.BuildAssetBundles(cPath, BuildAssetBundleOptions.None, target);
|
|
AssetDatabase.Refresh();
|
|
Debug.Log("打包成功:" + cPath);
|
|
}
|
|
|
|
|
|
void SetABNameDic(string path)
|
|
{
|
|
if (Directory.Exists(path))
|
|
{
|
|
DirectoryInfo TheFolder = new DirectoryInfo(path);
|
|
foreach (FileInfo NextFile in TheFolder.GetFiles())
|
|
{
|
|
if (NextFile.FullName.EndsWith(".meta") || NextFile.FullName.EndsWith(".cs") || NextFile.FullName.EndsWith(".xlsx")) continue;
|
|
string abName = Path.GetFileNameWithoutExtension(NextFile.Name).ToLower();
|
|
string loFullName = EditorTools.PathToNormal(NextFile.FullName);
|
|
string loAbPath = "Assets/" + loFullName.Replace(Application.dataPath, "");
|
|
abName = EditorTools.PathToNormal(Path.GetDirectoryName(loAbPath)).Replace("Assets/", "") + "/" + abName;
|
|
SetABName(abName, loAbPath);
|
|
}
|
|
foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
|
|
{
|
|
SetABNameDic(NextFolder.FullName);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetABName(string name, string path)
|
|
{
|
|
AssetImporter assetImporter = AssetImporter.GetAtPath(path);
|
|
if (assetImporter == null)
|
|
{
|
|
Debug.LogError("不存在此路径文件:" + path);
|
|
}
|
|
else
|
|
{
|
|
assetImporter.assetBundleName = name + ".assetbundle";
|
|
}
|
|
}
|
|
|
|
string saveName = "FrameworkAutoCreateCreateCSData";
|
|
void Save()
|
|
{
|
|
EditorTools.Save(saveName, loProgectBuildAssetbundleData);
|
|
}
|
|
void Load()
|
|
{
|
|
loProgectBuildAssetbundleData = EditorTools.Load<ProgectBuildAssetbundleData>(saveName);
|
|
}
|
|
}
|
|
} |