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>
85 lines
3.2 KiB
C#
85 lines
3.2 KiB
C#
using System;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace Chameleon.Editor.WebAssets
|
|
{
|
|
/// <summary>
|
|
/// Non-blocking UnityWebRequest transport for editor tools. All requests poll on
|
|
/// EditorApplication.update — never blocks the editor. Shared by the Poly Haven
|
|
/// and ambientCG importers.
|
|
/// </summary>
|
|
public static class EditorWebRequest
|
|
{
|
|
public const string UserAgent = "Chameleon-Unity-AssetImporter/1.0";
|
|
|
|
/// <summary>GET a URL and return the response body as text.</summary>
|
|
public static void GetText(string url, Action<string> onDone, Action<string> onError)
|
|
{
|
|
var req = UnityWebRequest.Get(url);
|
|
req.SetRequestHeader("User-Agent", UserAgent);
|
|
Send(req, () => onDone?.Invoke(req.downloadHandler.text), onError);
|
|
}
|
|
|
|
/// <summary>Fetches an image as a Texture2D (browser thumbnails, previews).</summary>
|
|
public static void GetTexture(string url, Action<Texture2D> onDone, Action<string> onError)
|
|
{
|
|
var req = UnityWebRequestTexture.GetTexture(url);
|
|
req.SetRequestHeader("User-Agent", UserAgent);
|
|
Send(req, () =>
|
|
{
|
|
var tex = DownloadHandlerTexture.GetContent(req);
|
|
// Survive play-mode enter/exit cleanup — otherwise Unity destroys these
|
|
// loose runtime textures and browser previews go blank.
|
|
if (tex)
|
|
tex.hideFlags = HideFlags.HideAndDontSave;
|
|
onDone?.Invoke(tex);
|
|
}, onError);
|
|
}
|
|
|
|
/// <summary>Streams a file straight to disk (no RAM peak on big downloads).</summary>
|
|
public static void DownloadFile(string url, string destPath, Action onDone,
|
|
Action<string> onError, Action<float> onProgress = null)
|
|
{
|
|
var req = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
|
|
req.downloadHandler = new DownloadHandlerFile(destPath) { removeFileOnAbort = true };
|
|
req.SetRequestHeader("User-Agent", UserAgent);
|
|
Send(req, () => onDone?.Invoke(), onError, onProgress);
|
|
}
|
|
|
|
public static void Send(UnityWebRequest req, Action onSuccess, Action<string> onError,
|
|
Action<float> onProgress = null)
|
|
{
|
|
var op = req.SendWebRequest();
|
|
void Tick()
|
|
{
|
|
if (onProgress != null && !op.isDone)
|
|
onProgress(req.downloadProgress);
|
|
|
|
if (!op.isDone)
|
|
return;
|
|
|
|
EditorApplication.update -= Tick;
|
|
try
|
|
{
|
|
if (req.result != UnityWebRequest.Result.Success)
|
|
{
|
|
onError?.Invoke($"{req.error} ({req.url})");
|
|
}
|
|
else
|
|
{
|
|
onProgress?.Invoke(1f);
|
|
onSuccess?.Invoke();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
req.Dispose();
|
|
}
|
|
}
|
|
EditorApplication.update += Tick;
|
|
}
|
|
}
|
|
}
|