using System.IO; using UnityEditor; namespace Chameleon.Editor.WebAssets { /// Small shared helpers for the web-asset importer windows. public static class ImportUtil { public static string SanitizeFileName(string name) { foreach (var c in Path.GetInvalidFileNameChars()) name = name.Replace(c, ' '); return name.Trim(); } /// "2k"/"2K" -> 2. Unknown strings sort last. 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; } /// Maps a "2k"-style resolution to a valid Unity max texture size. 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; } } /// /// 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. /// 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 } } }