zoukankan      html  css  js  c++  java
  • unity UTF8格式加载和保存xml

    UTF8格式加载xml

    string xmlPath="D:/xxx.xml"
    FileLoader fileLoader=new FileLoader();
    fileLoader.loadAsync(xmlPath);
    fileLoader.onComplete-=onloadXmlComplete;
    
    private void onloadXmlComplete(byte[][] bytesList){
    	fileLoader.onComplete-=onloadXmlComplete;
    	byte[] bytes=bytesList[0];
    	if(bytes!=null){
    		string xmlString=System.Text.Encoding.UTF8.GetString(bytes);
    		xmlDocument=new XmlDocument();
    		xmlDocument.LoadXml(xmlString);
    	}
    }
    

    UTF8格式保存xml

    xmlDocument.Save("D:/xxx.xml");
    

    FileLoader.cs

    using System;
    using System.IO;
    using System.Threading.Tasks;
    using UnityEngine;
    
    /// <summary>
    /// 文件加载器
    /// </summary>
    public class FileLoader{
    	
    	/// <summary>
    	/// 文件加载完成事件
    	/// <br>void(byte[][] bytesList)</br>
    	/// <br>bytesList:表示加载完成后各个文件的总字节(索引与加载时传递的参数对应)</br>
    	/// </summary>
    	public event Action<byte[][]> onComplete;
    	
    	private bool _isDestroyed;
    	private FileStream _fileStream;
    	private bool _isLoading;
    	
    	/// <summary>
    	/// 异步加载一个或多个本地文件
    	/// <br>如果文件不存在将在onComplete(byte[][] bytesList)事件参数bytesList添加一个null</br>
    	/// </summary>
    	/// <param name="filePaths">可变长度文件路径列表,如: @"C:UsersAdministratorDesktopviews0.xml"</param>
    	public async void loadAsync(params string[] filePaths){
    		onLoadStart();
    
    		byte[][] outBytesList=new byte[filePaths.Length][];
    		for(int i=0;i<filePaths.Length;i++){
    			byte[] buffer=null;
    			string filePath=filePaths[i];
    			await Task.Run(()=>{
    				if(File.Exists(filePath)){
    					_fileStream=File.OpenRead(filePath);
    
    					int fileLength=(int)_fileStream.Length;
    					buffer=new byte[fileLength];
    
    					_fileStream.Read(buffer,0,fileLength);
    				}
    			});
    			if(_isDestroyed){
    				//加载过程中,删除该脚本绑定的对象时,打断
    				break;
    			}
    			outBytesList[i]=buffer;
    			dispose();
    		}
    
    		//所有加载完成
    		if(!_isDestroyed){
    			onLoadCompleteAll(outBytesList);
    		}
    	}
    
    	private void onLoadStart(){
    		_isLoading=true;
    	}
    
    	private void onLoadCompleteAll(byte[][] outBytesList){
    		_isLoading=false;
    		onComplete?.Invoke(outBytesList);
    	}
    
    	private void dispose(){
    		if(_fileStream!=null){
    			_fileStream.Dispose();
    			_fileStream.Close();
    			_fileStream=null;
    		}
    	}
    	
    	public void destroy(){
    		if(_isDestroyed)return;
    		_isDestroyed=true;
    		
    		dispose();
    	}
    	
    	public bool isLoading{ get => _isLoading; }
    
    }
    
  • 相关阅读:
    Leetcode 191.位1的个数 By Python
    反向传播的推导
    Leetcode 268.缺失数字 By Python
    Leetcode 326.3的幂 By Python
    Leetcode 28.实现strStr() By Python
    Leetcode 7.反转整数 By Python
    Leetcode 125.验证回文串 By Python
    Leetcode 1.两数之和 By Python
    Hdoj 1008.Elevator 题解
    TZOJ 车辆拥挤相互往里走
  • 原文地址:https://www.cnblogs.com/kingBook/p/11600182.html
Copyright © 2011-2022 走看看