Unity 图片下载

方法一:

    private IEnumerator Downloader()
    {
        if (enableLog)
            Debug.Log("[Davinci] Download started.");

        var www = new WWW(url);

        while (!www.isDone)
        {
            if (www.error != null)
            {
                error("Error while downloading the image : " + www.error);
                yield break;
            }

            progress = Mathf.FloorToInt(www.progress * 100);
            if (onDownloadProgressChange != null)
                onDownloadProgressChange.Invoke(progress);

            if (enableLog)
                Debug.Log("[Davinci] Downloading progress : " + progress + "%");

            yield return null;
        }

        if (www.error == null)
            File.WriteAllBytes(filePath + uniqueHash, www.bytes);

        www.Dispose();
        www = null;

        if (onDownloadedAction != null)
            onDownloadedAction.Invoke();

        loadSpriteToImage();

        underProcessDavincies.Remove(uniqueHash);
    }

    private void loadSpriteToImage()
    {
        progress = 100;
        if (onDownloadProgressChange != null)
            onDownloadProgressChange.Invoke(progress);

        if (enableLog)
            Debug.Log("[Davinci] Downloading progress : " + progress + "%");

        if (!File.Exists(filePath + uniqueHash))
        {
            error("Loading image file has been failed.");
            return;
        }

        StopAllCoroutines();
        StartCoroutine(ImageLoader());
    }

 


方法二:

 IEnumerator HandleWebLoad(string localPath, string targetURL, GameObject target)
        {
            WWW www = new WWW(targetURL);
            Debug.LogFormat("Downloading from web... targetURL={0}", targetURL);
            yield return www;
            Debug.LogFormat("Download completed. targetURL={0} size={1:N0} bytes", targetURL, www.bytesDownloaded);

            if (string.IsNullOrEmpty(www.text))
            {
                Debug.LogErrorFormat("Download failed err={0} targetURL={1}", www.error, targetURL);
                isLoading = false;
                yield break;
            }

            // NOTE: save image to disk
            Debug.LogFormat("Saving image to disk... localPath={0}", localPath);
            System.IO.File.WriteAllBytes(localPath, www.bytes);
            Debug.LogFormat("Image save completed. localPath={0}", localPath);

            SetupSpriteDisp(target, www);
            isLoading = false;
        }

 private void SetupSpriteDisp(GameObject target, WWW www)
        {
            RectTransform rt = target.GetComponent<RectTransform>();
            SpriteRenderer sr = target.GetComponent<SpriteRenderer>();
            Rect r = rt.rect;
            Sprite sprite = Sprite.Create(
                www.texture,
                new Rect(0, 0, www.texture.width, www.texture.height),
                Vector2.one / 2,
                www.texture.width,
                0,
                SpriteMeshType.FullRect,
                Vector4.zero
            );

            sr.sprite = sprite;
            sr.drawMode = SpriteDrawMode.Sliced;
            sr.size = new Vector2(r.width, r.height);

            target.transform.localScale = Vector3.one;
            target.transform.localPosition = Vector3.zero;
            target.transform.position = Vector3.zero;

            rt.position = Vector3.zero;
            rt.localPosition = Vector3.zero;
            rt.localScale = Vector3.one;

            // NOTE: awill: make SURE to let the parent transform know it needs
            // to refresh after the image is done
            LayoutRebuilder.MarkLayoutForRebuild(target.transform.parent.GetComponent<RectTransform>());
        }

方法三:

using UnityEngine;
using System.Collections;

// Set the url on this object to cause it to render a texture from a given location as the object's texture
public class ImageSource : MonoBehaviour {

	// The url to access for the image
	public string url;

	private string lastUrl;

	IEnumerator Start() {
		lastUrl = url;
		return LoadTexture (url);
	}

	void Update() {
		// If url has been changed, load it
		if (url != lastUrl)
			LoadTexture (url);
	}
	// Load the texture specified by the current url value, or the url provided
	public IEnumerator LoadTexture (string newUrl) {

		// Set the new url if it's different
		if (url != newUrl)
			url = newUrl;

		lastUrl = url;

		if (url == null)
			yield break;

		// Create a www class to fetch the object
		WWW www = new WWW(url);

		// Defer processing until the www value is returned
		yield return www;

		// Define the renderer object by getting that component
		Renderer renderer = GetComponent<Renderer>();
		if (renderer != null) {
			renderer.material.mainTexture = www.texture;
		} else {
		//	Debug.Log ("No render on ImageSourcetarget: " + gameObject.name.ToString());
		}
	}
}

 

FR:徐海涛(hunk Xu)
QQ技术交流群:386476712

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值