- Unity中所有的文本类型的资源对应的类型为TextAsset,所以除了使用Resources.Load加载以外还可以拖拽赋值
- 一楼使用url加载的方式读取Resources文件夹下的xml,这样的方式并不推荐使用
这样的方式仅在编辑器模式下可以,发布后Resources文件所有的资源都会被压缩,这时候用Url加载一定找不到文件
即使要使用Url加载也是把Xml文件放在StreamingAssets文件夹下
- doc.LoadXml 里面的参数是文件路径,不是文件内容
- 从你的截图来看 你的XML没有被Unity识别,因为Xml文件的图标并不是文本资源的图标,
所以表现的特征有 1. 这xml应该不能拖拽 2. Resources.Load ("Xml4") 返回值应该为空
你第二张的截图空引用异常 也印证了这点
你可以尝试重新加载Xml 来解决未被识别的问题,就是 右键点击xml文件 --》Reimport
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public class XmlDataUtility
{
string m_file;
Stream m_fs;
public BinaryReader br
{
protected set;
get;
}
public BinaryWriter bw
{
protected set;
get;
}
public XmlDataUtility()
{
}
public BinaryReader BeginReaderBinary(string strFile)
{
m_file = strFile;
string path = "";
#if UNITY_EDITOR
if (Application.isPlaying) {
path += Util.DataPath;
}
else {
string target = string.Empty;
if (Application.platform == RuntimePlatform.OSXEditor){
target = "Mac";
}
else {
target = "Windows32";
}
path = Application.dataPath + "/StreamingAssets/" + target + "/";
}
path += "table/";
#else
path += Util.DataPath;
path += "table/";
#endif
string strFilePath = path + strFile;
//Resources
byte[] bytes = WebBytes.GetBytes(strFilePath);
m_fs = new MemoryStream(bytes);
br = new BinaryReader(m_fs);
return br;
}
public XmlDocument BeginWriteBinary(string root, string strFile)
{
m_file = strFile;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(PathSetting.platXmlPath() + strFile + ".xml");
//生成的目录(目前只需要windoweditor)
string path = root + "table/";
string strFilePath = path + strFile + ".bin";
if (File.Exists(strFilePath))
{
File.Delete(strFilePath);
}
m_fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Write);
bw = new BinaryWriter(m_fs);
return xmlDoc;
}
////TODO:测试中
//public XmlDocument BeginMSOWriteBinary(string root, string strFile, string sheetName, ref XmlNodeList rows)
//{
// XmlDocument xmlDoc = BeginWriteBinary(root, strFile);
// GetMSORows( xmlDoc, string sheetName)
// XmlNodeList workSheet = xmlDoc.GetElementsByTagName("Worksheet");
// if (workSheet.Count == 0)
// {
// return xmlDoc;
// }
// XmlNodeList tables = null;
// foreach (XmlElement sheet in workSheet)
// {
// string name = sheet.GetAttribute("ss:Name");
// if (name == sheetName)//"common"
// {
// tables = sheet.GetElementsByTagName("Table");
// }
// }
// if (tables == null || tables.Count <= 0)
// {
// return xmlDoc;
// }
// foreach (XmlElement row in tables)
// {
// rows = row.GetElementsByTagName("Row");
// if (rows.Count > 0)
// break;
// }
// return xmlDoc;
//}
public static XmlNodeList GetMSORows(XmlDocument xmlDoc, string sheetName)
{
XmlNodeList workSheet = xmlDoc.GetElementsByTagName("Worksheet");
if (workSheet.Count == 0)
{
return null;
}
XmlNodeList tables = null;
foreach (XmlElement sheet in workSheet)
{
string name = sheet.GetAttribute("ss:Name");
if (name == sheetName)//"common"
{
tables = sheet.GetElementsByTagName("Table");
}
}
if (tables == null || tables.Count <= 0)
{
return null;
}
XmlNodeList rows = null;
foreach (XmlElement row in tables)
{
rows = row.GetElementsByTagName("Row");
if (rows.Count > 0)
break;
}
return rows;
}
public void End()
{
if (m_fs != null)
{
m_fs.Close();
}
if (br != null)
{
br.Close();
//Debug.Log("ReaderBinary: " + m_file);
}
if (bw != null)
{
bw.Close();
//Debug.Log("WriteBinary: " + m_file);
}
}
}
网友评论