Hi guys, I’m trying to solve the problem related to Addressable to create short folders in the build.
The script managed to reduce folders and files inside them, but the trouble is the game does not see the resources ![]()
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
// This class processes the build after it is completed
public class PostBuildProcessor : IPostprocessBuildWithReport
{
// The order in which the callback is invoked
public int callbackOrder => 999;
// This method is called after the build is completed
public void OnPostprocessBuild(BuildReport report)
{
string originalBuildPath = report.summary.outputPath;
string rootPath = Path.GetDirectoryName(originalBuildPath);
string platformFolder = "";
string streamingAssetsPath = "";
// Determine the platform and set the appropriate paths
switch (report.summary.platform)
{
case BuildTarget.StandaloneWindows64:
platformFolder = "StandaloneWindows64";
streamingAssetsPath = Path.Combine(rootPath, Path.GetFileNameWithoutExtension(originalBuildPath) + "_Data", "StreamingAssets", "aa", platformFolder);
DeleteWindowsCrashHandler(rootPath);
break;
case BuildTarget.WebGL:
platformFolder = "WebGL";
streamingAssetsPath = Path.Combine(originalBuildPath, "StreamingAssets", "aa", platformFolder);
break;
case BuildTarget.StandaloneOSX:
platformFolder = "StandaloneOSX";
streamingAssetsPath = Path.Combine(originalBuildPath, "Contents", "Resources", "Data", "StreamingAssets", "aa", platformFolder);
break;
default:
return;
}
// If the StreamingAssets path does not exist, return
if (!Directory.Exists(streamingAssetsPath))
{
return;
}
// Process the directory and log the results
int processedFiles = ProcessDirectory(streamingAssetsPath);
Debug.Log($"Platform: {report.summary.platform}, Path: {streamingAssetsPath}, Renamed files: {processedFiles}");
}
// Deletes the Windows crash handler executable if it exists
private void DeleteWindowsCrashHandler(string buildPath)
{
string crashHandlerPath = Path.Combine(buildPath, "UnityCrashHandler64.exe");
if (File.Exists(crashHandlerPath))
{
try
{
File.Delete(crashHandlerPath);
}
catch
{
// Ignore deletion errors
}
}
}
// Processes the directory by renaming files and subdirectories
private int ProcessDirectory(string dirPath)
{
int processedCount = 0;
// Process subdirectories
foreach (string subDir in Directory.GetDirectories(dirPath))
{
processedCount += ProcessDirectory(subDir);
string folderName = Path.GetFileName(subDir);
string shortName = GetShortenedName(folderName);
string hash = GenerateHash(folderName, 10);
string newFolderPath = Path.Combine(dirPath, $"{shortName}-{hash}");
if (subDir != newFolderPath)
{
Directory.Move(subDir, newFolderPath);
processedCount++;
}
}
// Process files
foreach (string file in Directory.GetFiles(dirPath))
{
string fileName = Path.GetFileName(file);
if (fileName == "catalog.json" || fileName == "settings.json")
continue;
string shortName = GetShortenedName(fileName);
string hash = GenerateHash(fileName, 10);
string newFilePath = Path.Combine(dirPath, $"{shortName}-{hash}{Path.GetExtension(file)}");
if (file != newFilePath)
{
File.Move(file, newFilePath);
processedCount++;
}
}
return processedCount;
}
// Shortens the name to 8 characters
private string GetShortenedName(string name)
{
return new string(name.Take(8).ToArray());
}
// Generates a hash of the input string
private string GenerateHash(string input, int length)
{
using (var sha = SHA256.Create())
{
byte[] hashBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
return BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, length);
}
}
}
Can you tell me how to tweak the script to write information about files and folders in catalog.json or settings.json correctly. Or perhaps there is another solution to achieve my goal?
Guys I am a complete newbie in unity, do not judge strictly the code written by GPT I do not understand a lot yet, but I am learning.

