XML在Unity中使用

本文探讨了Unity3D中使用轻量级XML库Mono.Xml进行XML文件读取的方法,对比System.Xml,介绍了如何减少包体大小并实现跨平台XML热更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值