Skip to content

Instantly share code, notes, and snippets.

@tomkail
Created September 25, 2023 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkail/a15eac40b5f09430462f509ee9406df3 to your computer and use it in GitHub Desktop.
Save tomkail/a15eac40b5f09430462f509ee9406df3 to your computer and use it in GitHub Desktop.
Automatically applies Preset files to assets in the same directory. Also adds labels present on the Preset file to those assets.
using System.IO;
using UnityEditor;
using UnityEditor.Presets;
using System.Linq;
// This script automatically applies Presets to assets when they are imported into a folder containing a Preset.
// It also adds any labels on the Preset file to the asset.
public class PresetImportPerFolder : AssetPostprocessor {
void OnPreprocessAsset() {
// Make sure we are applying presets the first time an asset is imported.
if (assetImporter.importSettingsMissing) {
// Get the current imported asset folder.
var path = Path.GetDirectoryName(assetPath);
if(path == "ProjectSettings") return;
while (!string.IsNullOrEmpty(path)) {
// Find all Preset assets in this folder.
var presetGuids = AssetDatabase.FindAssets("t:Preset", new[] { path });
foreach (var presetGuid in presetGuids) {
// Make sure we are not testing Presets in a subfolder.
string presetPath = AssetDatabase.GUIDToAssetPath(presetGuid);
if (Path.GetDirectoryName(presetPath) == path) {
// Load the Preset and try to apply it to the importer.
var preset = AssetDatabase.LoadAssetAtPath<Preset>(presetPath);
if (preset.ApplyTo(assetImporter)) {
return;
}
}
}
// Try again in the parent folder.
path = Path.GetDirectoryName(path);
}
}
}
// Also add Labels on the preset
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
foreach (string assetPath in importedAssets) {
TryApplyPresetLabelsToAsset(assetPath);
}
}
static void TryApplyPresetLabelsToAsset(string assetPath) {
// Get the current imported asset folder.
var path = Path.GetDirectoryName(assetPath);
if(path == "ProjectSettings") return;
while (!string.IsNullOrEmpty(path)) {
// Find all Preset assets in this folder.
var presetGuids = AssetDatabase.FindAssets("t:Preset", new[] { path });
foreach (var presetGuid in presetGuids) {
// Make sure we are not testing Presets in a subfolder.
string presetPath = AssetDatabase.GUIDToAssetPath(presetGuid);
if (Path.GetDirectoryName(presetPath) == path) {
// Load the asset
var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
var existingLabels = AssetDatabase.GetLabels(asset);
// Load the Preset
var preset = AssetDatabase.LoadAssetAtPath<Preset>(presetPath);
var presetLabels = AssetDatabase.GetLabels(preset);
// Set labels
var labels = presetLabels.Concat(existingLabels).Distinct().ToArray();
AssetDatabase.SetLabels(asset, labels);
AssetDatabase.Refresh();
return;
}
}
// Try again in the parent folder.
path = Path.GetDirectoryName(path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment