纳金网
标题:
unity json数据读、取txt文件内容
[打印本页]
作者:
狂风大尉
时间:
2014-7-31 01:55
标题:
unity json数据读、取txt文件内容
1将从服务器端下载的JSON数据写入到本地的txt文件中
2从txt中读取此内容
这个cs文件写:
void
actFunction
(
string
[]
content
)
{
print
(
"
Activity has got:
"
);
Debug
.
Log
(
"
content0:
"
+
content
[
0
]);
Debug
.
Log
(
"
content1:
"
+
content
[
1
]);
Debug
.
Log
(
"
content2:
"
+
content
[
2
]);
Debug
.
Log
(
"
content3:
"
+
content
[
3
]);
Debug
.
Log
(
"
content4:
"
+
content
[
4
]);
//
save
into
database
#
if
UNITY_IPHONE
Path
=
PlayerData
.
sqlpathOfIphoneSQL
;
#
endif
#
if
UNITY_ANDROID
Path
=
PlayerData
.
sqlpathOfAndroidSQL
;
#
endif
#
if
UNITY_EDITOR
Path
=
PlayerData
.
sqlpathOfEditorSQL
;
#
endif
string
filepath
=
Path
+
"
json.txt
"
;
File
.
WriteAllText
(
filepath
,
content
[
4
]);
}
另一个cs文件读
void
Start
()
{
//
save
into
database
#
if
UNITY_IPHONE
Path
=
PlayerData
.
sqlpathOfIphoneSQL
;
#
endif
#
if
UNITY_ANDROID
Path
=
PlayerData
.
sqlpathOfAndroidSQL
;
#
endif
#
if
UNITY_EDITOR
Path
=
PlayerData
.
sqlpathOfEditorSQL
;
#
endif
string
filepath
=
Path
+
"
json.txt
"
;
string
filecontent
=
File
.
ReadAllText
(
filepath
);
act
=
JsonMapper
.
ToObject
(
filecontent
);
//解析json数据
DataParses
();
}
//解析:用到Litjson.dll
void
DataParses
()
{
for
(
int
i
=
0
;
i
<
act
.
Count
;
i
++)
{
Debug
.
Log
(
act
[
i
][
"
title
"
]+
"
-
"
+
act
[
i
][
"
content
"
]);
}
//将载入的内容在界面上呈现出来
loadDataToView
(
act
.
Count
);
}
作者:
hyui
时间:
2014-7-31 19:16
Thanks for sharing this !
作者:
我不再年轻
时间:
2014-7-31 19:21
学习了, 很有用的代码
作者:
Kadina
时间:
2014-8-2 07:07
谢谢楼主分享!
欢迎光临 纳金网 (http://old.narkii.com/club/)
Powered by Discuz! X2.5