Unity3D Tutorial: Streaming Assets Path

When we read and write XML and TXT files, for example, the paths on the computer and the mobile phone are inconsistent, which causes a lot of trouble. In fact, there is a simple method. Create a new StreamingAssets folder in the project and save your XML and TXT files. Put it here.

Note: In fact, the path of each platform can be Application.streamingAssetsPath+"/Achievement.xml". But the android platform must be loaded with WWW.

    using UnityEngine;   
    using System.Collections;   
    using System.Xml;   
    using System.Xml.Serialization;   
    using System.IO;   
    using System.Text;   
 
    public class Reward    
     {   
       public int taskNo;  
 
       public Task[] task = new Task[15];  
       public Attribute attribute;   
       public Reward () {}   
       public struct Task  
       {   
          [XmlAttribute("taskReward")]   
          public string taskReward{ get; set;}   
          public Id id1;   
          public Id id2;  
          public Id id3;  
       }  
       public struct Id  
       {  //Unity3D教程手册:www.unitymanual.com
          [XmlAttribute("flag")]   
          public bool flag{ get; set;}   
          [XmlAttribute("name")]   
          public string name{ get; set;}  
          [XmlText()]  
          public string description{get;set;}  
 
       }    
    }  
 
    public class AchievementManager: MonoBehaviour {   
       Reward reward ;   
       FileInfo fileInfo;  
       string _data;   
 
       void Start ()   
       {     
          reward = new Reward();  
          LoadXML();  
       }   
       void LoadXML()   
       {   
          if(Application.platform == RuntimePlatform.IPhonePlayer)  
          {  
             fileInfo = new FileInfo(Application.dataPath + "/Raw/" + "Achievement.xml");   
              StreamReader r = fileInfo.OpenText();   
             _data = r.ReadToEnd();   
             r.Close();   
          }  //Unity3D教程手册:www.unitymanual.com
          else if(Application.platform == RuntimePlatform.Android)  
          {  
             fileInfo = new FileInfo(Application.streamingAssetsPath+"/Achievement.xml");  
             StartCoroutine("LoadWWW");  
          }  
          else  
          {  
             fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets/"+ "Achievement.xml");   
             StreamReader r = fileInfo.OpenText();   
             _data = r.ReadToEnd();   
             r.Close();   
          }     
          if(_data.ToString() != "")   
          {   
             reward = (Reward)DeserializeObject(_data);                
          }   
       }  
       void OnGUI()  
       {  
           GUI.Label(new Rect(0,0,Screen.width,Screen.height),"data:"+_data);      
           if(Input.GetKey(KeyCode.Space))  
            {  
                Application.Quit();   
            }  
       }  
 
        IEnumerator LoadWWW()  
        {  
            WWW www = new WWW(Application.streamingAssetsPath+"/Achievement.xml");  
            yield return www;  
            _data =www.text;  
        }  
       public void Save()  
       {       
          _data = SerializeObject(reward);  
          StreamWriter writer;   
          fileInfo.Delete();      
          writer = fileInfo.CreateText();   
          writer.Write(_data);  
          writer.Close();   
       }  
       string UTF8ByteArrayToString(byte[] characters)   
       {        
          UTF8Encoding encoding = new UTF8Encoding();   
          string constructedString = encoding.GetString(characters);   
          return (constructedString);   
       }   
 
       byte[] StringToUTF8ByteArray(string pXmlString)   
       {   
          UTF8Encoding encoding = new UTF8Encoding();   
          byte[] byteArray = encoding.GetBytes(pXmlString);   
          return byteArray;   
       }   
 
       // Here we serialize our Reward object of reward   
       string SerializeObject(object pObject)   
       {  
          string XmlizedString = null;   
          MemoryStream memoryStream = new MemoryStream();   
          XmlSerializer xs = new XmlSerializer(typeof(Reward));   
          XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
          xs.Serialize(xmlTextWriter, pObject);   
          memoryStream = (MemoryStream)xmlTextWriter.BaseStream;   
          XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());   
          return XmlizedString;   
       }   
 
       // Here we deserialize it back into its original form   
       object DeserializeObject(string pXmlizedString)   
       {   
          XmlSerializer xs = new XmlSerializer(typeof(Reward));   
          MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));   
          XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
          return xs.Deserialize(memoryStream);   
       }   
    }  

Guess you like

Origin blog.csdn.net/weixin_55688630/article/details/128510875