Skip to content

Commit

Permalink
Merge pull request #32 from pokong/main
Browse files Browse the repository at this point in the history
打包的AssetBundles复制到Assets外Bundles内,并且加上平台名进行区分。
热更新打包地址也增加平台进行区分。
开发工具进行了排序。
添加HybridCLR的补充元数据。
  • Loading branch information
TippingGame authored Mar 5, 2025
2 parents 0cf47d0 + f0deaf8 commit 353b06f
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 13 deletions.
69 changes: 66 additions & 3 deletions Editor/AssetManager/ABBuildTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void BuildAllAB()
{
AssetDatabase.RemoveUnusedAssetBundleNames();

// 获取“StreamingAssets”文件夹路径(不一定这个文件夹,可自定义)
// 获取“StreamingAssets”文件夹路径(不一定这个文件夹,可自定义)
string strABOutPAthDir = URLSetting.GetAssetBundlesOutPath();

GenerateAssetNames();
Expand All @@ -45,11 +45,17 @@ public static void BuildAllAB()
// 清理多余文件夹和ab
DeleteRemovedAssetBundles();

//复制AB到steam打包目录
string outpath = URLSetting.GetAssetBundlesStreamPath();
FileTools.SafeClearDir(outpath);
FileTools.CheckDirAndCreateWhenNeeded(outpath);
FileTools.SafeCopyDirectory(strABOutPAthDir, outpath, true);

// 等待AB打包完成,再写入数据
GenerateAssetNames(true);
GenerateResourceNames(true);
LogF8.LogAsset("写入资产数据 生成:AssetBundleMap.json,生成:ResourceMap.json");

AssetDatabase.Refresh();

LogF8.LogAsset("资产打包成功!");
Expand Down Expand Up @@ -173,7 +179,8 @@ public static string SetAssetBundleName(string path)
{
bundleName = Path.ChangeExtension(path, null).Replace(URLSetting.AssetBundlesPath, "");
}


bundleName = bundleName.ToLower();
if (!ai.assetBundleName.Equals(bundleName))
{
if (ai.assetBundleName.IsNullOrEmpty())
Expand All @@ -184,20 +191,76 @@ public static string SetAssetBundleName(string path)
else if (DiscrepantAssetPathMapping != null)
{
// 资产名和ab包名不相等
if (!AssetGetParentPath(ai.assetBundleName).Equals(AssetGetParentPath(bundleName)))
{
//打印出父路径
Debug.LogError("资产名和ab包名不相等:"+ai.assetBundleName + " > " + bundleName);
}
DiscrepantAssetPathMapping["/" + ai.assetBundleName] = "/" + bundleName.ToLower();
}

}
return ai.assetBundleName;
}

//得到上级路径
private static string AssetGetParentPath(string path)
{
string parentPath = path.Substring(0, path.LastIndexOf('/'));
return parentPath;
}

private static bool AssetPathsContainsDiscrepantAssetBundle(List<string> assetPaths, string ab)
{
if (DiscrepantAssetPathMapping.TryGetValue(ab, out string disPath))
return assetPaths.Contains(disPath);
return false;
}

//清除AssetBundleNames
public static void ClearAllAssetNames()
{
ClearAssetNames();
}

public static void ClearAssetNames()
{
FileTools.CheckDirAndCreateWhenNeeded(URLSetting.GetAssetBundlesFolder());
if (Directory.Exists(URLSetting.GetAssetBundlesFolder()))
{
// 获取文件夹的路径
string[] folderPaths = Directory.GetDirectories(URLSetting.GetAssetBundlesFolder(), "*", SearchOption.AllDirectories);
// 获取文件的路径
string[] filePaths = Directory.GetFiles(URLSetting.GetAssetBundlesFolder(), "*", SearchOption.AllDirectories);
// 合并文件夹和文件的路径,可以根据需要调整顺序
string[] allPaths = filePaths.Concat(folderPaths).ToArray();

List<string> tempNames = new List<string>();

assetMapping = new Dictionary<string, AssetBundleMap.AssetMapping>();

foreach (string _filePath in allPaths)
{
// 排除.meta文件 .DS_Store文件
if (Path.GetExtension(_filePath) == ".meta" || Path.GetExtension(_filePath) == ".DS_Store")
{
continue;
}
string filePath = FileTools.FormatToUnityPath(_filePath);

// 获取GetAssetPath
string assetPath = GetAssetPath(filePath);

if (File.Exists(filePath)) // 文件
{
AssetImporter ai = AssetImporter.GetAtPath(assetPath);
ai.assetBundleName = "";
EditorUtility.SetDirty(ai);
}
}
}
}

public static void GenerateAssetNames(bool isWrite = false)
{
if (isWrite) DiscrepantAssetPathMapping = null;
Expand Down
12 changes: 9 additions & 3 deletions Editor/Build/BuildPkgTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BuildPkgTool : ScriptableObject
private static bool _enableOptionalPackage = false;
private static bool _enableNullPackage = false;
private static string _optionalPackage = "0_1_2_3";
private static string _assetRemoteAddress = "http://127.0.0.1:6789" + HotUpdateManager.RemoteDirName;
private static string _assetRemoteAddress = ""; //"http://127.0.0.1:6789/"
private static bool _enablePackage = false;

private static BuildTarget _buildTarget = BuildTarget.NoTarget;
Expand Down Expand Up @@ -370,7 +370,11 @@ public static void DrawHotUpdate()

GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("资产远程地址/游戏远程版本:", GUILayout.Width(160));
GUILayout.Label("资产远程地址/游戏远程版本 例:http://127.0.0.1:6789/", GUILayout.Width(360));
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Space(10);
string assetRemoteAddressValue = F8EditorPrefs.GetString(_assetRemoteAddressKey, "");
if (string.IsNullOrEmpty(assetRemoteAddressValue))
{
Expand Down Expand Up @@ -649,7 +653,9 @@ public static void WriteGameVersion()

string toVersion = F8EditorPrefs.GetString(_toVersionKey, "");

string assetRemoteAddress = F8EditorPrefs.GetString(_assetRemoteAddressKey, "");
// string assetRemoteAddress = F8EditorPrefs.GetString(_assetRemoteAddressKey, "");
//http://192.168.11.69/sgyyweb/Remote//Windows //远程资源增加区分平台
string assetRemoteAddress = F8EditorPrefs.GetString(_assetRemoteAddressKey, "") + "Remote/" + URLSetting.GetPlatformName();

bool enableHotUpdate = F8EditorPrefs.GetBool(_enableHotUpdateKey, false);

Expand Down
62 changes: 57 additions & 5 deletions Editor/F8Helper/F8Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,90 @@ public static void SwitchIsEditorMode()
F8EditorPrefs.SetBool("IsEditorMode", !isEditorMode);
}

[MenuItem("开发工具/F8Run _F8")]

[MenuItem("开发工具/清除AssetBundleNames")]
public static void ClearAssetBundlesName()
{
ABBuildTool.ClearAllAssetNames();
}

[MenuItem("开发工具/1: F8Run _F8",false,200)]
public static void F8Run()
{
LoadAllExcelData();
F8EditorPrefs.SetBool("compilationFinishedHotUpdateDll", true);
F8EditorPrefs.SetBool("compilationFinishedBuildAB", true);
LoadAllExcelData();
}

[MenuItem("开发工具/生成并复制热更新Dll-F8")]
[MenuItem("开发工具/3: 生成并复制热更新Dll-F8",false,210)]
public static void GenerateCopyHotUpdateDll()
{
// F8EditorPrefs.SetBool("compilationFinishedHotUpdateDll", false);
// HybridCLR.Editor.Commands.PrebuildCommand.GenerateAll();

// string outpath = Application.dataPath + "/AssetBundles/Code";

// //临时目录空的话就创建
// if (Directory.Exists(outpath))
// {
// //删除临时目录下的所有文件
// foreach (string filePath in Directory.GetFiles(outpath))
// {
// File.Delete(filePath);
// }
// }
// else
// {
// //创建目录
// Directory.CreateDirectory(outpath);
// }

// FileTools.SafeClearDir(Application.dataPath + "/AssetBundles/Code");
// FileTools.CheckDirAndCreateWhenNeeded(Application.dataPath + "/AssetBundles/Code");
// foreach (var dll in HybridCLR.Editor.SettingsUtil.HotUpdateAssemblyNamesExcludePreserved) // 获取HybridCLR设置面板的dll名称
// {
// var path =
// HybridCLR.Editor.SettingsUtil.GetHotUpdateDllsOutputDirByTarget(EditorUserBuildSettings
// .activeBuildTarget) + "/" + dll + ".dll";
// Debug.Log("dll:"+path);
// FileTools.SafeCopyFile(
// HybridCLR.Editor.SettingsUtil.GetHotUpdateDllsOutputDirByTarget(EditorUserBuildSettings.activeBuildTarget) + "/" + dll + ".dll",
// Application.dataPath + "/AssetBundles/Code/" + dll + ".bytes");
// LogF8.LogAsset("生成并复制热更新dll:" + dll);
// }

// //补充元数据
// List<string> aotDllList = new List<string>
// {
// "mscorlib.dll",
// "System.dll",
// "System.Core.dll", // 如果使用了Linq,需要这个
// // "Newtonsoft.Json.dll",
// // "protobuf-net.dll",
// };

// foreach (var aotDllName in aotDllList)
// {
// var mscorlibsouPath =
// HybridCLR.Editor.SettingsUtil.GetAssembliesPostIl2CppStripDir(EditorUserBuildSettings
// .activeBuildTarget) + "/" + aotDllName;

// FileTools.SafeCopyFile(
// mscorlibsouPath,
// Application.dataPath + "/AssetBundles/Code/"+ aotDllName +"by.bytes");
// LogF8.LogAsset("生成并复制源数据dll:"+aotDllName);
// }

// AssetDatabase.Refresh();
}

[MenuItem("开发工具/Excel导表-F8")]
[MenuItem("开发工具/2: Excel导表-F8",false,205)]
public static void LoadAllExcelData()
{
ExcelDataTool.LoadAllExcelData();
}

[MenuItem("开发工具/打包AssetBundles目录资源-F8")]
[MenuItem("开发工具/4: 打包AssetBundles目录资源-F8",false,215)]
public static void BuildAssetBundles()
{
F8EditorPrefs.SetBool("compilationFinishedBuildAB", false);
Expand Down
8 changes: 7 additions & 1 deletion Runtime/Common/URLSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public static string GetAssetBundlesFolder()

public static string GetAssetBundlesOutPath()
{
return Application.dataPath + "/StreamingAssets/" + AssetBundlesName + "/" + GetPlatformName();
// return Application.dataPath + "/StreamingAssets/" + AssetBundlesName + "/" + GetPlatformName();
return Application.dataPath + "/../Bundles/" + AssetBundlesName + "/" + GetPlatformName();
}

public static string GetAssetBundlesStreamPath()
{
return Application.dataPath + "/StreamingAssets/" + AssetBundlesName + "/" + GetPlatformName()+ "/";
}

public static string GetPlatformName()
Expand Down
7 changes: 6 additions & 1 deletion Runtime/HotUpdateManager/HotUpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class HotUpdateManager : ModuleSingleton<HotUpdateManager>, IModule
{
public static string Separator = "_";
public static string PackageSplit = "Package" + Separator;
public static string RemoteDirName = "/Remote";
// public static string RemoteDirName = "/Remote";
public static string RemoteDirName = "/Remote/" + URLSetting.GetPlatformName();
public static string HotUpdateDirName = "/HotUpdate";
public static string PackageDirName = "/Package";

Expand Down Expand Up @@ -52,6 +53,9 @@ public IEnumerator InitRemoteVersion()
}

string path = GameConfig.LocalGameVersion.AssetRemoteAddress + "/" + nameof(GameVersion) + ".json";
LogF8.Log($"初始化远程版本:{path}");


UnityWebRequest webRequest = UnityWebRequest.Get(path);
yield return webRequest.SendWebRequest();
#if UNITY_2020_2_OR_NEWER
Expand Down Expand Up @@ -81,6 +85,7 @@ public IEnumerator InitAssetVersion()
}

string path = GameConfig.LocalGameVersion.AssetRemoteAddress + "/HotUpdate" + Separator + nameof(AssetBundleMap) + ".json";
LogF8.Log($"始化资源版本:{path}");
UnityWebRequest webRequest = UnityWebRequest.Get(path);
yield return webRequest.SendWebRequest();
#if UNITY_2020_2_OR_NEWER
Expand Down

0 comments on commit 353b06f

Please sign in to comment.