Xml的方便之处:方便配置,也方便热更新,方便数据读取。
unity3d读取xml有好几种方式,最简单是直接利用System.Xml读取xml,但是项目打包会比较大,增加了1M的资源占用。另外两个是利用其他轻量级xml库来实现,如Mono.Xml、XMLParser。Mono.Xml是c#写的,XMLParser是js写的。文章主要说明Mono.Xml的用法。
为什么不建议使用System.Xml,unity的解释如下:https://docs.unity3d.com/355/Documentation/Manual/ReducingFilesize.html
When building a player (Desktop, Android or iOS) it is important to not depend on System.dll or System.Xml.dll. Unity does not include System.dll or System.Xml.dll in the players installation. That means, if you want to use Xml or some Generic containers which live in System.dll then the required dlls will be included in the players. This usually adds 1mb to the download size, obviously this is not very good for the distribution of your players and you should really avoid it. If you need to parse some Xml files, you can use a smaller xml library like this one Mono.Xml.zip. While most Generic containers are contained in mscorlib, Stack<> and few others are in System.dll. So you really want to avoid those.
总结是:为了减少包体大小。ios版本对System.Xml支持不是很好。
官方下载地址:https://docs.unity3d.com/355/Documentation/Images/manual/Mono.Xml.zip
csdn下载地址:http://download.youkuaiyun.com/detail/cwqcwk1/7105071
XmlVo=>baseVo=>GameConfig.PetMaxLevel。
项目中源码部分,https://github.com/zhutaorun/SomeTips/tree/master/Assets/Scripts/Tools
MiniParser,SmallXmlParser,MiniParser是Mono.Xml.zip主要内容
PathUtils.cs 获取StreamingAssets文件下的xml文件,同时实现xml热更新对模块管理。根据平台不同获取不同路径
using System;
using System.Collections.Generic;
using UnityEngine;
public class PathUtils
{
public static string GetStreamFilePath(string fileUrl)
{
string path = "";
if (fileUrl.ToLower().IndexOf("http://") == 0)
return fileUrl;
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
{
path = "file://"+Application.streamingAssetsPath+"/"+fileUrl;
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
path = "file://"+Application.streamingAssetsPath+"/"+fileUrl;
}
else if(Application.platform ==RuntimePlatform.Android)
{
path = "jar:file://"+Application.dataPath+"!/assets/"+fileUrl;
}
else
{
path = Application.dataPath + "/config/" + fileUrl;
}
return path;
}
public static string GetPersistebrFilePath(string filename)
{
string filepath;
if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXWebPlayer ||
Application.platform == RuntimePlatform.WindowsEditor ||
Application.platform == RuntimePlatform.WindowsPlayer)
filepath = "file://"+Application.dataPath+"/StreamingAssets/+filename";
else if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
filepath = Application.persistentDataPath + "/" + filename;
else
{
filepath = Application.persistentDataPath + "/" + filename;
}
#if UNITY_IPHONE
iphone.SetNoBackupFlag(filepath);
#endif
return filepath;
}
}
参考 http://blog.youkuaiyun.com/mycwq/article/details/19813779