unity安卓用WWW实现读写JSON数据
编译环境unity2018+VS2017+JsonLite插件
JsonLite下载,本资源仅供学习交流使用!
链接:https://pa删n.bai删du.com/s/1qgnDui删7tKQAtgJ删2b8Y2m8w
提取码:vvh8
场景从json数据中读写一个plane(飞机)对象的数据,存到list中,JSON数据保存在本地。
//创建的plane结构体
public struct plane
{
public string PlaneName;
public int HP;
public int Level;
public double Exp2NextLevel;
public double Damage;
public double Speed;
public double TurnAngle;
public double AttackCD;
public double Energy;
public double EnergyRecover;
public double ShootConsume;
public double SpeedUpConsume;
public int ammoType;
public double bulletSpeed;
}
先看安卓版WWW实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class JsonLoad : MonoBehaviour
{
public List<plane> planes;
public static JsonLoad instance;
public bool LoadDonePlane = false;
public bool SaveDonwPlane=false;
private void Awake()
{
instance = this;
#if UNITY_ANDROID
StartCoroutine(LoadPlanePropertyAndroid_WWW());
#endif
}
private void planeDefine()
{
planes = new List<plane>()
{
//Pistol
new plane()
{
PlaneName="rocket",
HP=100,
Level=1,
Exp2NextLevel=200,
Damage=5,
Speed=20,
TurnAngle=360,
AttackCD=0.35f,
Energy=100,
EnergyRecover=5,
ShootConsume=6,
SpeedUpConsume=7,
ammoType=0,
bulletSpeed=0.9f
}
};
}
private void SaveByJson()
{
string filePath = Application.streamingAssetsPath + "/plane";
string saveJsonStr = JsonMapper.ToJson(planes);
StreamWriter sw = new StreamWriter(filePath);
sw.Write(saveJsonStr);
sw.Close();
}
private IEnumerator LoadPlanePropertyAndroid_WWW()
{
planes = new List<plane>();
string filePath = Application.streamingAssetsPath + "/plane";
WWW www = new WWW(filePath);
while (!www.isDone)
{
yield return null;
}
string jsonStr = www.text;
planes = JsonMapper.ToObject<List<plane>>(jsonStr);
if (planes.Count == 0)
{
Debug.Log("读取飞机Json文件失败");
}
else
{
// Debug.Log("飞机Json文件读取成功!");
LoadDonePlane = true;
}
}
}
PC版StreamWriter实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class JsonLoad : MonoBehaviour
{
public List<plane> planes;
public static JsonLoad instance;
private void Awake()
{
instance = this;
planeDefine();
SaveByJson();
LoadPlaneProperty();
}
private void planeDefine()//写入plane
{
planes = new List<plane>()
{
//Pistol
new plane()
{
PlaneName="rocket",
HP=100,
Level=1,
Exp2NextLevel=200,
Damage=5,
Speed=10,
TurnAngle=60,
AttackCD=0.35f,
Energy=100,
EnergyRecover=5,
ShootConsume=5,
SpeedUpConsume=5,
ammoType=0
}
};
}
private void SaveByJson()
{
string filePath = Application.streamingAssetsPath + "/plane";
string saveJsonStr = JsonMapper.ToJson(planes);
StreamWriter sw = new StreamWriter(filePath);
sw.Write(saveJsonStr);
sw.Close();
}
private void LoadPlaneProperty()
{
planes = new List<plane>();
string filePath = Application.streamingAssetsPath + "/plane";
if (File.Exists(filePath))
{
StreamReader sr = new StreamReader(filePath);
string jsonStr = sr.ReadToEnd();
sr.Close();
planes = JsonMapper.ToObject<List<plane>>(jsonStr);
}
if (planes.Count == 0)
{
Debug.Log("读取飞机Json文件失败");
}
else
{
Debug.Log("飞机Json文件读取成功!");
}
}
}
下载JsonLite导入到项目,复制代码,把plane改成你想存的类,就可以用了。