Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. using System;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.IO;
  8.  
  9. public class gamecontrol : MonoBehaviour {
  10. public static gamecontrol control;
  11. public float health ;
  12. public Text healthtxt;
  13. public float PositionX;
  14. public float PositionY;
  15. public float PositionZ;
  16. public Transform player;
  17. int id[] = new int[20];
  18. public GameObject[] item = new GameObject[20];
  19. public Transform inventory;
  20.  
  21.  
  22.  
  23. // Use this for initialization
  24. void Awake () {
  25. if (control == null) {
  26. DontDestroyOnLoad (gameObject);
  27. control = this;
  28. } else if (control != this) {
  29. Destroy (gameObject);
  30. }
  31.  
  32. }
  33. // Update is called once per frame
  34. void Update () {
  35. healthtxt.text = "Health : " + health;
  36. if (Input.GetKey (KeyCode.H)) {
  37. gamecontrol.control.health += 1;
  38. }
  39. }
  40. public void Save (){
  41. BinaryFormatter bf = new BinaryFormatter ();
  42. FileStream file = File.Create (Application.persistentDataPath + "/playerData.tst");
  43. playerDate.playerdata = new playerDate ();
  44. playerDate.playerdata.health = health;
  45. PositionX = player.transform.position.x;
  46. PositionY = player.transform.position.y;
  47. PositionZ = player.transform.position.z;
  48. playerDate.playerdata.positionX = PositionX;
  49. playerDate.playerdata.positionY = PositionY;
  50. playerDate.playerdata.positionZ = PositionZ;
  51. fo(int i = 0; i < item.length; i++)
  52. {
  53. if (item[i].tag == "InInventory")
  54. {
  55. playerDate.playerdata.ID[i] = id[i];
  56. }
  57. }
  58. bf.Serialize (file, playerDate.playerdata);
  59. }
  60. public void Load(){
  61. if (File.Exists(Application.persistentDataPath + "/playerData.tst"))
  62. {
  63. BinaryFormatter bf = new BinaryFormatter();
  64. FileStream file = File.Open(Application.persistentDataPath + "/playerData.tst" , FileMode.Open);
  65. playerDate.playerdata = (playerDate) bf.Deserialize(file);
  66. file.Close();
  67. health = playerDate.playerdata.health;
  68. PositionX = playerDate.playerdata.positionX;
  69. PositionY = playerDate.playerdata.positionY;
  70. PositionZ = playerDate.playerdata.positionZ;
  71. id = playerDate.playerdata.ID;
  72. for(int i = 0; i < id.length;i++)
  73. {
  74. if (item[i].tag == "InInventory")
  75. {
  76. if (item[i].GetComponent<aboutItem> ().ID == id[i]) {
  77. GameObject itemInstantiated = Instantiate (item[i]);
  78. itemInstantiated.transform.SetParent (inventory);
  79. }
  80. }
  81. }
  82. Vector3 savedpos = new Vector3 (PositionX,PositionY,PositionZ);
  83. player.position = savedpos;
  84. }
  85. }
  86. }
  87. [Serializable]
  88. class playerDate
  89. {
  90. public static playerDate playerdata;
  91. public float health;
  92. public float positionX;
  93. public float positionY;
  94. public float positionZ;
  95. public int[] ID = new int[20];
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement