Skip to content

Commit e590858

Browse files
author
MichaelO
committed
modified replace by hash feature
1 parent cc3e394 commit e590858

File tree

5 files changed

+59
-60
lines changed

5 files changed

+59
-60
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog (unofficial)
22

3+
## [1.6.1] - 2022-10-06
4+
- Modified ReplaceByHash feature load manifest from build folder to replace, not use append hash to bundle name to replace.
5+
36
## [1.6.0] - 2022-10-05
47
- Added WithoutManifest feature for build advenced settings.
58
- Added ReplaceByHash feature for build advenced settings.

Editor/AssetBundleBuildTab.cs

+35-49
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Runtime.Serialization.Formatters.Binary;
6-
76
using AssetBundleBrowser.AssetBundleDataSource;
8-
using System.Linq;
97

108
namespace AssetBundleBrowser
119
{
@@ -182,7 +180,7 @@ internal void OnEnable(EditorWindow parent)
182180

183181
m_TargetContent = new GUIContent("Build Target", "Choose target platform to build for.");
184182
m_CompressionContent = new GUIContent("Compression", "Choose no compress, standard (LZMA), or chunk based (LZ4)");
185-
m_BundleNameContent = new GUIContent("Bundle Name", "Choose none, append hash, or replace by hash (Including manifest)");
183+
m_BundleNameContent = new GUIContent("Bundle Name", "Choose none, append hash, or replace by hash");
186184

187185
if (m_UserData.m_UseDefaultPath)
188186
{
@@ -396,10 +394,7 @@ private void ExecuteBuild()
396394
if (m_UserData.m_BundleNameOption == BundleNameOptions.AppendHash)
397395
opt |= BuildAssetBundleOptions.AppendHashToAssetBundleName;
398396
else if (m_UserData.m_BundleNameOption == BundleNameOptions.ReplaceByHash)
399-
{
400-
opt |= BuildAssetBundleOptions.AppendHashToAssetBundleName;
401397
replaceByHash = true;
402-
}
403398

404399
// toggle options
405400
foreach (var tog in m_ToggleData)
@@ -494,73 +489,64 @@ private void ResetPathToDefault()
494489
/// Remove manifest file from build folder
495490
/// </summary>
496491
/// <param name="outputDirectory"></param>
497-
internal static void WithoutManifestFile(string outputDirectory)
492+
internal static bool WithoutManifestFile(string outputDirectory)
498493
{
499494
// filter only extension is manifest
500495
string[] files = Directory.GetFiles(outputDirectory, "*.manifest", SearchOption.AllDirectories);
501496
foreach (string file in files)
502497
{
503498
if (File.Exists(file)) File.Delete(file);
504499
}
500+
501+
return true;
505502
}
506503

507504
/// <summary>
508-
/// Repace bundle name by hash (keep hash)
505+
/// Repace bundle name by hash (read hash from manifest to replace)
509506
/// </summary>
510507
/// <param name="outputDirectory"></param>
511-
internal static void ReplaceBundleNameByHash(string outputDirectory)
508+
internal static bool ReplaceBundleNameByHash(string outputDirectory)
512509
{
513-
// set replacement map for manifest file to replace hash
514-
Dictionary<string, string> dictReplacement = new Dictionary<string, string>();
510+
// outputDirectory last path name = manifestName
511+
var firstIdx = outputDirectory.LastIndexOf("\\") == -1 ? outputDirectory.LastIndexOf("/") : outputDirectory.LastIndexOf("\\");
512+
string manifestName = outputDirectory.Substring(firstIdx + 1, outputDirectory.Length - firstIdx - 1);
513+
514+
string manifestFullPath = string.Empty;
515515

516-
// get all file
516+
// search from all file to find menifest
517517
string[] files = Directory.GetFiles(outputDirectory, "*.*", SearchOption.AllDirectories);
518518
foreach (var file in files)
519519
{
520-
// get fileName without extension
521-
string fileName = Path.GetFileNameWithoutExtension(file);
522-
523-
// Hash128 = 16 bytes (hex str => 0xFF = 1 bytes = 1 set, 16 * 2 = 32)
524-
int hashSize = 32;
525-
526-
// append hash has _
527-
var lastIdx = file.LastIndexOf("_");
528-
// path \ for win, / for mac, linux
529-
var firstIdx = (file.LastIndexOf("\\") == -1 ? file.LastIndexOf("/") : file.LastIndexOf("\\"));
530-
531-
// fileName.Length > hashSize = including hash code
532-
if (lastIdx != -1 && firstIdx != -1 && fileName.Length > hashSize)
520+
if (file.IndexOf(manifestName) != -1)
533521
{
534-
string originFileName = file.Substring(firstIdx + 1, lastIdx - firstIdx - 1);
535-
string hash = file.Substring(firstIdx + 2 + originFileName.Length, hashSize);
536-
537-
// set pair
538-
dictReplacement.Add(originFileName, hash);
539-
540-
// replace origin file name to empty (only keep hash code)
541-
string newFile = file.Replace($"{originFileName}_", string.Empty);
542-
543-
// rename it
544-
if (File.Exists(file)) File.Move(file, newFile);
522+
manifestFullPath = Path.GetFullPath(file);
523+
break;
545524
}
546525
}
547526

548-
// get all manifest file
549-
string[] manifestFiles = Directory.GetFiles(outputDirectory, "*.manifest", SearchOption.AllDirectories);
550-
foreach (string file in manifestFiles)
527+
// file stream to read manifest
528+
var fs = new FileStream(manifestFullPath, FileMode.Open, FileAccess.Read, FileShare.None);
529+
var bundle = AssetBundle.LoadFromStream(fs);
530+
fs.Dispose();
531+
532+
// load manifest asset
533+
AssetBundleManifest manifest = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
534+
535+
// replace all bundle file name by hash
536+
foreach (var file in files)
551537
{
552-
foreach (var pair in dictReplacement)
553-
{
554-
string originFileName = pair.Key;
555-
string hash = pair.Value;
538+
string bundleName = file.Replace(outputDirectory, string.Empty);
539+
bundleName = bundleName.Substring(1, bundleName.Length - 1);
540+
// skip process manifest & .manifest extension
541+
if (bundleName.IndexOf(manifestName) != -1 || file.IndexOf(".manifest") != -1) continue;
556542

557-
if (file.IndexOf(originFileName) != -1)
558-
{
559-
string newFile = file.Replace(originFileName, hash);
560-
if (File.Exists(file)) File.Move(file, newFile);
561-
}
562-
}
543+
string fileName = Path.GetFileNameWithoutExtension(file);
544+
string hash = manifest.GetAssetBundleHash(bundleName).ToString();
545+
string newFile = file.Replace(fileName, hash);
546+
if (File.Exists(file)) File.Move(file, newFile);
563547
}
548+
549+
return true;
564550
}
565551

566552
//Note: this is the provided BuildTarget enum with some entries removed as they are invalid in the dropdown

Editor/BundleBuildMap.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -547,19 +547,28 @@ public static bool BuildAssetBundles(string outputDirectory, AssetBundleBuild[]
547547
{
548548
if (!Directory.Exists(outputDirectory)) Directory.CreateDirectory(outputDirectory);
549549

550-
// set option first
551-
if (replaceByHash) options |= BuildAssetBundleOptions.AppendHashToAssetBundleName;
552-
553550
var buildManifest = BuildPipeline.BuildAssetBundles(outputDirectory, buildMap, options, buildTarget);
554551
if (buildManifest == null)
555552
{
556553
Debug.Log("Error in build");
557554
return false;
558555
}
559556

560-
// after build
561-
if (withoutManifest) AssetBundleBuildTab.WithoutManifestFile(outputDirectory);
562-
if (replaceByHash) AssetBundleBuildTab.ReplaceBundleNameByHash(outputDirectory);
557+
// after build (without manifest)
558+
if (withoutManifest)
559+
{
560+
bool completes = AssetBundleBuildTab.WithoutManifestFile(outputDirectory);
561+
if (!completes) Debug.Log("Error in process remove manifest.");
562+
else Debug.Log($"<color=#60ffb0>Remove all manifest file completes.</color>");
563+
}
564+
565+
// after build (replace by hash)
566+
if (replaceByHash)
567+
{
568+
bool completes = AssetBundleBuildTab.ReplaceBundleNameByHash(outputDirectory);
569+
if (!completes) Debug.Log("Error in process replace by hash.");
570+
else Debug.Log($"<color=#60ffb0>Replace all bundle name by hash completes.</color>");
571+
}
563572

564573
if (onBuild != null)
565574
{

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,22 @@ When enabled sync feature can sync asset data to other BuildMap <font color=#005
5454
return;
5555
}
5656
57-
// outPath
58-
string fullBundleOutPath = Path.Combine(Application.dataPath, $"../AssetBundles/MyBundle");
57+
// output path
58+
string outputDirectory = Path.Combine(Application.dataPath, $"../AssetBundles/MyBundle");
5959
6060
// platform
6161
BuildTarget target = BuildTarget.StandaloneWindows;
6262
6363
// LZ4
6464
BuildAssetBundleOptions bundleOptions = BuildAssetBundleOptions.ChunkBasedCompression;
6565
66-
BundleBuildMap.BuildAssetBundles(fullBundleOutPath, bundleBuildMap.GetBuildMap(), bundleOptions, target, null);
66+
// regular
67+
BundleBuildMap.BuildAssetBundles(outputDirectory, bundleBuildMap.GetBuildMap(), bundleOptions, target, null);
6768
6869
// including withoutManifest and replaceByHash params
6970
bool withoutManifest = true;
7071
bool replaceByHash = true;
71-
BundleBuildMap.BuildAssetBundles(fullBundleOutPath, bundleBuildMap.GetBuildMap(), bundleOptions, target, withoutManifest, replaceByHash, null);
72+
BundleBuildMap.BuildAssetBundles(outputDirectory, bundleBuildMap.GetBuildMap(), bundleOptions, target, withoutManifest, replaceByHash, null);
7273
```
7374

7475
### Extension Advenced Settings

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name": "com.unity.assetbundlebrowser.plus",
44
"displayName": "Asset Bundle Browser Plus",
5-
"version": "1.6.0",
5+
"version": "1.6.1",
66
"unity": "2018.1",
77
"description": "The Asset Bundle Browser tool enables the user to view and edit the configuration of asset bundles for their Unity project. It will block editing that would create invalid bundles, and inform you of any issues with existing bundles. It also provides basic build functionality.\n\nUse this tool as an alternative to selecting assets and setting their asset bundle manually in the inspector. It can be dropped into any Unity project with a version of 5.6 or greater. It will create a new menu item in Window > AssetBundle Browser. The bundle configuration, build functionality, and built-bundle inspection are split into three tabs within the new window.",
88
"keywords": ["asset", "bundle", "bundles", "assetbundles"],

0 commit comments

Comments
 (0)