Last active
August 31, 2021 19:03
-
-
Save strich/8063d423b868d6688259948040f1a373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
using UnityEditor; | |
/// <summary> | |
/// This post processor will detect when assets move in and out of a specified bundles directory | |
/// and assign the correct Asset Bundle name to it. | |
/// Current design is to create an asset bundle for each 2-folder deep combination. | |
/// For example /Assets/Bundles/Tilesets/Spacestation/* will set any folders and assets under it | |
/// to a bundle called 'tilesets-spacestation'. | |
/// </summary> | |
public class AssetBundleAssetPostProcessor : AssetPostprocessor | |
{ | |
private const string BundlesLocation = "Assets/Bundles"; | |
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { | |
foreach (var path in importedAssets) | |
{ | |
if(IsValidBundlePath(path)) UpdateBundleInformation(path); | |
} | |
for (var i = 0; i < movedAssets.Length; i++) | |
{ | |
if (IsValidBundlePath(movedAssets[i])) | |
{ | |
UpdateBundleInformation(movedAssets[i]); | |
} else if (IsValidBundlePath(movedFromAssetPaths[i])) | |
{ | |
var assetImporter = AssetImporter.GetAtPath(movedAssets[i]); | |
assetImporter.SetAssetBundleNameAndVariant("", ""); | |
} | |
} | |
} | |
private static void UpdateBundleInformation(string assetPath) { | |
var assetImporter = AssetImporter.GetAtPath(assetPath); | |
//var assetBundleName = GetParentAssetBundleName(assetPath); | |
SetParentFolderBundleNames(assetPath); | |
var assetBundleName = GetAssetBundleName(assetPath); | |
Debug.Log("Setting bundle for asset " + assetPath + " to " + assetBundleName); | |
assetImporter.SetAssetBundleNameAndVariant(assetBundleName, ""); | |
} | |
private static string GetAssetBundleName(string assetPath) { | |
var regex = new Regex(BundlesLocation + "/(.*?/.*?)/.*"); | |
var isFolder = !Path.HasExtension(assetPath); | |
var path = isFolder ? assetPath + "/" : Path.GetDirectoryName(assetPath) + "/"; | |
var match = regex.Match(path); | |
var assetBundleName = match.Groups[1].Value.Replace("/", "-").ToLower(); | |
return assetBundleName; | |
} | |
private static bool IsValidBundlePath(string path) | |
{ | |
if (!new Regex(BundlesLocation + "/.*").IsMatch(path)) return false; | |
var bundlesFolder = new Regex(BundlesLocation + "/.*?/.*?/"); | |
var isFolder = !Path.HasExtension(path); | |
var dir = isFolder ? path + "/" : Path.GetDirectoryName(path) + "/"; | |
var isMatch = bundlesFolder.IsMatch(dir); | |
if(!isMatch) Debug.LogError("File or folder " + path + " is not in a valid location in the Asset Bundle path!"); | |
return isMatch; | |
} | |
private static string GetParentAssetBundleName(string path) { | |
var regex = new Regex("(.*)/"); | |
var isFolder = !Path.HasExtension(path); | |
path = isFolder ? path : Path.GetDirectoryName(path); | |
var assetImporter = AssetImporter.GetAtPath(path); | |
if (string.IsNullOrEmpty(assetImporter.assetBundleName)) | |
{ | |
if (!regex.IsMatch(path)) | |
{ | |
Debug.LogError("There was no valid parent asset bundle set for some asset recently added!"); | |
return null; | |
} | |
path = GetParentAssetBundleName(regex.Split(path)[0]); | |
} | |
return assetImporter.assetBundleName; | |
} | |
private static void SetParentFolderBundleNames(string path) | |
{ | |
var bundleName = GetAssetBundleName(path); | |
var folder2 = new Regex("(" + BundlesLocation + "/.*?/.*?)/"); | |
if (folder2.IsMatch(path)) | |
{ | |
var assetImporter = AssetImporter.GetAtPath(folder2.Split(path)[1]); | |
assetImporter.SetAssetBundleNameAndVariant(bundleName, ""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment