VR平台开发笔记(三)游戏广告的插入与更新

来源:互联网 发布:mac缺少flash插件 编辑:程序博客网 时间:2024/04/29 05:02

之前的版本中已经做过视频播放的功能,但只是本机视频的播放。随着需求的变化,需要在网上获取游戏视频进行更新。
设计思路:
第一步,写xml,将最初的视频加入到播放序列中,并在新的视频未下载之前播放。
第二步,运用WWW类(结合文件流转换方法)在相应的网址下载视频”http://www.tulingvr.cn/sshTest/ver/GG/Trailer.ogv“,并将最后一位“/”和“.”作为分隔符,截取出文件的名字;
第三步,删除xml表中之前的文件名,同时删除文件。将新的文件名存储到xml表中,下次开启的时候读取xml表;
需要用到的知识点:
1,视频文件的播放(plane或者raw image)
2,WWW类文件下载与存储
3,Xml表的增删改查
4,本地文件的删减。

最后使用到了的类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Linq;using System.Collections;using System.Xml;using UnityEngine;using System.Collections;/// <summary>/// 游戏广告/// ADid:广告版本(游戏运行记录对比) ADname:广告名字(加载的依据)/// </summary>public class AD{    public int ADid;    public string ADname;}public class ADXmlManage : MonoBehaviour {    //创建广告XML的存储路径    public static string myXmlPath = Application.dataPath + "/TheAD/advertisement.xml";    //创建一个读取AD的类    public static AD ad = new AD();    // Use this for initialization    void Start () {        //实例化ad        ad.ADid = 1;        ad.ADname = "Trailer.ogv";        //在首次执行的时候创建文件        //GenerateXmlFile(myXmlPath);        //当版本发生变化的时候,将变化的AD信息加入到xml表中        //AddXmlNodeInformation(myXmlPath);        //查询ADxml中最后一位的名字,作为WWW加载的对象        //GetXmlNodeInformation(myXmlPath);    }    //添加一个xml文件(在首次执行的时候引用)    private static void GenerateXmlFile(string xmlPath)    {        try        {            //定义一个XDocument结构            XDocument myXDoc = new XDocument(               new XElement("AD",                   new XElement("AD", new XAttribute("ADid", ad.ADid ),                       new XElement("ADname",ad.ADname )                       )                  )           );            //保存此结构(即:我们预期的xml文件)            myXDoc.Save(xmlPath);        }        catch (Exception ex)        {            Console.WriteLine(ex.ToString());        }    }    //在xml表中新加节点(在版本对比不一样的时候调用)    public static void AddXmlNodeInformation(string xmlPath)    {        try        {            ad.ADid++;            //定义并从xml文件中加载节点(根节点)            XElement rootNode = XElement.Load(xmlPath);            //定义一个新节点            XElement newNode = new XElement("AD", new XAttribute("ADid", ad.ADid ),                                                        new XElement("ADname",MoviesManage .willMovie));            //将此新节点添加到根节点下            rootNode.Add(newNode);            //保存对xml的更改操作            rootNode.Save(xmlPath);        }        catch (Exception ex)        {            Console.WriteLine(ex.ToString());        }    }    //查找xml表中的节点    public static void GetXmlNodeInformation(string xmlPath)    {        try        {            XmlDocument rootnode = new XmlDocument();            rootnode.Load(xmlPath);            XmlNode node = rootnode.DocumentElement;            XmlNodeList nodes = node.ChildNodes;            ////获取属性ADid            //for (int i = 0; i < nodes.Count; i++)            //{            //    XmlElement ele = (XmlElement)nodes[i];            //    string a = ele.GetAttribute("ADid");            //    Debug.Log(a);            //}            //获取所有的名字            //for (int i = 0; i < nodes.Count; i++)            //{            //    XmlElement ele = (XmlElement)nodes[i];            //  string a=  ele.GetElementsByTagName("ADname")[0].InnerText;            //    Debug.Log(a+111);            //}            //直接获取最后一位的name,赋值给ad.ADname,作为将要加载的对象            ad.ADname = ((XmlElement)nodes[nodes.Count-1]).GetElementsByTagName("ADname")[0].InnerText;            DownloadLink.Mname = ad.ADname;            Debug.Log(DownloadLink.Mname);        }        catch (Exception ex)        {            Console.WriteLine(ex.ToString());        }    }    // Update is called once per frame    void Update () {    }}

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;

///
/// 下载需要用到的数据(由其它类提供)
/// 下载状态
///
public class DownloadLink
{
//下载地址
public static string link = “http://www.tulingvr.cn/sshTest/ver/GG/Trailer2.ogv“;
//false:可以下载 true:下载端口正在被占用
public static bool isDownload = false;

//需要加载的视频名称public static string Mname = "Trailer";

}

public class MoviesManage : MonoBehaviour {

//播放器背景public RawImage landMovie;//将要播放的视频(纹理)private MovieTexture movieTexture;//将要下载的视频名称(截取获取地址的名称)public static string willMovie = "Trailer.ogv";//保存的文件路径名称public static string filePath = Application.dataPath + "/TheAD/" + willMovie;/// <summary>/// 下载方法,在广告版本对比不一致的时候调用/// </summary>/// <returns></returns>IEnumerator LoadMovie(){    //占用端口    DownloadLink.isDownload = true;    //开始下载    WWW _www = new WWW(DownloadLink .link);    Debug.Log("loging" + Time.time);    yield return _www;    Debug.Log("stop" + Time.time);    //通过下载地址解析出将要下载的视频名称    int i = DownloadLink.link.LastIndexOf('/');    MoviesManage.willMovie = DownloadLink.link.Substring(i + 1, DownloadLink.link.Length - i - 1);    Debug.Log(MoviesManage.willMovie);    //将下载的名字添加到ADxml表中    ADXmlManage.AddXmlNodeInformation(ADXmlManage.myXmlPath);    Debug.Log("添加成功");    //下载完成,保存到路径filePath    byte[] bytes = _www.bytes;    filePath = Application.dataPath + "/TheAD/" + willMovie;    Bytes2File(bytes, filePath);    //释放端口    DownloadLink.isDownload = false;    Debug.Log("加载文件完成" + Time.time);}/// <summary>/// 将byte数组转换为文件并保存到指定地址/// </summary>/// <param name="buff">byte数组</param>/// <param name="savepath">保存地址</param>public static void Bytes2File(byte[] buff, string savepath){    if (System.IO.File.Exists(savepath))    {        System.IO.File.Delete(savepath);    }    FileStream fs = new FileStream(savepath, FileMode.CreateNew);    BinaryWriter bw = new BinaryWriter(fs);    bw.Write(buff, 0, buff.Length);    bw.Close();    fs.Close();}// Use this for initializationvoid Start () {    StartCoroutine("PlayMovie");    //StartCoroutine("LoadMovie");}// Update is called once per framevoid Update () {}//视频播放管理(循环播放加载)IEnumerator PlayMovie(){    //查询xml中最新的视频信息    ADXmlManage.GetXmlNodeInformation(ADXmlManage.myXmlPath);    //获取视频的名称    willMovie = DownloadLink.Mname ;    filePath = Application.dataPath + "/TheAD/" + willMovie;    Debug.Log(DownloadLink.Mname);    Debug.Log(filePath);    //将WWW加载路径赋给电影纹理    WWW www = new WWW("file://"+filePath);    yield return www;    if (www.isDone)    {        Debug.Log(filePath );        movieTexture = www.movie;        //将纹理播放属性设置为循环        movieTexture.loop = true;        //将纹理赋给landMovie        landMovie.texture = movieTexture;        Debug.Log(landMovie.texture == movieTexture);        //播放视频        movieTexture.Play();        //释放资源        www.Dispose();    }    Resources.UnloadUnusedAssets();}

}

这是自己写的小model:http://download.csdn.net/detail/tel18839970659/9721677

0 0
原创粉丝点击