unity安卓用WWW实现读写JSON数据,PC用StreamWriter

unity安卓用WWW实现读写JSON数据

编译环境unity2018+VS2017+JsonLite插件

JsonLite下载,本资源仅供学习交流使用!
链接:https://pa删n.bai删du.com/s/1qgnDui删7tKQAtgJ删2b8Y2m8w
提取码:vvh8

场景从json数据中读写一个plane(飞机)对象的数据,存到list中,JSON数据保存在本地。
目录结构Asset/StreamingAssets

//创建的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改成你想存的类,就可以用了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值