Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- public class E_FileScript : MonoBehaviour {
- public static E_FileScript Access;
- public EditorGlobals GlobalAccess;
- public bool TestSave;
- public bool TestLoad;
- public string TestLoadFilename;
- public LevelData Level;
- #region Convert Vector3 to Floats
- /// <summary>
- /// Which value of Vector3 is being returned.
- /// </summary>
- public enum xyz { X, Y, Z };
- /// <summary>
- /// Convert X, Y, or Z of a Vector3 to a float.
- /// </summary>
- /// <param name="In">Vector3 that is being converted.</param>
- /// <param name="Returned">X, Y, Z of Vector3 being converted.</param>
- /// <remarks></remarks>
- public static float Vector3_To_Float(Vector3 In, xyz Returned)
- {
- switch (Returned)
- {
- case xyz.X:
- return In.x;
- case xyz.Y:
- return In.y;
- case xyz.Z:
- return In.z;
- default:
- Debug.LogError("Invalid Convertion in Vector3");
- return 0;
- }
- }
- #endregion
- void Start ()
- {
- GlobalAccess = gameObject.GetComponent<EditorGlobals>();
- }
- void Update ()
- {
- // Test new save system
- if (TestSave)
- {
- TestSave = false;
- SaveLevel(GlobalAccess.LevelName, GlobalAccess.Creator, GlobalAccess.FileName, GlobalAccess.FileSystemVersion, false);
- }
- }
- /// <summary>
- /// Save the level.
- /// </summary>
- /// <param name="LevelName">Level Name.</param>
- /// <param name="LevelAuthor">Level Creator.</param>
- /// <param name="FileName">File system being used.</param>
- /// <param name="IsOfficial">Is the level official?</param>
- /// <remarks></remarks>
- public void SaveLevel(string LevelName, string LevelAuthor, string FileName, int FileSystem, bool IsOfficial)
- {
- // Generates a list of all the objects in the current level.
- GameObject[] ObjectList = GameObject.FindGameObjectsWithTag("EditorPiece");
- // All the level data in a single class.
- Level = new LevelData(LevelName, LevelAuthor, FileSystem, ObjectList, IsOfficial);
- // New Formatter
- BinaryFormatter formatter = new BinaryFormatter();
- // Create or replace the file.
- FileStream file = File.Create(Application.dataPath + "/Levels/" + FileName + ".dlvl");
- // Place information into file.
- formatter.Serialize(file, Level);
- // Close the file.
- file.Close();
- // Report that this function completed successfully.
- Debug.Log(LevelName + " was successfully saved: " + Application.dataPath + "/Levels/" + FileName + ".dlvl");
- }
- }
- [Serializable]
- public class LevelData
- {
- public string LevelName;
- public string LevelAuthor;
- public int FileVersion;
- public List<I_Object> I_ObjectList;
- public bool IsOfficial;
- /// <summary>
- /// Collect level data to be saved.
- /// No information is saved yet.
- /// </summary>
- /// <param name="name">Name of the level.</param>
- /// <param name="author">Creator of the level.</param>
- /// <param name="FileSystem">File system being used.</param>
- /// <param name="ObjectList">All the objects with the tag "EditorPiece"</param>
- /// <param name="OfficialLevel">Is this level official?</param>
- public LevelData(string name, string author, int FileSystem, GameObject[] ObjectList, bool OfficialLevel)
- {
- LevelName = name;
- LevelAuthor = author;
- FileVersion = FileSystem;
- IsOfficial = OfficialLevel;
- I_ObjectList = new List<I_Object>();
- foreach (GameObject Individual in ObjectList)
- {
- E_ComponentControl I_CC = Individual.GetComponent<E_ComponentControl>();
- I_Object Object = new I_Object();
- // Apply all information to this object
- Object.Name = I_CC.componentName;
- Object.Color = I_CC.componentColor;
- Object.Orientation = I_CC.componentOrientation;
- Object.posX = E_FileScript.Vector3_To_Float(I_CC.componentPosition, E_FileScript.xyz.X);
- Object.posY = E_FileScript.Vector3_To_Float(I_CC.componentPosition, E_FileScript.xyz.Y);
- Object.posZ = E_FileScript.Vector3_To_Float(I_CC.componentPosition, E_FileScript.xyz.Z);
- Object.Size = I_CC.componentSize;
- Object.StartPowered = I_CC.componentStartPowered;
- I_ObjectList.Add(Object);
- }
- }
- }
- [Serializable]
- public class I_Object
- {
- public string Name;
- public ComponentList.Components Type;
- public EditorGlobals.EditorColor Color;
- public EditorGlobals.Orientation Orientation;
- public float posX;
- public float posY;
- public float posZ;
- public int Size;
- public bool StartPowered;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement