Advertisement
Guest User

SaveScript

a guest
Jan 13th, 2019
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class LevelSave : MonoBehaviour {
  6.  
  7.     public int floorContainerChildCount;
  8.     public GameObject FloorContainer;
  9.     public PlayerData[] playerData;
  10.  
  11.     [System.Serializable]
  12.     public class PlayerData
  13.     {
  14.         public string name;
  15.         public int x;
  16.         public int y;
  17.     }
  18.  
  19.     void Awake ()
  20.     {
  21.         FloorContainer = GameObject.Find("FloorContainer");
  22.     }
  23.  
  24.     private void Update()
  25.     {
  26.         floorContainerChildCount = FloorContainer.transform.childCount;
  27.         if (Input.GetKeyDown(KeyCode.Space))
  28.         {
  29.             Save();
  30.         }
  31.     }
  32.  
  33.     void Save()
  34.     {
  35.         PlayerData tempData = new PlayerData();
  36.         tempData.name = "a";
  37.         tempData.x = 0;
  38.         tempData.y = 0;
  39.  
  40.         playerData = new PlayerData[FloorContainer.transform.childCount];
  41.         int childCounter = 0;
  42.         //foreach (Transform child in FloorContainer.transform)
  43.         for (int i = 0; i < FloorContainer.transform.childCount; i++)
  44.         {
  45.             playerData[i] = tempData;
  46.         }
  47.         for (int i = 0; i < FloorContainer.transform.childCount; i++)
  48.         {
  49.             playerData[i].name = FloorContainer.transform.GetChild(i).name; //child.name;
  50.             playerData[i].x = FloorContainer.transform.GetChild(i).GetComponent<FloorValuesHolder>().x; //child.gameObject.GetComponent<FloorValuesHolder>().x;
  51.             playerData[i].y = FloorContainer.transform.GetChild(i).GetComponent<FloorValuesHolder>().y; //child.gameObject.GetComponent<FloorValuesHolder>().y;
  52.             //childCounter++;
  53.         }
  54.  
  55.         string json = JsonUtility.ToJson(playerData);
  56.         print("This is JSON " + json);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement