Android IOS ##StreamingAssets路径##XML操纵







我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦,其实有个简单的方法,在项目工程中新建一个StreamingAssets文件夹,把你的XML和TXT文件放到这里。

注:其实每个平台的路径都可以是Application.streamingAssetsPath+"/Achievement.xml"。但是android平台必须要用WWW加载,其他的平台貌似也可以的



 

unity StreamingAssets路径 .


[html]  view plain copy
  1. using UnityEngine;   
  2. using System.Collections;   
  3. using System.Xml;   
  4. using System.Xml.Serialization;   
  5. using System.IO;   
  6. using System.Text;   
  7.    
  8. public class Reward    
  9.  {   
  10.    public int taskNo;  
  11.      
  12.    public Task[] task = new Task[15];  
  13.    public Attribute attribute;   
  14.    public Reward () {}   
  15.    public struct Task  
  16.    {   
  17.       [XmlAttribute("taskReward")]   
  18.       public string taskReward{ get; set;}   
  19.       public Id id1;   
  20.       public Id id2;  
  21.       public Id id3;  
  22.    }  
  23.    public struct Id  
  24.    {  
  25.       [XmlAttribute("flag")]   
  26.       public bool flag{ get; set;}   
  27.       [XmlAttribute("name")]   
  28.       public string name{ get; set;}  
  29.       [XmlText()]  
  30.       public string description{get;set;}  
  31.           
  32.    }    
  33. }  
  34.   
  35. public class AchievementManager: MonoBehaviour {   
  36.    Reward reward ;   
  37.    FileInfo fileInfo;  
  38.    string _data;   
  39.       
  40.    void Start ()   
  41.    {     
  42.       reward = new Reward();  
  43.       LoadXML();  
  44.    }   
  45.    void LoadXML()   
  46.    {   
  47.       if(Application.platform == RuntimePlatform.IPhonePlayer)  
  48.       {  
  49.          fileInfo = new FileInfo(Application.dataPath + "/Raw/" + "Achievement.xml");   
  50.           StreamReader r = fileInfo.OpenText();   
  51.          _data = r.ReadToEnd();   
  52.          r.Close();   
  53.       }  
  54.       else if(Application.platform == RuntimePlatform.Android)  
  55.       {  
  56.          fileInfo = new FileInfo(Application.streamingAssetsPath+"/Achievement.xml");  
  57.          StartCoroutine("LoadWWW");  
  58.       }  
  59.       else  
  60.       {  
  61.          fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets/"+ "Achievement.xml");   
  62.          StreamReader r = fileInfo.OpenText();   
  63.          _data = r.ReadToEnd();   
  64.          r.Close();   
  65.       }     
  66.       if(_data.ToString() != "")   
  67.       {   
  68.          reward = (Reward)DeserializeObject(_data);                
  69.       }   
  70.    }  
  71.    void OnGUI()  
  72.    {  
  73.        GUI.Label(new Rect(0,0,Screen.width,Screen.height),"data:"+_data);      
  74.        if(Input.GetKey(KeyCode.Space))  
  75.         {  
  76.             Application.Quit();   
  77.         }  
  78.    }  
  79.       
  80.     IEnumerator LoadWWW()  
  81.     {  
  82.         WWW www = new WWW(Application.streamingAssetsPath+"/Achievement.xml");  
  83.         yield return www;  
  84.         _data =www.text;  
  85.     }  
  86.    public void Save()  
  87.    {       
  88.       _data = SerializeObject(reward);  
  89.       StreamWriter writer;   
  90.       fileInfo.Delete();      
  91.       writer = fileInfo.CreateText();   
  92.       writer.Write(_data);  
  93.       writer.Close();   
  94.    }  
  95.    string UTF8ByteArrayToString(byte[] characters)   
  96.    {        
  97.       UTF8Encoding encoding = new UTF8Encoding();   
  98.       string constructedString = encoding.GetString(characters);   
  99.       return (constructedString);   
  100.    }   
  101.    
  102.    byte[] StringToUTF8ByteArray(string pXmlString)   
  103.    {   
  104.       UTF8Encoding encoding = new UTF8Encoding();   
  105.       byte[] byteArray = encoding.GetBytes(pXmlString);   
  106.       return byteArray;   
  107.    }   
  108.    
  109.    // Here we serialize our Reward object of reward   
  110.    string SerializeObject(object pObject)   
  111.    {  
  112.       string XmlizedString = null;   
  113.       MemoryStream memoryStream = new MemoryStream();   
  114.       XmlSerializer xs = new XmlSerializer(typeof(Reward));   
  115.       XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
  116.       xs.Serialize(xmlTextWriter, pObject);   
  117.       memoryStream = (MemoryStream)xmlTextWriter.BaseStream;   
  118.       XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());   
  119.       return XmlizedString;   
  120.    }   
  121.    
  122.    // Here we deserialize it back into its original form   
  123.    object DeserializeObject(string pXmlizedString)   
  124.    {   
  125.       XmlSerializer xs = new XmlSerializer(typeof(Reward));   
  126.       MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));   
  127.       XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
  128.       return xs.Deserialize(memoryStream);   
  129.    }   
  130. }  







 

移动平台操控读取XML!

[html]  view plain copy
  1. using UnityEngine;  
  2. using System.Xml;  
  3. using System.Collections;  
  4. using System.IO;  
  5. public class XmlLoad   
  6. {  
  7.     /// <summary>  
  8.     /// XML 文档实例化  
  9.     /// </summary>  
  10.     private XmlDocument xmlDoc=new XmlDocument();  
  11.     private string data = "";  
  12.     private TextAsset asset;  
  13.     public int zombiesHp = 0;  
  14.     public float zombiesSpeed = 0f;  
  15.     /// <summary>  
  16.     /// 加载  
  17.     /// </summary>  
  18.     public void ShowData(string jsName)  
  19.     {  
  20.         asset = (TextAsset)Resources.Load("test", typeof(TextAsset));  
  21.         data = asset.text;  
  22.         xmlDoc.LoadXml(data);  
  23.         XmlElement root = xmlDoc.DocumentElement; // 得到根节点  
  24.         foreach (XmlNode node in root.ChildNodes)  
  25.         {  
  26.             XmlElement xe = (XmlElement)node;  
  27.             if (node.Name == jsName)  
  28.             {  
  29.                 zombiesHp =int.Parse(xe.GetAttribute("hp"));  
  30.                 zombiesSpeed = int.Parse(xe.GetAttribute("speed"));  
  31.             }  
  32.         }  
  33.     }  
  34. }  
  35. XML  
  36. <?xml version="1.0"?>  
  37. <root>  
  38.   <js01 hp="10" speed="50" damage="0.2">  
  39.   </js01>  
  40.   <js02 hp="15" speed="2" damage="0.2">  
  41.   </js02>  
  42.   <js03 hp="20" speed="3" damage="0.2">  
  43.   </js03>  
  44. </root>   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值