Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using System.Text;
- using System;
- public class LoadGameObjects : MonoBehaviour
- {
- SaveGameObjects SGO;
- string specialPath = "";
- GameObject[] ObjectsToLoad;
- String[] gameObjectTags;
- string keyWord = "";
- void Start()
- {
- SGO = FindObjectOfType(typeof(SaveGameObjects)) as SaveGameObjects;
- specialPath = SGO.specialPath;
- ObjectsToLoad = SGO.gameObjectPrefabs;
- gameObjectTags = SGO.gameObjectIDs;
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.L))
- {
- Load(1);
- }
- }
- public void RefreshGameObjects(int slot)
- {
- //Delete All Current Loaded Objects
- object[] allObjects = FindObjectsOfType(typeof(GameObject));
- foreach (GameObject obj in allObjects)
- {
- if (obj.name.Contains(keyWord))
- {
- GameObject.Destroy(obj);
- }
- }
- Load(slot);
- }
- public void Load(int slot)
- {
- string path = specialPath + "\\Slot" + slot.ToString();
- for (int i = 0; i < gameObjectTags.Length;)
- {
- string[] fileEntries = Directory.GetFiles(path + "\\" + gameObjectTags[i]);
- foreach (string fileName in fileEntries)
- {
- string lines = File.ReadAllText(fileEntries[i].ToString());
- string posx = ReadLine(lines, 1);
- string posy = ReadLine(lines, 2);
- string posz = ReadLine(lines, 3);
- string rotx = ReadLine(lines, 4);
- string roty = ReadLine(lines, 5);
- string rotz = ReadLine(lines, 6);
- string rotw = ReadLine(lines, 7);
- string scalex = ReadLine(lines, 8);
- string scaley = ReadLine(lines, 9);
- string scalez = ReadLine(lines, 10);
- Vector3 tempPos = new Vector3(float.Parse(posx), float.Parse(posy), float.Parse(posz));
- Quaternion tempRot = new Quaternion(float.Parse(rotx), float.Parse(roty), float.Parse(rotz), float.Parse(rotw));
- ObjectsToLoad[i].transform.localScale = new Vector3(float.Parse(scalex), float.Parse(scaley), float.Parse(scalez));
- if (!ObjectsToLoad[i].name.Contains(keyWord))
- {
- ObjectsToLoad[i].name += " " + keyWord + " ";
- }
- Instantiate(ObjectsToLoad[i], tempPos, tempRot);
- Debug.Log("Jogo carregado");
- }
- i++;
- }
- }
- private static string ReadLine(string text, int lineNumber)
- {
- var reader = new StringReader(text);
- string line;
- int currentLineNumber = 0;
- do
- {
- currentLineNumber += 1;
- line = reader.ReadLine();
- }
- while (line != null && currentLineNumber < lineNumber);
- return (currentLineNumber == lineNumber) ? line :
- string.Empty;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement