Ejemplo n.º 1
0
    public void saveViaJson()
    {
        Debug.Log("kkkk");
        go = GameObject.FindGameObjectsWithTag("fool");   //to find out all gameobject which with tag ("fool"), that one is to find out the gameobjec which player created
        saveddata      data       = new saveddata();      //the class which hold the data
        List <Vector3> positionls = new List <Vector3>(); //object position
        List <string>  path       = new List <string>();  //object prefab file location
        List <Vector3> rotals     = new List <Vector3>(); //objcet rotation
        List <Vector3> scales     = new List <Vector3>(); //objcet scale

        //for lopp to save each element which player created
        foreach (var g in go)
        {
            Vector3 position = new Vector3();
            Vector3 rota     = new Vector3();
            Vector3 sca      = new Vector3();

            position = g.transform.position;
            rota     = g.transform.eulerAngles;
            sca      = g.transform.localScale;

            //to check the object's name and  take out the "(clone)"
            var    pos = g.name.IndexOf("(");
            string ojname;
            if (pos < 0)
            {
                ojname = g.name;
            }
            else
            {
                ojname = g.name.Substring(0, pos);
            }
            //Debug.Log(sca);
            path.Add(ojname);
            rotals.Add(rota);
            scales.Add(sca);
            positionls.Add(position);
        }

        data.go       = positionls;
        data.path     = path;
        data.rotation = rotals;
        data.scale    = scales;
        data.xlength  = GameObject.FindGameObjectWithTag("floorplan").GetComponent <GridSpawner>().xLength;
        data.zlength  = GameObject.FindGameObjectWithTag("floorplan").GetComponent <GridSpawner>().zLength;
        //creat a json file to save the game data
        string json = JsonUtility.ToJson(data);

        File.WriteAllText(Application.persistentDataPath + "/savefile.json", json);
    }
Ejemplo n.º 2
0
    public void loadViaJson()
    {     //delete the gameobject which haved save in the file
        go = GameObject.FindGameObjectsWithTag("fool");
        foreach (var g in go)
        {
            Destroy(g);
        }

        //read the .json file and change to readable saved data
        string    json  = File.ReadAllText(Application.persistentDataPath + "/savefile.json");
        saveddata saved = JsonUtility.FromJson <saveddata>(json);

        //to load the saved data
        int    count     = 0;
        string floorpath = "Assets/Prefab/";

        //for loop to load the data
        foreach (var g in saved.path)
        {
            string pathname = floorpath + g + ".prefab";
            var    grid     = Resources.Load(pathname, typeof(GameObject));//for build
            //AssetDatabase.LoadAssetAtPath(pathname, typeof(GameObject));//for unity editor
            GameObject pre = Instantiate((GameObject)grid, saved.go[count], Quaternion.identity);
            pre.transform.SetParent(GameObject.FindGameObjectWithTag("floorplan").transform);
            pre.transform.eulerAngles = saved.rotation[count];
            pre.transform.localScale  = saved.scale[count];
            if (g == "GroundGridCube")
            {
                pre.GetComponentInChildren <GroundCube>().SetBuildSystem(bs);
            }

            count++;
        }

        GameObject.FindGameObjectWithTag("floorplan").GetComponent <GridSpawner>().xLength = saved.xlength;
        GameObject.FindGameObjectWithTag("floorplan").GetComponent <GridSpawner>().zLength = saved.zlength;
    }