Last active
April 4, 2022 00:26
-
-
Save 314pies/ee4045f163e63a3d4d5e18e7876bc1f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Tmp | |
{ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
[Serializable] | |
public class mazeCube | |
{ | |
public int preFabType; | |
public Vector3 position; | |
public Vector3 rotation; | |
} | |
[Serializable] | |
public class mazesData | |
{ | |
public mazeCube[] datas; | |
} | |
public class MazeSerialization : MonoBehaviour | |
{ | |
public Transform MazeRoot; | |
public void MazeToJson() | |
{ | |
List<mazeCube> cubes = new List<mazeCube>(); | |
foreach (Transform child in MazeRoot) | |
{ | |
mazeCube cube = new mazeCube(); | |
cube.position = child.localPosition; | |
cube.rotation = child.localEulerAngles; | |
cube.preFabType = 0; //Should assign prefab type | |
cubes.Add(cube); | |
} | |
mazesData data = new mazesData(); | |
data.datas = cubes.ToArray(); | |
string json = JsonUtility.ToJson(data); | |
print(json); | |
SaveEncodedFile(json, "maze.json"); | |
} | |
public void SaveEncodedFile(String data, String fileName) | |
{ | |
string path = Application.persistentDataPath; | |
File.WriteAllText(path + "/" + fileName, data); | |
} | |
public TextAsset JsonTextFile;//Assign from inspector | |
public Transform CloneMazeRoot;//The root of the re-created maze | |
public GameObject[] Prefabs; | |
public void JsonToMaze() | |
{ | |
mazesData mazeDatas2 = JsonUtility.FromJson<mazesData>(JsonTextFile.text); | |
foreach (var d in mazeDatas2.datas) | |
{ | |
var cube_instant = Instantiate(Prefabs[d.preFabType]); | |
cube_instant.transform.SetParent(CloneMazeRoot); | |
cube_instant.transform.localPosition = d.position; | |
cube_instant.transform.localEulerAngles = d.rotation; | |
cube_instant.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment