Quantcast
Channel: Questions in topic: "webrequest"
Viewing all articles
Browse latest Browse all 387

UnityWebRequest in ScriptedImporter

$
0
0
I am writing an network asset importer to allow me to keep large assets out of my git repo, deduplicate assets, etc. I was wondering if it was possible to write a asynchronous file importer or even if not is it possible to run unity a UnityWebRequest synchronously. I can't start a coroutine in this content, and while I can start a task, attempting to wait for it to finish results in it never finishing, and it seems I can't write to context outside of `public override void OnImportAsset(AssetImportContext ctx)` (causes a crash) Any thoughts on if this is possible to do somehow. Thread.sleep causes hang, as does just looping. using UnityEngine; using UnityEditor.Experimental.AssetImporters; using System.IO; using UnityEngine.Networking; using System; using System.Threading; using System.Collections; using System.Threading.Tasks; [ScriptedImporter(1, "ipfs")] public class CubeImporter : ScriptedImporter { public float m_Scale = 1; public string uriStr; public Uri ipfsGateway = new Uri("http://localhost:8080"); public Texture2D debugTexture; private IEnumerator DoIt(AssetImportContext ctx, Uri uri) { using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(uri)) { var asyncOp = www.SendWebRequest(); yield return asyncOp; var texture = DownloadHandlerTexture.GetContent(www); ctx.AddObjectToAsset("main obj", texture); ctx.SetMainObject(texture); } } private async Task DoItAsync(AssetImportContext ctx, Uri uri) { using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(uri)) { var asyncOp = www.SendWebRequest(); while (asyncOp.isDone == false) { await Task.Delay(1000 / 30);//30 hertz } if (www.isNetworkError || www.isHttpError) { Debug.LogError($"{ www.error }, URL:{ www.url }"); return; } var texture = DownloadHandlerTexture.GetContent(www); Debug.Log($"{texture}, URL:{ www.url }"); debugTexture = texture; ctx.AddObjectToAsset("main obj", texture); ctx.SetMainObject(texture); } } public override void OnImportAsset(AssetImportContext ctx) { var ipfs = File.ReadAllText(ctx.assetPath); var uri = new Uri(ipfsGateway, ipfs); uriStr = uri.AbsoluteUri; var texture = new Texture2D(512, 512); var task = DoItAsync(ctx, uri); //task.Wait(); //ctx.AddObjectToAsset("main obj", texture); //ctx.SetMainObject(texture); //task.Wait(); // 'cube' is a a GameObject and will be automatically converted into a Prefab // (Only the 'Main Asset' is elligible to become a Prefab.) //var material = new Material(Shader.Find("Standard")); //material.color = Color.red; //// Assets must be assigned a unique identifier string consistent across imports //ctx.AddObjectToAsset("my Material", material); //// Assets that are not passed into the context as import outputs must be destroyed //var tempMesh = new Mesh(); //DestroyImmediate(tempMesh); } }

Viewing all articles
Browse latest Browse all 387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>