Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using System;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- public class gamecontrol : MonoBehaviour {
- public static gamecontrol control;
- public float health ;
- public Text healthtxt;
- public float PositionX;
- public float PositionY;
- public float PositionZ;
- public Transform player;
- int id[] = new int[20];
- public GameObject[] item = new GameObject[20];
- public Transform inventory;
- // Use this for initialization
- void Awake () {
- if (control == null) {
- DontDestroyOnLoad (gameObject);
- control = this;
- } else if (control != this) {
- Destroy (gameObject);
- }
- }
- // Update is called once per frame
- void Update () {
- healthtxt.text = "Health : " + health;
- if (Input.GetKey (KeyCode.H)) {
- gamecontrol.control.health += 1;
- }
- }
- public void Save (){
- BinaryFormatter bf = new BinaryFormatter ();
- FileStream file = File.Create (Application.persistentDataPath + "/playerData.tst");
- playerDate.playerdata = new playerDate ();
- playerDate.playerdata.health = health;
- PositionX = player.transform.position.x;
- PositionY = player.transform.position.y;
- PositionZ = player.transform.position.z;
- playerDate.playerdata.positionX = PositionX;
- playerDate.playerdata.positionY = PositionY;
- playerDate.playerdata.positionZ = PositionZ;
- fo(int i = 0; i < item.length; i++)
- {
- if (item[i].tag == "InInventory")
- {
- playerDate.playerdata.ID[i] = id[i];
- }
- }
- bf.Serialize (file, playerDate.playerdata);
- }
- public void Load(){
- if (File.Exists(Application.persistentDataPath + "/playerData.tst"))
- {
- BinaryFormatter bf = new BinaryFormatter();
- FileStream file = File.Open(Application.persistentDataPath + "/playerData.tst" , FileMode.Open);
- playerDate.playerdata = (playerDate) bf.Deserialize(file);
- file.Close();
- health = playerDate.playerdata.health;
- PositionX = playerDate.playerdata.positionX;
- PositionY = playerDate.playerdata.positionY;
- PositionZ = playerDate.playerdata.positionZ;
- id = playerDate.playerdata.ID;
- for(int i = 0; i < id.length;i++)
- {
- if (item[i].tag == "InInventory")
- {
- if (item[i].GetComponent<aboutItem> ().ID == id[i]) {
- GameObject itemInstantiated = Instantiate (item[i]);
- itemInstantiated.transform.SetParent (inventory);
- }
- }
- }
- Vector3 savedpos = new Vector3 (PositionX,PositionY,PositionZ);
- player.position = savedpos;
- }
- }
- }
- [Serializable]
- class playerDate
- {
- public static playerDate playerdata;
- public float health;
- public float positionX;
- public float positionY;
- public float positionZ;
- public int[] ID = new int[20];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement