Xml配置文件的保存以及读取

本文介绍了一种在Unity中使用XML文件管理资产依赖的方法。包括如何将资产信息保存到XML文件,以及如何从XML文件中读取资产及其依赖的捆绑包信息。此方法有助于大型项目中资产的管理和追踪。

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

这种格式的xml配置文件的读取和保存为例。

1,保存

private static string path = Application.dataPath.Replace('\\', '/');    
private static string projectPath = path.Substring(0, path.LastIndexOf('/'));
private static string xmlAssetDependencesBundle = outPrintPath + "/" + "AssetDependenceBundles.xml";

 public static void SaveAssetsToXml()
    {
        string xmlPath = xmlAssetDependencesBundle;
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlRoot = xmlDoc.CreateElement("AssetList");
        xmlDoc.AppendChild(xmlRoot);
        Dictionary<string, List<string>> orderDic = m_dicAssetDependenceBundle.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
        Dictionary<string, List<string>>.Enumerator it = orderDic.GetEnumerator();

        while (it.MoveNext())
        {
            XmlElement xmlAsset = xmlDoc.CreateElement("Asset");
            XmlAttribute name = xmlDoc.CreateAttribute("Name");
            name.Value = it.Current.Key.ToLower();
            xmlAsset.Attributes.Append(name);

            XmlAttribute count = xmlDoc.CreateAttribute("Count");
            count.Value = (it.Current.Value.Count).ToString();
            xmlAsset.Attributes.Append(count);

            foreach (string dtPath in it.Current.Value)
            {
                XmlElement xmlDtAsset = xmlDoc.CreateElement("DtBundle");
                XmlAttribute dtName = xmlDoc.CreateAttribute("Name");
                dtName.Value = dtPath.ToLower();
                xmlDtAsset.Attributes.Append(dtName);
                xmlAsset.AppendChild(xmlDtAsset);
            }
            xmlRoot.AppendChild(xmlAsset);
        }

        string xmlDir = xmlAssetDependencesBundle.Substring(0, xmlAssetDependencesBundle.LastIndexOf('/'));
        if (!Directory.Exists(xmlDir))
            Directory.CreateDirectory(xmlDir);

        string xmlSavePath = xmlPath + "/" + "AssetDependenceBundles.xml";
        xmlDoc.Save(xmlSavePath);
    }

2,读取

public static void ReadAssetIncludeBundleXml()
    {
        xmlAssetDependenceBundle = new Dictionary<string, List<string>>();

        XmlDocument xmlDoc = new XmlDocument();
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreComments = true;
        XmlReader xmlReader = XmlReader.Create(xmlAssetDependencesBundle, settings);
        xmlDoc.Load(xmlReader);

        //Dictionary<string, List<string>> assetDtBundles = new Dictionary<string, List<string>>();

        XmlNode rootNode = xmlDoc.SelectSingleNode("AssetList");
        XmlNodeList lstNode = rootNode.ChildNodes;
        foreach (XmlNode item in lstNode)
        {
            string assetPath = string.Empty;
            List<string> bundles = new List<string>();
            XmlElement asset = (XmlElement)item;
            assetPath = asset.Attributes["Name"].Value;
            XmlNodeList xmlbundles = asset.ChildNodes;
            foreach (XmlNode bundle in xmlbundles)
            {
                bundles.Add(bundle.Attributes["Name"].Value);
            }
            if (string.IsNullOrEmpty(assetPath) || bundles.Count < 0)
                Debug.LogError("资源有问题,资源:" + assetPath);
            xmlAssetDependenceBundle.Add(assetPath, bundles);
        }

        PrintAssetBundleInfoToText(xmlAssetDependenceBundle, readAssetDependenceBundlesXml);
        Debug.Log("读取xml成功");
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值