0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unity 時間のかかるテクスチャの読み込みテスト用

0
Posted at

10秒待ち

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);
    }


}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?