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
- VR平台开发笔记(三)游戏广告的插入与更新
- VR平台开发笔记(五).net的域名解析
- VR平台开发笔记(四).net添加与删除文件夹
- VR游戏与传统游戏开发区别 和 Unite 2016关于VR/AR的消息
- 教你怎么开发VR游戏系列教程三:VR游戏开发中常见的一些概念
- VR平台开发笔记(二)关于提示的一些做法
- 游戏开发流程与思想学习笔记(持续更新)
- 走进VR开发世界(4)——走进VR游戏开发的世界
- 我的游戏开发笔记(三):扔东西与跟踪。
- Unity2016 Unity3D开发VR游戏的经验
- 走进VR游戏开发的世界
- 走进VR游戏开发的世界
- 走进VR游戏开发的世界
- VR游戏交互开发的一些体验
- 走进VR游戏开发的世界
- 走进VR游戏开发的世界
- 走进VR游戏开发的世界
- 《MFC游戏开发》笔记三 游戏贴图与透明特效的实现
- bootstrap-datepicker日历控件的使用心得
- 【android开发】滑动按钮 SlipSwitch开关的实现
- C语言中.c与.h文件解析
- Android 过度渲染及优化方法--3D效果(JakeWharton大神的scalpel)
- 从Set里面取出有序的记录
- VR平台开发笔记(三)游戏广告的插入与更新
- warning: push.default is unset; its implicit value is changing in
- mongodb聚合利用日期分组。
- 关于在SpringMVC框架中 实现数据库session会话环境变量的功能
- Spring Boot
- 调用DropBox的情景
- javamail发件、收件(SSL连接)
- Android进阶系列之Support Annotation Library使用详解
- 减少GC,从Assetbundle缓存开始