55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.IO;
|
|
|
|
public class BlockDataIconLinker : AssetPostprocessor
|
|
{
|
|
// Fires after any assets are imported
|
|
static void OnPostprocessAllAssets(
|
|
string[] importedAssets,
|
|
string[] deletedAssets,
|
|
string[] movedAssets,
|
|
string[] movedFromAssetPaths)
|
|
{
|
|
foreach (string assetPath in importedAssets)
|
|
{
|
|
// Only care about Texture2D PNGs
|
|
if (!assetPath.EndsWith(".png", System.StringComparison.OrdinalIgnoreCase))
|
|
continue;
|
|
|
|
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
|
|
if (texture == null) continue;
|
|
|
|
// Try to find a matching BlockData for this texture
|
|
TryLinkToBlockData(texture, assetPath);
|
|
}
|
|
}
|
|
|
|
private static void TryLinkToBlockData(Texture2D texture, string texturePath)
|
|
{
|
|
string textureName = Path.GetFileNameWithoutExtension(texturePath);
|
|
|
|
// Find all BlockData assets in the project
|
|
string[] guids = AssetDatabase.FindAssets("t:BlockData");
|
|
foreach (string guid in guids)
|
|
{
|
|
string blockDataPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
BlockData blockData = AssetDatabase.LoadAssetAtPath<BlockData>(blockDataPath);
|
|
|
|
if (blockData == null || blockData.blockPrefab == null) continue;
|
|
|
|
// Match by checking if the texture name contains the prefab name
|
|
// Handles prefixes/suffixes like "Icon_zPin", "Icon_zPin_v2", etc.
|
|
if (!textureName.Contains(blockData.blockPrefab.name)) continue;
|
|
|
|
// Skip if already assigned to avoid unnecessary reimports
|
|
if (blockData.icon == texture || blockData.icon != null) continue;
|
|
|
|
blockData.icon = texture;
|
|
EditorUtility.SetDirty(blockData);
|
|
AssetDatabase.SaveAssetIfDirty(blockData);
|
|
|
|
Debug.Log($"[BlockDataIconLinker] Linked '{texturePath}' → '{blockDataPath}'");
|
|
}
|
|
}
|
|
} |