New Tools/ambientCG Importer window (catalog browser, ZIP download with selective map extraction, URP/Lit material build). Generic pieces moved from the PolyHaven tool into Editor/WebAssets (EditorWebRequest, PbrMapType, PbrMaterialBuilder, ImportUtil, AssetBrowserGrid); PolyHaven API/window now delegate to the shared code, behavior unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
using System.IO;
|
|
using UnityEditor;
|
|
|
|
namespace Chameleon.Editor.WebAssets
|
|
{
|
|
/// <summary>Small shared helpers for the web-asset importer windows.</summary>
|
|
public static class ImportUtil
|
|
{
|
|
public static string SanitizeFileName(string name)
|
|
{
|
|
foreach (var c in Path.GetInvalidFileNameChars())
|
|
name = name.Replace(c, ' ');
|
|
return name.Trim();
|
|
}
|
|
|
|
/// <summary>"2k"/"2K" -> 2. Unknown strings sort last.</summary>
|
|
public static int ResolutionRank(string res)
|
|
{
|
|
if (string.IsNullOrEmpty(res)) return int.MaxValue;
|
|
var digits = res.TrimEnd('k', 'K');
|
|
return int.TryParse(digits, out var n) ? n : int.MaxValue;
|
|
}
|
|
|
|
/// <summary>Maps a "2k"-style resolution to a valid Unity max texture size.</summary>
|
|
public static int ResolutionToMaxSize(string res)
|
|
{
|
|
int k = ResolutionRank(res);
|
|
int px = k * 1024;
|
|
int[] valid = { 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 };
|
|
foreach (var v in valid)
|
|
if (px <= v) return v;
|
|
return 16384;
|
|
}
|
|
|
|
public static void EnsureFolder(string folder)
|
|
{
|
|
if (AssetDatabase.IsValidFolder(folder))
|
|
return;
|
|
var parts = folder.Split('/');
|
|
string cur = parts[0]; // "Assets"
|
|
for (int i = 1; i < parts.Length; i++)
|
|
{
|
|
string next = $"{cur}/{parts[i]}";
|
|
if (!AssetDatabase.IsValidFolder(next))
|
|
AssetDatabase.CreateFolder(cur, parts[i]);
|
|
cur = next;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Non-destructive folder-collision handling: if the folder exists, asks the user
|
|
/// (New folder / Cancel / Overwrite). Returns the folder to use, or null on cancel.
|
|
/// </summary>
|
|
public static string ResolveFolderCollision(string folder, string dialogTitle)
|
|
{
|
|
if (!AssetDatabase.IsValidFolder(folder))
|
|
return folder;
|
|
|
|
int choice = EditorUtility.DisplayDialogComplex(dialogTitle,
|
|
$"Folder already exists:\n{folder}", "New folder", "Cancel", "Overwrite");
|
|
if (choice == 1) return null; // Cancel
|
|
if (choice == 0) return AssetDatabase.GenerateUniqueAssetPath(folder); // New folder
|
|
return folder; // Overwrite
|
|
}
|
|
}
|
|
}
|