数据存档

首先写一个脚本 GameData在内写数据的模板 ,因为数据不止一条 因此要写一个list来存这些模板

    [Serialization]
    //这是自己定义的模板
    public class GameData{
      public string PeopleName;
      public List<Vector3> pos;
    }
    
    //这里是很多条模板
public class GameDatas
{
   public List<GameData> dataList;
}

然后我们写一个脚本来记录数据的存储
首先

//数据的写入
    void WriteData(GameDatas data)
    {
      StreamWriter sw = new StreamWriter("C:/Users/37864/Desktop/1.json", false, Encoding.UTF8);
        string json = JsonUtility.ToJson(data);
        sw.WriteLine(json);
        sw.Close();//关闭
        sw.Dispose();//销毁流
    }
//数据的读取
GameDatas ToReadData()
{
  StreamReader sr = new StreamReader("C:/Users/37864/Desktop/1.json", Encoding.UTF8);
        string datas = sr.ReadToEnd();
        GameDatas team = new GameDatas();
        team = JsonUtility.FromJson<GameDatas>(datas);
        sr.Close();
        return team;
}

剩下的就是逻辑的事情,首先我们要实现的功能是打地鼠编辑功能就是说可以出地鼠的几个洞我们希望可以选择而且希望选择完之后可以将洞的坐标保存下来,和输入的名字一样存为一条数据也就是我们的模板那样的数据,而且我们当第二个人玩的时候可以存第二条数据,且和第一条数据一起保存在本地。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class GameEditor : MonoBehaviour {

   //这里是我们的UI声明
        public List<Button> bts;//如果没有底下的find我们就需要把他找出来
        private Button writeButton;
        private InputField _nameField;
//这里是存储数据声明
//我们首先new一个类对象来存放数据
    GameDatas datas = new GameDatas(); 
    //new一个集合来存放很多模板
    List<GameData> s = new List<GameData>();    
    //new一个类对象来存放新的模板
    GameData gameData = new GameData();
    //new一个集合来存放pos的位置
    List<Vector3> points = new List<Vector3>();
    //new一个类对象来存放读取到的数据
    GameDatas readData = new GameDatas();

    void Awake()
    {
        //第一次--普通写入
        //我们new一个集合来表示许多的Button,因为我们的Button只在Awake里面用到了因此就只需要在这里进行变量声明
        List<Button> bts = new List<Button>();
        //然后依次把Button在列表中find出来
        for (int i = 0; i < 8; i++)
        {
            string buttonName = "/Canvas/bt" + i;
            bts.Add(GameObject.Find(buttonName).GetComponent<Button>());
        }
        writeButton = GameObject.Find("/Canvas/write").GetComponent<Button>();
        //这是许多的按钮的监听事件
        bts[0].onClick.AddListener(delegate { ButtonClick(bts[0]); });
        bts[1].onClick.AddListener(delegate { ButtonClick(bts[1]); });
         bts[2].onClick.AddListener(delegate { ButtonClick(bts[2]); });
          bts[3].onClick.AddListener(delegate { ButtonClick(bts[3]); });
           bts[4].onClick.AddListener(delegate { ButtonClick(bts[4]); });
            bts[5].onClick.AddListener(delegate { ButtonClick(bts[5]); });
             bts[6].onClick.AddListener(delegate { ButtonClick(bts[6]); });
              bts[7].onClick.AddListener(delegate { ButtonClick(bts[7]); });
        //我们先把数据保存下来,然后当点击writeButton的时候一齐把数据写入
        writeButton.onClick.AddListener(() => {
            //首先判断文件是否存在
            bool inspect = File.Exists("C:/Users/37864/Desktop/1.json");
            if (inspect)
            {
                Debug.Log("拥有文件,读取数据");
                GameDatas readDatas = ToReadData();
                if(readDatas != null) //有数据
                {
                    gameData.pos = points;
                    readDatas.dataList.Add(gameData);   
                    WriteData(readDatas);
                }
            }
            else /否则就是第一次写入
            {
                gameData.pos = points;
                s.Add(gameData);
                datas.dataList = s;
                //datas1=readData;
                //WriteData(datas1);
                WriteData(datas);
            }
        });    
        });

        _nameField = GameObject.Find("/Canvas/NameInputField").GetComponent<InputField>();
        _nameField.onEndEdit.AddListener(delegate { NameEnd(); });
        _nameField.contentType = InputField.ContentType.Name;                
//------------------------------------------------------------------------------
        //② 写入(不读取,直接写入)
            //先打开文件,在末尾添加","
            //写入第一次数据(在数据末尾添加,而不是覆盖)

      //读取
        //先看看是否文件存在
            //存在
                //判断数据是否为空
                    //为null
                        //文件损坏,return,不执行任何操作
                    //不为null
                        //读取到人物数据,创建人物信息
            //不存在
                //return,不执行任何操作
    }
    //按钮点击执行的方法
        void ButtonClick(Button bt)
        {
            points.Add(GetPosition(bt));
        }
  //输入框结束之后执行的方法  
        private void NameEnd()
        {
            string _name = _nameField.text;
            gameData.PeopleName = _name;
        }
        //写入数据 
        void WriteData(GameDatas data)
        {
    
            StreamWriter sw = new StreamWriter("C:/Users/37864/Desktop/1.json", false, Encoding.UTF8);
            string json = JsonUtility.ToJson(data);
            sw.WriteLine(json);
            sw.Close();//关闭
        sw.Dispose();//销毁流
    }

    //读取数据
    GameDatas ToReadData()
    {
        StreamReader sr = new StreamReader("C:/Users/37864/Desktop/1.json", Encoding.UTF8);
        string datas = sr.ReadToEnd();
        GameDatas team = new GameDatas();
        team = JsonUtility.FromJson<GameDatas>(datas);
        sr.Close();
        return team;
    }

//获取按钮的位置当点击之后获取位置
private Vector3 GetPosition(Button bt)
{
return bt.transform.position;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值