unity StreamingAssets路径
原地址: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路径的更多相关文章
- unity Android在streamingAssets路径下文件无法读取的的解决方法
unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...
- Unity特殊路径
Resources: Resources文件可以在根目录下,也可以在子目录下,只要叫Resources就好.Resources目录下所有资源将被打包进游戏存放资源的archive中,Resources ...
- Unity读取StreamingAssets路径下的文件
/// <summary> ///读取StreamingAssets中的文件 /// </summary> /// <param name="path" ...
- Unity相关路径
Application.dataPath 只读 在项目根目录下读取文件,但移动端没有访问权限.一般适用于PC端调试用. Application.streamingAssetsPath 在Assets目 ...
- unity组件路径自动生成
unity 有时候找路径太麻烦 写了一个自动生成脚本的工具 using System.Collections.Generic; using System.IO; using System.Text; ...
- Unity存储路径
一.在项目根目录中创建Resources文件夹来保存文件 可以使用Resources.Load("文件名字,注:不包括文件后缀名");把文件夹中的对象加载出来注:此方可实现对文件实 ...
- unity绝对路径与相对路径转化
绝对路径->相对路径 string mp =“H:\unity(project)\New Unity Project\Assets\111.mat”; mp = mp.Substring(mp. ...
- unity文件路径
转载自:https://blog.csdn.net/linxinfa/article/details/51679528 各平台具体路径: 1.Resources Resources文件夹是一个只读的文 ...
- Unity Android路径及注意事项
Application.temporaryCachePath==/storage/emulated/0/Android/data/com.***.***/cache Application.persi ...
随机推荐
- 自己写了个H5版本的俄罗斯方块
在实习公司做完项目后,实在无聊.就用H5写了几个游戏出来玩一下.从简单的做起,就搞了个经典的俄罗斯方块游戏. 先上效果: 上面的数字是得分,游戏没有考虑兼容性,只在chrome上测试过,不过大部分现代 ...
- Windows下apache php wordpress配置
2. Use notepad to open httpd.conf config file. Make use the line "LoadModule rewrite_module mod ...
- express 笔记 app.helpers 和 app.locals
app.helpers 和app.dynamicHelpers 是express2.X使用的 分别为静态/动态 视图助手通过其注册函数, 例如 app.helpers({ <span style ...
- 年前辞职-WCF入门学习(3)
前言 [2了,刚写好的,手贱,点了删除,还真TMD的删除了,只好重写] 今天周六,去同事家聚了一个餐,感觉各种豪华. 废话不多说,前两集大致介绍了一下什么是WCF以及和WCF相关的WebService ...
- ES6的模块、构建工具及应用的发布
作者:寸志链接:https://zhuanlan.zhihu.com/p/19569085来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 总的说来就是按照将来的标准书写 ...
- 【codevs1044】导弹拦截问题与Dilworth定理
题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某 ...
- Java编程思想学习(十四) 枚举
关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 基本enum特性 调用enum的values()方法可以遍历enum实例,values()方法返 ...
- 求第N数大问题
问题: InputThe first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of ...
- Java的多线程机制系列:(三)synchronized的同步原理
synchronized关键字是JDK5之实现锁(包括互斥性和可见性)的唯一途径(volatile关键字能保证可见性,但不能保证互斥性,详细参见后文关于vloatile的详述章节),其在字节码上编译为 ...
- BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...