Advertisement
GabrielRabeloLopes

SaveGame

Dec 9th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text;
  5. using System;
  6.  
  7. public class LoadGameObjects : MonoBehaviour
  8. {
  9. SaveGameObjects SGO;
  10.  
  11. string specialPath = "";
  12. GameObject[] ObjectsToLoad;
  13. String[] gameObjectTags;
  14. string keyWord = "";
  15.  
  16. void Start()
  17. {
  18. SGO = FindObjectOfType(typeof(SaveGameObjects)) as SaveGameObjects;
  19. specialPath = SGO.specialPath;
  20. ObjectsToLoad = SGO.gameObjectPrefabs;
  21. gameObjectTags = SGO.gameObjectIDs;
  22. }
  23.  
  24. void Update()
  25. {
  26. if (Input.GetKeyDown(KeyCode.L))
  27. {
  28. Load(1);
  29. }
  30. }
  31.  
  32. public void RefreshGameObjects(int slot)
  33. {
  34. //Delete All Current Loaded Objects
  35. object[] allObjects = FindObjectsOfType(typeof(GameObject));
  36. foreach (GameObject obj in allObjects)
  37. {
  38. if (obj.name.Contains(keyWord))
  39. {
  40. GameObject.Destroy(obj);
  41. }
  42. }
  43.  
  44. Load(slot);
  45. }
  46.  
  47. public void Load(int slot)
  48. {
  49. string path = specialPath + "\\Slot" + slot.ToString();
  50. for (int i = 0; i < gameObjectTags.Length;)
  51. {
  52. string[] fileEntries = Directory.GetFiles(path + "\\" + gameObjectTags[i]);
  53. foreach (string fileName in fileEntries)
  54. {
  55. string lines = File.ReadAllText(fileEntries[i].ToString());
  56. string posx = ReadLine(lines, 1);
  57. string posy = ReadLine(lines, 2);
  58. string posz = ReadLine(lines, 3);
  59. string rotx = ReadLine(lines, 4);
  60. string roty = ReadLine(lines, 5);
  61. string rotz = ReadLine(lines, 6);
  62. string rotw = ReadLine(lines, 7);
  63. string scalex = ReadLine(lines, 8);
  64. string scaley = ReadLine(lines, 9);
  65. string scalez = ReadLine(lines, 10);
  66. Vector3 tempPos = new Vector3(float.Parse(posx), float.Parse(posy), float.Parse(posz));
  67. Quaternion tempRot = new Quaternion(float.Parse(rotx), float.Parse(roty), float.Parse(rotz), float.Parse(rotw));
  68. ObjectsToLoad[i].transform.localScale = new Vector3(float.Parse(scalex), float.Parse(scaley), float.Parse(scalez));
  69. if (!ObjectsToLoad[i].name.Contains(keyWord))
  70. {
  71. ObjectsToLoad[i].name += " " + keyWord + " ";
  72. }
  73. Instantiate(ObjectsToLoad[i], tempPos, tempRot);
  74. Debug.Log("Jogo carregado");
  75. }
  76. i++;
  77. }
  78. }
  79.  
  80. private static string ReadLine(string text, int lineNumber)
  81. {
  82. var reader = new StringReader(text);
  83.  
  84. string line;
  85. int currentLineNumber = 0;
  86.  
  87. do
  88. {
  89. currentLineNumber += 1;
  90. line = reader.ReadLine();
  91. }
  92. while (line != null && currentLineNumber < lineNumber);
  93.  
  94. return (currentLineNumber == lineNumber) ? line :
  95. string.Empty;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement