源码:
public class test02 : MonoBehaviour {
private GameObject obj;
private Texture texture0;
private Texture texture1;
// Use this for initialization
void Start () {
obj = GameObject.Find ("Cube");
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
if (GUILayout.Button ("加载本地图片")) {
StartCoroutine (loadLocal());
}
if (GUILayout.Button ("加载服务器图片")) {
StartCoroutine (loadNetwork());
}
}
IEnumerator loadLocal() {
if (texture0 == null) {
//资源在本地的路径上
WWW date = new WWW ("file://" + Application.dataPath + "/muzico.jpg");
//等待下载完
yield return date;
//下载完,得到所下载的图像的贴图
texture0 = date.texture;
}
//更换为下载的贴图
Renderer render = obj.GetComponent<Renderer>();
render.material.mainTexture = texture0;
}
IEnumerator loadNetwork() {
if (texture1 == null) {
//资源在服务器的路径上
WWW date = new WWW ("http://app.08138.com/test/test.jpg");
//等待下载完
yield return date;
//下载完,得到所下载的图像的贴图
texture1 = date.texture;
}
Renderer render = obj.GetComponent<Renderer>();
//更换为下载的贴图
render.material.mainTexture = texture1;
}
}