using UnityEngine;
using System.Collections;
using System;
[Serializable] //一定要有,同时不能继承MonoBehaviour
public class Config {
public int Num1; //我文档里有2个int,2个string类型
public int Num2;
public string String1;
public string String2;
// Use this for initialization
void Start () {
}
private static Config _data;
public static Config _Data
{
get
{
if (_data == null)
{
string json = System.IO.File.ReadAllText(Application.streamingAssetsPath + "/test.txt");
_data = JsonUtility.FromJson<Config>(json);
}
return _data;
}
}
}
using UnityEngine;
using System.Collections;
public class LoadConfig : MonoBehaviour {
// Use this for initialization
void Start () {
print(Config._Data.Num1);
print(Config._Data.String2);
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[Serializable] //一定要有,同时不能继承MonoBehaviour
public class Config {
public Weatherinfo weatherinfo; //这里的weatherinfo 就是上面信息的第一层weatherinfo,建立父级保存信息(名字要对应天气的weatherinfo)
}
[Serializable]
public class Weatherinfo //weatherinfo下的信息保存,类里的city 对应信息里的city,一次类推,我就写2个了;
{
public string city;
public int cityid;
}
再新建一个名字为LoadWWW的脚本,用于读取网络的数据:
using UnityEngine;
using System.Collections;
using System;
public class LoadWWW : MonoBehaviour {
Config _config = new Config();
// Use this for initialization
void Start () {
StartCoroutine("load");
Invoke("LoadMessage", 1f);
}
// Update is called once per frame
void Update () {
}
IEnumerator load()
{
WWW w = new WWW("www.weather.com.cn/data/sk/101010100.html");//加载某网页数据,根据自己需求改
yield return w;
string json = w.text;
print(json);
_config = JsonUtility.FromJson<Config>(json);
}
void LoadMessage()
{
print(_config.weatherinfo.city);
print(_config.weatherinfo.cityid);
}
}
using UnityEngine;
using System.Collections;
using System;
[Serializable] //一定要有,同时不能继承MonoBehaviour
public class Config {
public int Num1; //我文档里有2个int,2个string类型
public int Num2;
public string String1;
public string String2;
// Use this for initialization
void Start () {
}
private static Config _data;
public static Config _Data
{
get
{
if (_data == null)
{
string json = System.IO.File.ReadAllText(Application.streamingAssetsPath + "/test.txt");
_data = JsonUtility.FromJson<Config>(json);
}
return _data;
}
}
}
using UnityEngine;
using System.Collections;
public class LoadConfig : MonoBehaviour {
// Use this for initialization
void Start () {
print(Config._Data.Num1);
print(Config._Data.String2);
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[Serializable] //一定要有,同时不能继承MonoBehaviour
public class Config {
public Weatherinfo weatherinfo; //这里的weatherinfo 就是上面信息的第一层weatherinfo,建立父级保存信息(名字要对应天气的weatherinfo)
}
[Serializable]
public class Weatherinfo //weatherinfo下的信息保存,类里的city 对应信息里的city,一次类推,我就写2个了;
{
public string city;
public int cityid;
}
再新建一个名字为LoadWWW的脚本,用于读取网络的数据:
using UnityEngine;
using System.Collections;
using System;
public class LoadWWW : MonoBehaviour {
Config _config = new Config();
// Use this for initialization
void Start () {
StartCoroutine("load");
Invoke("LoadMessage", 1f);
}
// Update is called once per frame
void Update () {
}
IEnumerator load()
{
WWW w = new WWW("www.weather.com.cn/data/sk/101010100.html");//加载某网页数据,根据自己需求改
yield return w;
string json = w.text;
print(json);
_config = JsonUtility.FromJson<Config>(json);
}
void LoadMessage()
{
print(_config.weatherinfo.city);
print(_config.weatherinfo.cityid);
}
}