using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
using UnityEngine.UI;
public class jsonjx : MonoBehaviour
{
public RawImage rawImage;
ArrayList imgShow = new ArrayList();
int selectedIndex = 0;
string DownPath = "";
string localPath = "";
void Awake()
{
selectedIndex = PlayerPrefs.GetInt("selectedImage");
}
// Use this for initialization
void Start () {
imgShow.Add("Image1");
imgShow.Add("Image2");
imgShow.Add("Image3");
StartCoroutine(DownLoadToLocal("http://192.168.0.98/MyItems/TankCard/MyJson.json"));
StartCoroutine(downJson());
UpdateImage();
}
void UpdateImage()
{
if (selectedIndex < imgShow.Count)
{
selectedIndex++;
selectedIndex %= imgShow.Count;
}
else
{
selectedIndex--;
if (selectedIndex == -1)
{
selectedIndex = imgShow.Count - 1;
}
}
}
void Update()
{
PlayerPrefs.SetInt("selectedImage", selectedIndex);
}
//从服务器下载资源
private IEnumerator DownLoadToLocal(string url)
{
WWW.EscapeURL(url); //url编码
WWW www = new WWW(url);//访问url
WWW.UnEscapeURL(url); //url解码
string filename = url.Substring(url.LastIndexOf(‘/‘) + 1); //根据URL获取文件的名字。
yield return www; //等待下载
if (www.error == null)
{
FileStream fs = File.Create(Application.persistentDataPath + "/" + filename); //path为你想保存文件的路径。
DownPath = Application.persistentDataPath + "/" + filename; //把下载的数据写到文件
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
Debug.Log(filename + "下载完成");
}
else
{
Debug.Log(www.error);
}
}
IEnumerator downJson()
{
//string url = "http://192.168.0.116/index.php?m=App&c=Api&a=room&ajax=json";
string url = "http://192.168.0.98/MyItems/TankCard/MyJson.json";
WWW www = new WWW(url);
yield return www;
Debug.Log(www.text);
JsonJx(www.text);
}
//解析json
void JsonJx(string str)
{
JsonData jd = JsonMapper.ToObject(str);
for (int i = 0; i < jd.Count; i++)
{
string name = jd[i]["name"].ToString();
Debug.Log("jd[i][name]+++" + name);
string url=jd[i]["url"].ToString();
Debug.Log("jd[i][url]+++" + url);
if (File.Exists(Application.persistentDataPath + "/" + name + ".jpg"))
{
rawImage.texture = LoadIMG(imgShow[selectedIndex] + ".jpg");
// string id = jd[i]["sid"].ToString();
//Debug.Log("jd[i][sid]++++" + id);
}
else
{
StartCoroutine(DownLoadToLocalIMG(name, url));
}
}
}
//从服务器下载IMG资源
private IEnumerator DownLoadToLocalIMG(string strname,string url )
{
//url编码
WWW.EscapeURL(url);
//访问url
WWW www = new WWW(url);
//url解码
WWW.UnEscapeURL(url);
//根据URL获取文件的名字。
string filename = url.Substring(url.LastIndexOf(‘/‘) + 1);
//等待下载
yield return www;
//Instantiate(www.assetBundle.mainAsset);
if (www.error == null)
{
Debug.Log(www);
//path为你想保存文件的路径。
FileStream fs = File.Create(Application.persistentDataPath + "/" + filename);
DownPath = Application.persistentDataPath + "/" + filename;
//把下载的数据写到文件
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
Debug.Log(filename + "下载完成");
//加载img
//rawImage.GetComponent<RawImage>().texture = LoadIMG(strname + ".jpg");
}
else
{
Debug.Log(www.error);
}
}
//从磁盘上读取文件内容 Texture2D
Texture2D LoadIMG(string ImgName)
{
//实例化一个文件流
FileStream fs = File.Open(Application.persistentDataPath + "/" + ImgName, FileMode.Open);
//把文件读取到字节数组
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
//实例化一个内存流--->把从文件流中读取的内容[字节数组]放到内存流中去
MemoryStream ms = new MemoryStream(data);
//创建Texture
int width = 85;
int height = 85;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(data);
return texture;
}
}
json文档
[
{"name":"Image1","url":"http://192.168.0.98/MyItems/TankCard/Image1.jpg"},
{"name":"Image2","url":"http://192.168.0.98/MyItems/TankCard/Image2.jpg"},
{"name":"Image3","url":"http://192.168.0.98/MyItems/TankCard/Image3.jpg"}
]