原地址:http://blog.csdn.net/nateyang/article/details/8493791

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

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System; public class Reward
{
public int taskNo; public Task[] task = new Task[];
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
{
[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();
}
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(,,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);
}
}

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

unity StreamingAssets路径的更多相关文章

  1. unity Android在streamingAssets路径下文件无法读取的的解决方法

    unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...

  2. Unity特殊路径

    Resources: Resources文件可以在根目录下,也可以在子目录下,只要叫Resources就好.Resources目录下所有资源将被打包进游戏存放资源的archive中,Resources ...

  3. Unity读取StreamingAssets路径下的文件

    /// <summary> ///读取StreamingAssets中的文件 /// </summary> /// <param name="path" ...

  4. Unity相关路径

    Application.dataPath 只读 在项目根目录下读取文件,但移动端没有访问权限.一般适用于PC端调试用. Application.streamingAssetsPath 在Assets目 ...

  5. unity组件路径自动生成

    unity 有时候找路径太麻烦 写了一个自动生成脚本的工具 using System.Collections.Generic; using System.IO; using System.Text; ...

  6. Unity存储路径

    一.在项目根目录中创建Resources文件夹来保存文件 可以使用Resources.Load("文件名字,注:不包括文件后缀名");把文件夹中的对象加载出来注:此方可实现对文件实 ...

  7. unity绝对路径与相对路径转化

    绝对路径->相对路径 string mp =“H:\unity(project)\New Unity Project\Assets\111.mat”; mp = mp.Substring(mp. ...

  8. unity文件路径

    转载自:https://blog.csdn.net/linxinfa/article/details/51679528 各平台具体路径: 1.Resources Resources文件夹是一个只读的文 ...

  9. Unity Android路径及注意事项

    Application.temporaryCachePath==/storage/emulated/0/Android/data/com.***.***/cache Application.persi ...

随机推荐

  1. [转]Oracle中的索引详解

    原文地址:http://www.oschina.net/question/30362_4057 一. ROWID的概念 存储了row在数据文件中的具体位置:64位 编码的数据,A-Z, a-z, 0- ...

  2. publish_subscribe

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  3. 修改Oracle权限的SQL及常见错误

    1.在cmd命令中进入sqlplus:相应的在DOS命令下执行:(1)set ORACLE_SID = $INSTANCE_NAME(2)sqlplus /nolog(3)connect user/p ...

  4. 35.Android之带删除按钮EditText学习

    今天实现Android里自定义带删除功能的EditText,效果如下: 当输入内容时,EditText变为带有一个删除功能按钮的编辑框,如图: 实现代码很简单,直接上代码, 布局文件xml: < ...

  5. Type-Length-Value编码

    Within data communication protocols, optional information may be encoded as a type-length-value or T ...

  6. kindeditor粘贴word文档内容时去除格式的方法?如何设置为默认无文本格式呢?

    打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可. 设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴.

  7. Android:Touch和Click的区别

    http://blog.csdn.net/hufeng882412/article/details/7310142 针对屏幕上的一个View控件,Android如何区分应当触发onTouchEvent ...

  8. 关于API的设计与实现

    http://blog.csdn.net/horkychen/article/details/46612899 API的设计是软件开发中一个独特的领域.最主要的特殊点在于API是供开发者使用的界面,即 ...

  9. PHP中PDO的配置与说明

    住[PDO是啥] PDO是PHP5新加入的一个重大功能,因为在PHP5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接和处理,什么php_mysql.dll.php_pgsql.dll ...

  10. OOA/OOD/OOP(了解)

    Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构化分析有较大的区别.OOA所强调的是在系统 ...