广告切换展示

下载xml到本地-读取解析xml-读取xml里的地址将图片下载到本地-从本地读取调用图片。

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using System.Xml;
using System.Text;

public class ndAdroiTempload : MonoBehaviour {

ArrayList imageArr = new ArrayList();
public Text res;

public Text set;
public Text get;
public float timer;//广告展示时间
int selectedInsex = 0;
string DownPath = ""; //资源下载保存的路径
string localPath = ""; //读取资源的路径
public GameObject rawImage;//图片展示的GameObject
string filepath = null;//图片存放的路径

 


void Awake()
{
selectedInsex = PlayerPrefs.GetInt("selectedImage");//获得上次存档的图片
get.text = "get———" + selectedInsex;
}


void Start ()
{
//将所有图片放入列表中
imageArr.Add("Image1");
imageArr.Add("Image2");
imageArr.Add("Image3");


StartCoroutine(DownLoadToLocal("http://192.168.0.98/MyItems/TankCard/AndroidLoad.xml")); // 从服务器上下载XML到本地
StartCoroutine(ReadInAndroid()); //读取xml
UpdateImageChange();

}

void UpdateImageChange()
{

if (selectedInsex < imageArr.Count)
{
selectedInsex++;
selectedInsex %= imageArr.Count;

}

else
{
selectedInsex--;
if (selectedInsex == -1)
{
selectedInsex = imageArr.Count - 1;

}


}

}
void Update()
{
timer += Time.deltaTime;//广告播放时间设定
//if (timer > 3)
//{
// rawImage.SetActive(false);
//}
set.text = "set———" + selectedInsex;
PlayerPrefs.SetInt("selectedImage", selectedInsex);
}


//从服务器图片下载资源
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);
}
}

//解析Xml
//安卓解析XML
//用于读取文件的协程函数
IEnumerator ReadInAndroid()
{
localPath = Application.streamingAssetsPath + "/AndroidLoad.xml";
WWW www = new WWW(localPath);
while (!www.isDone)
{
Debug.Log("Getting GetXML");
yield return www;
ParseXml(www);
}
}


//解析
public void ParseXml(WWW www)
{
List<XmlNode> xmlNodeList = new List<XmlNode>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(www.text);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Image").ChildNodes;


//遍历每一个节点,拿节点的属性以及节点的内容
foreach (XmlElement xe in nodeList)
{
foreach (XmlElement x1 in xe.ChildNodes)
{
string newstr = x1.InnerText; //分割为字符串数组(Split)
string[] arr = newstr.Split('|');


if (File.Exists(Application.persistentDataPath + "/" + x1.Name + ".jpg"))
{
//可以使用switch----case制作
//if (selectedInsex == 1)
//{
// rawImage.GetComponent<RawImage>().texture = LoadIMG("Image1" + ".jpg");
//}
//if (selectedInsex == 2)
//{
// rawImage.GetComponent<RawImage>().texture = LoadIMG("Image2" + ".jpg");
//}
//if (selectedInsex == 3)
//{
// rawImage.GetComponent<RawImage>().texture = LoadIMG("Image3" + ".jpg");
//}
//加载存储选择了的图片
rawImage.GetComponent<RawImage>().texture = LoadIMG(imageArr[selectedInsex] + ".jpg");
res.text = "imageArr[selectedInsex] ++++" + imageArr[selectedInsex];


}

else
{
StartCoroutine(DownLoadToLocalIMG(x1.Name,arr[1] ));//从服务器上下载图片
}
}
}
}

//从服务器下载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;
}
void OnGUI()
{
if (GUI.Button(new Rect(430, 0, 100, 100), "跳过"))
{
rawImage.SetActive(false);
}
}

}

转载于:https://www.cnblogs.com/ZeroMurder/p/5616265.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值