Unity数据持久化—Json存档

文章描述了一个使用Unity开发的项目中,如何通过Json进行存档和读档操作,包括SaveData和SaveList类的设计,以及使用JsonUtility进行数据序列化和反序列化的代码示例。
摘要由CSDN通过智能技术生成

项目需求为:

1.实现存档列表,显示存档截图,可以查看之前保存的所有存档

2.点击存档直接加载到场景

首先,定义两个类,用于声明存档列表和存档所需要的List

[System.Serializable]
public class SaveData
{
    //存储目标物的位置和朝向
    public List<Vector3> targetPosition = new List<Vector3>();
    public List<Quaternion> targetRotation = new List<Quaternion>();
    //保存截图
    public string targetImg;
}

[System.Serializable]
public class SaveList
{   
    //存档列表存储各个存档的地址
    public List<string> DataPath = new List<string>();
}

存储存档列表、存档

//存档
public void TestSave()
{        
    //取当前时间来做存档的名字
    string now = DateTime.Now.ToString("yyyy-M-d-HH-mm-ss");
    string path = Application.dataPath + "/StreamingFile/" + now;
    //调用存档方法
    SaveData saveData = CreatSave(path);
    //利用jsonutility将savedata转为json
    string saveJson = JsonUtility.ToJson(saveData);
    //将json写入文件
    //新建StreamWriter,写入json
    StreamWriter streamWriter = new StreamWriter(path + ".json");
    streamWriter.Write(saveJson);
    //关闭StreamWriter
    streamWriter.Close();
    TestListSave(path);
    Debug.Log("保存成功");
}

/// <summary>
/// 存储存档列表
/// </summary>
/// <param name="pathname"></param>
void TestListSave(string pathname)
{
    SaveList sl = new SaveList();
    string path = Application.dataPath + "/StreamingFile/list.list";
    //alllist是临时List,用于存所有的存档个数
    alllist.Add(pathname);
    sl.DataPath = alllist;
    //将所有的存档个数打包保存(覆盖式)
    string saveList = JsonUtility.ToJson(sl);
    StreamWriter streamWriter = new StreamWriter(path);
    streamWriter.Write(saveList);
    streamWriter.Close();        
}

/// <summary>
/// 创建Save对象,存储当前状态
/// </summary>
/// <returns></returns>
private SaveData CreatSave(string path)
{
    SaveData saveTest = new SaveData();
    //all是所有的目标物的父物体,将位置和旋转存入list
    for (int i = 0;i < all.childCount;i++)
    {
        saveTest.targetPosition.Add(all.GetChild(i).position);
        saveTest.targetRotation.Add(all.GetChild(i).rotation);
    }
     //将存档截图地址保存
     saveTest.targetImg = path;
     //截图功能
     CameraCapture(path, main, new Rect(0, 0, Screen.width, Screen.height));
    return saveTest;
}

截图相关代码

  
//指定相机指定范围
public static void CameraCapture(string i, Camera camera, Rect rect)
{        
    string path = Path.Combine(i, $"{i}.png");

    int w = (int)rect.width;
    int h = (int)rect.height;

    RenderTexture rt = new RenderTexture(w, h, 0);
    //将相机渲染内容存到指定RenderTexture
    camera.targetTexture = rt;
    camera.Render();

    //激活指定RenderTexture
    RenderTexture.active = rt;

    //参数4:mipchain多级渐远纹理
    Texture2D t2D = new Texture2D(w, h, TextureFormat.ARGB32, true);

    //防止截黑屏,可能会报错
    //yield return new WaitForEndOfFrame();
    //把RenderTexture像素读到texture2d
    t2D.ReadPixels(rect, 0, 0);
    t2D.Apply();

    //存成PNG
    byte[] bytes = t2D.EncodeToPNG();
    File.WriteAllBytes(path, bytes);

    //用完重置、销毁
    camera.targetTexture = null;
    RenderTexture.active = null;
    GameObject.Destroy(rt);
}

public static Sprite LoadShot(string i)
{
    string shotPath = Application.dataPath + "/StreamingFile/Shot";
    var path = Path.Combine(shotPath, $"{i}.png");

    Texture2D t = new Texture2D(640, 360);
    t.LoadImage(GetImgByte(path));
    return Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
}

static byte[] GetImgByte(string path)
{
    FileStream s = new FileStream(path, FileMode.Open);
    byte[] imgByte = new byte[s.Length];
    s.Read(imgByte, 0, imgByte.Length);
    s.Close();
    return imgByte;
}

public static void deleteShot(string i)
{
    string shotPath = Application.dataPath + "/StreamingFile/Shot";
    var path = Path.Combine(shotPath, $"{i}.png");
    if (File.Exists(path))
    {
        File.Delete(path);
        Debug.Log($"删除{i}");
    }
}

读取存档列表和存档

//读档
public void LoadTest(Text text)
{        
   string filepath = Application.dataPath + "/StreamingFile/" + text.text + ".json";
   if (File.Exists(filepath))
   {
       //创建StreamReader,读取json
       StreamReader streamReader = new StreamReader(filepath);
       //将json赋值给string
       string json = streamReader.ReadToEnd();
       //关闭
       streamReader.Close();
       //将字符串json转换为savedata对象
       SaveData saveData = JsonUtility.FromJson<SaveData>(json);
       SetLoadSave(saveData);
   }
   else
   {
       Debug.Log("没有存档");
   }
}
/// <summary>
/// 根据列表加载存档处理数据
/// </summary>
/// <param name="pathname"></param>
void TestListLoad()
{
    string path = Application.dataPath + "/StreamingFile/list.list";
    if (File.Exists(path))
    {
        //创建StreamReader,读取json
        StreamReader streamReader = new StreamReader(path);
        //将json赋值给string
        string json = streamReader.ReadToEnd();
        //关闭
        streamReader.Close();
        //将字符串json转换为SaveList对象
        SaveList saveList = JsonUtility.FromJson<SaveList>(json);
        if(saveList != null)
        {
            for (int i = 0; i< saveList.DataPath.Count; i++)
            {
               根据数据显示scroll view下的物体并将数据赋过去
               var obj = list.transform.GetChild(0).GetChild(0).GetChild(i);
                obj.gameObject.SetActive(true);
                obj.GetComponent<Image>().sprite = LoadShot(saveList.DataPath[i]);
                string str = saveList.DataPath[i].Split("/")[5];
                obj.GetChild(2).GetComponent<Text>().text = str.Substring(0, 18);
                //读档时候将之前的列表内容读到临时list暂存
                alllist.Add(saveList.DataPath[i]);
            }
        }
    }
}

 /// <summary>
 /// 将savedata数据赋值给当前场景
 /// </summary>
 /// <param name="saveData"></param>
 private void SetLoadSave( SaveData saveData)
 {
     if (saveData != null)
     {
         for (int i = 0; i < all.childCount; i++)
         {
             all.GetChild(i).position = saveData.targetPosition[i];
             all.GetChild(i).rotation = saveData.targetRotation[i];
         }
     }
 }

项目地址:【免费】unity数据持久化json存档与读档资源-CSDN文库

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity游戏中,可以使用JSON格式进行数据存储。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,通过简单的键值对来表示数据。可以使用LitJson.dll插件来实现在Unity中读取和存储JSON数据。 以下是使用JSON进行数据存储的步骤: 1. 创建一个技能类(Skill),在该类中封装需要存储的字段,比如Id和Name。 2. 创建Skill类型的List集合,并向其中添加Skill对象。 3. 使用LitJson中的JsonMapper.ToJson()方法将List集合转换为JSON格式的字符串。 4. 使用File.WriteAllText()方法将JSON字符串写入文件。 5. 为了防止中文乱码,可以使用Regex.Replace()方法将转义字符转码为实际字符。 在Unity中使用LitJson.dll插件来读取和存储JSON数据需要将LitJson.dll文件放入Unity的Plugins文件夹中。编写相应的代码来实现读取和存储的功能。可以参考相关的教程或视频来学习更详细的使用方法。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Unity Json读取存储](https://download.csdn.net/download/qq_42126516/12251667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [UnityJSON数据存储)](https://blog.csdn.net/m0_74427422/article/details/129540637)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [学习记录:unityjson存储](https://blog.csdn.net/shijinlinaaa/article/details/125896790)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值