public void Save() { var blocks = GameObject.FindGameObjectsWithTag("EditBlock"); var blockTransforms = new BlockTransform[blocks.Length]; for (var index = 0; index < blocks.Length; index++) { var blockTransform = new BlockTransform(); blockTransform.blockPosition = blocks[index].transform.localPosition; blockTransform.blockRotation = blocks[index].transform.localEulerAngles; blockTransform.blockType = blocks[index].name; blockTransforms[index] = blockTransform; } var stagePlacement = new StagePlacement { blockTransform = blockTransforms, charaPosition = chara.transform.localPosition, goalPosiotion = goal.transform.localPosition }; Load(); saveDataClass.saveData[saveNumber] = stagePlacement; var jsonString = JsonUtility.ToJson(saveDataClass); using (var file = new FileStream(Application.persistentDataPath + "/save.json", FileMode.Create, FileAccess.Write)) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(jsonString); file.Write(bytes, 0, bytes.Length); } saveDataClass = null; }
public void Load() { saveDataClass = new SaveDataClass(); if (!File.Exists(Application.persistentDataPath + "/save.json")) { var blockTransform = new BlockTransform[11] { new BlockTransform { blockPosition = new Vector3(0, 0, 0), blockRotation = Vector3.zero, blockType = "L" }, new BlockTransform { blockPosition = new Vector3(1, 0, 0), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(3, 0, 0), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(0, 0, 1), blockRotation = Vector3.zero, blockType = "Obs" }, new BlockTransform { blockPosition = new Vector3(1, 0, 1), blockRotation = Vector3.zero, blockType = "H" }, new BlockTransform { blockPosition = new Vector3(2, 0, 1), blockRotation = Vector3.zero, blockType = "Obs" }, new BlockTransform { blockPosition = new Vector3(0, 0, 2), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(2, 0, 2), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(0, 0, 3), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(2, 0, 3), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(3, 0, 3), blockRotation = new Vector3(0, 90, 0), blockType = "H" }, }; var stagePlacement = new StagePlacement[6]; stagePlacement[0] = new StagePlacement { blockTransform = blockTransform, charaPosition = new Vector3(3, 0.75f, 3), goalPosiotion = new Vector3(0, 0, 3) }; blockTransform = new BlockTransform[14] { new BlockTransform { blockPosition = new Vector3(0, 0, 0), blockRotation = new Vector3(0, 90, 0), blockType = "L" }, new BlockTransform { blockPosition = new Vector3(1, 0, 0), blockRotation = Vector3.zero, blockType = "H" }, new BlockTransform { blockPosition = new Vector3(2, 0, 0), blockRotation = Vector3.zero, blockType = "H" }, new BlockTransform { blockPosition = new Vector3(3, 0, 0), blockRotation = Vector3.zero, blockType = "L" }, new BlockTransform { blockPosition = new Vector3(0, 0, 1), blockRotation = new Vector3(0, 90, 0), blockType = "H" }, new BlockTransform { blockPosition = new Vector3(2, 0, 1), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(3, 0, 1), blockRotation = new Vector3(0, 90, 0), blockType = "H" }, new BlockTransform { blockPosition = new Vector3(0, 0, 2), blockRotation = new Vector3(0, 90, 0), blockType = "H" }, new BlockTransform { blockPosition = new Vector3(1, 0, 2), blockRotation = Vector3.zero, blockType = "F" }, new BlockTransform { blockPosition = new Vector3(3, 0, 2), blockRotation = new Vector3(0, 90, 0), blockType = "H" }, new BlockTransform { blockPosition = new Vector3(0, 0, 3), blockRotation = new Vector3(0, 180, 0), blockType = "L" }, new BlockTransform { blockPosition = new Vector3(1, 0, 3), blockRotation = Vector3.zero, blockType = "H" }, new BlockTransform { blockPosition = new Vector3(2, 0, 3), blockRotation = Vector3.zero, blockType = "H" }, new BlockTransform { blockPosition = new Vector3(3, 0, 3), blockRotation = new Vector3(0, -90, 0), blockType = "L" } }; stagePlacement[1] = new StagePlacement { blockTransform = blockTransform, charaPosition = new Vector3(0, 0.75f, 0), goalPosiotion = new Vector3(2, 0, 3) }; saveDataClass.saveData = stagePlacement; return; } using (var file = new FileStream(Application.persistentDataPath + "/save.json", FileMode.Open, FileAccess.Read)) { byte[] bytes = new byte[file.Length]; file.Read(bytes, 0, bytes.Length); var jsonString = System.Text.Encoding.UTF8.GetString(bytes); saveDataClass = JsonUtility.FromJson <SaveDataClass>(jsonString); } }