[Unity菜鸟] Unity读XML
1. 在Unity中调试可行,发布成exe可行,发布成web不行
Application.dataPath 在Unity中调试是在“..Assets”文件夹下, 发布成exe文件是在“..yourName_Data”文件夹下
using UnityEngine;
using System.Collections;
using System.Xml; public class CallXml
{
//XML文件路径 在Unity中调试
public static string localUrl = Application.dataPath + "/CallExe.xml"; public static XmlDocument ReadAndLoadXml()
{
XmlDocument doc = new XmlDocument();
//Debug.Log("加载xml文档");
doc.Load(localUrl);
return doc; } public static string GetPath(string tagName)
{
string _path = "";
XmlDocument xmlDoc = ReadAndLoadXml();
XmlNode provinces = xmlDoc.SelectSingleNode("xml"); //顶层节点 foreach (XmlNode province in provinces)
{
XmlElement _province = (XmlElement)province; if (tagName == _province.GetAttribute("name"))
{
//根据传入的tagName获取路径
_path = _province.GetAttribute("path");
}
}
return _path;
}
}
调用程序(unity中测试时,发布exe时调用)
Process proc = new Process();
// unity中和发布exe时调用
proc.StartInfo.FileName = CallXml.GetPath("one");
proc.Start();
//可以用下面代码直接调用程序
System.Diagnostics.Process.Start("notepad");
Process.Start(@"C:\Users\DELL\Desktop\123.txt"); //但当报如下错误提示时,就要用上述方式调用程序
error CS0120: An object reference is required to access non-static member `System.Diagnostics.Process.Start()'
2. 发布成web之后调用xml里的文件
using UnityEngine;
using System.Collections;
using System.Xml; public class CallXmlWeb : MonoBehaviour { private static WWW _statusFile; // Use this for initialization
IEnumerator Start ()
{
yield return StartCoroutine(ReadXML());
print(_statusFile.text);
} private IEnumerator ReadXML()
{
yield return _statusFile = new WWW(Application.dataPath + "/CallExe.xml");
} public static XmlDocument ReadAndLoadXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(_statusFile.text);
return xmlDoc;
} //根据节点名,读取节点中的值
public static string GetPath(string tagName)
{
string _path = "";
XmlDocument xmlDoc = ReadAndLoadXml();
XmlNode provinces = xmlDoc.SelectSingleNode("xml"); //顶层节点 foreach (XmlNode province in provinces)
{
XmlElement _province = (XmlElement)province; if (tagName == _province.GetAttribute("name"))
{
//根据传入的tagName获取路径
_path = _province.GetAttribute("path");
}
}
return _path;
}
}
调用程序(发布web时调用)
注意1: 此时用下面代码调用不会有结果,因为web的安全机制,不能随便调用本地程序。 传入路径/xx/xx.exe等也不行,网上有其它的解决方法。
proc.StartInfo.FileName = CallXmlWeb.GetPath("notepad");
但可以用GetPath("notepad");读取到XML文件里面的内容
注意2:从其他函数调用此函数时,读取不到XML里的内容,在一个函数内部测试就可以,暂不知道原因。
3. XML文件CallExe.xml
<?xml version="1.0" encoding="utf-8"?>
<xml>
<tag name ="notepad" path ="notepad"> </tag>
<tag name ="qq" path ="C:\Program Files (x86)\Tencent\QQ\QQProtect\Bin\QQProtect.exe"> </tag>
<tag name ="jisuanqi" path ="calc"> </tag>
</xml>
[Unity菜鸟] Unity读XML的更多相关文章
- [Unity菜鸟] Unity发布web后,从HTML调用本地文件
1. 遇到的问题 从xml读数据,xml的编码用中文就会乱码,改编码格式就调用不了.目前采用默认的ANSI编码. .exe .txt .xlsx .xml .test Run √ ...
- [Unity菜鸟] Unity Web Player 相关问题 (待完善)
1. 发布网页版Unity自适应网页大小 发布网页版,Unity3D自适应网页大小.这个问题困扰了我很长时间,今天终于把他解决了,给大家分享一下. 这里用Uinty4.0发布网页版,我去掉了里面的标题 ...
- [Unity菜鸟] Unity鼠标双击,鼠标函数整理(未完)
1. 鼠标双击 void OnGUI() { Event Mouse = Event.current; if (Mouse.isMouse && Mouse.type == Event ...
- [Unity][安卓]Unity和Android Studio 3.0 交互通讯(1)Android Studio 3.0 设置
[安卓]Android Studio 3.0 JDK安卓环境配置(2017.10) http://blog.csdn.net/bulademian/article/details/78387052 [ ...
- Unity又称Unity Application Block
本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): Ioc/DI简介: Unity简单示例 一.Ioc/DI简介 IoC 即 Inversion of C ...
- C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)【转载】
已知有一个XML文件(bookstore.xml)如下: Corets, Eva 5.95 1.插入节点 往节点中插入一个节点: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- python 应用xml.dom.minidom读xml
xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...
- Ubuntu 11.10 安装GMONE3,卸载 UNITY和UNITY 2D
Ubuntu 11.10安装GNOME3: 1)sudo apt-get install gnome-shell sudo apt-get install gnome-themes* (或者 ...
- C#操作Xml:使用XmlReader读Xml
XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...
随机推荐
- 本地安装discuz
出处:http://jingyan.baidu.com/article/b87fe19eb57ff252183568d9.html 网站建目前都很简单,建站容易,管理难,网站做大优化更难.本人有建站经 ...
- 1.ssh访问限制
1.要求:限制my133.t.org(172.168.1.0/24)这个攻击域的主机访. 2.操作:vim /etc/host.deny 忘记可^tab ,在最末尾添加行:sshd: 172.168 ...
- 一款js控制背景图片平铺
背景图片的平铺方法有很多种,纯色背景,渐变背景,图片背景,今天讲的是移动端的图片背景~~~~ <style> html,body{;;} .body{background: url(ima ...
- asp.net webform download excel
private void exportBinaryToExcel(byte[] bytes, string filename) { Response.AddHeader("Content-D ...
- Node.js 【使用npm安装一些包失败之笔记】
镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry https://regist ...
- eclipse 僵死/假死 问题排查及解决
症状: 使用Eclipse win 64位版本,indigo及kepler都重现了,使用tomcat 6.0.39,jdk1.6.u45及1.7u45均尝试了,也重现. 重现步骤很简单,使用debug ...
- EXTJS 4.2 资料 控件GroupingGrid
http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-17/179.html
- Unity3d之UGUI- Image拦截Button响应事件
在编辑UI的时候Image放在Button前面就会挡掉button的响应 这种情况只需要为Image添加CanvasGroup組件 按照这个属性配置就不会挡掉后面的事件了
- Tomcat 搭配 Nginx 还是 Apache 呢?
Apache .Tomcat.Nginx的区别, 哪个与Tomcat搭配效率高? 一. 定义: 1. Apache Apache HTTP服务器是一个模块化的服务器,可以运行在几乎所有广泛使用的计算机 ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...