方法一:
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