using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ma
{
public static Material ToMat(this Texture2D tex, string shadername = "Transparent/Diffuse")
{
Material mat = new Material(Shader.Find(shadername));
mat.SetTexture("_MainTex", tex);
return mat;
}
}
public class test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
var uri = "https://gnjo.github.io/mock3d/mons/ajin1-min.png";
var heavy =10.0f;//
GameObject h = GameObject.CreatePrimitive(PrimitiveType.Quad);
h.name = "heavy";
Texture2D tex_h = new Texture2D(1, 1);
h.GetComponent<Renderer>().material =tex_h.ToMat();
h.transform.localPosition += new Vector3(1, 1, 1);
StartCoroutine(
byweb(uri, bytes => { tex_h.LoadImage(bytes); tex_h.Apply(); },heavy)
);
}
IEnumerator byweb(string uri, System.Action<byte[]> caller,float waitsec=0)
{
//using UnityEngine.Networking;
var www = UnityEngine.Networking.UnityWebRequest.Get(uri);
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError) Debug.Log(www.error);
//
if(waitsec!=0) yield return new WaitForSeconds(waitsec);
//
caller(www.downloadHandler.data);
}
}