Advertisement
CassataGames

Untitled

Dec 9th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.IO;
  7.  
  8. public class E_FileScript : MonoBehaviour {
  9.  
  10.     public static E_FileScript Access;
  11.     public EditorGlobals GlobalAccess;
  12.  
  13.     public bool TestSave;
  14.     public bool TestLoad;
  15.     public string TestLoadFilename;
  16.  
  17.     public LevelData Level;
  18.  
  19.     #region Convert Vector3 to Floats
  20.     /// <summary>
  21.     /// Which value of Vector3 is being returned.
  22.     /// </summary>
  23.     public enum xyz { X, Y, Z };
  24.     /// <summary>
  25.     /// Convert X, Y, or Z of a Vector3 to a float.
  26.     /// </summary>
  27.     /// <param name="In">Vector3 that is being converted.</param>
  28.     /// <param name="Returned">X, Y, Z of Vector3 being converted.</param>
  29.     /// <remarks></remarks>
  30.     public static float Vector3_To_Float(Vector3 In, xyz Returned)
  31.     {
  32.         switch (Returned)
  33.         {
  34.             case xyz.X:
  35.                 return In.x;
  36.             case xyz.Y:
  37.                 return In.y;
  38.             case xyz.Z:
  39.                 return In.z;
  40.             default:
  41.                 Debug.LogError("Invalid Convertion in Vector3");
  42.                 return 0;
  43.         }
  44.     }
  45.     #endregion
  46.    
  47.     void Start ()
  48.     {
  49.         GlobalAccess = gameObject.GetComponent<EditorGlobals>();
  50.     }
  51.  
  52.     void Update ()
  53.     {  
  54.         // Test new save system
  55.         if (TestSave)
  56.         {
  57.             TestSave = false;
  58.             SaveLevel(GlobalAccess.LevelName, GlobalAccess.Creator, GlobalAccess.FileName, GlobalAccess.FileSystemVersion, false);
  59.         }
  60.     }
  61.  
  62.  
  63.     /// <summary>
  64.     /// Save the level.
  65.     /// </summary>
  66.     /// <param name="LevelName">Level Name.</param>
  67.     /// <param name="LevelAuthor">Level Creator.</param>
  68.     /// <param name="FileName">File system being used.</param>
  69.     /// <param name="IsOfficial">Is the level official?</param>
  70.     /// <remarks></remarks>
  71.     public void SaveLevel(string LevelName, string LevelAuthor, string FileName, int FileSystem, bool IsOfficial)
  72.     {
  73.         // Generates a list of all the objects in the current level.
  74.         GameObject[] ObjectList = GameObject.FindGameObjectsWithTag("EditorPiece");
  75.         // All the level data in a single class.
  76.         Level = new LevelData(LevelName, LevelAuthor, FileSystem, ObjectList, IsOfficial);
  77.  
  78.         // New Formatter
  79.         BinaryFormatter formatter = new BinaryFormatter();
  80.         // Create or replace the file.
  81.         FileStream file = File.Create(Application.dataPath + "/Levels/" + FileName + ".dlvl");
  82.         // Place information into file.
  83.         formatter.Serialize(file, Level);
  84.         // Close the file.
  85.         file.Close();
  86.         // Report that this function completed successfully.
  87.         Debug.Log(LevelName + " was successfully saved: " + Application.dataPath + "/Levels/" + FileName + ".dlvl");
  88.     }
  89. }
  90.  
  91. [Serializable]
  92. public class LevelData
  93. {
  94.     public string LevelName;
  95.     public string LevelAuthor;
  96.     public int FileVersion;
  97.     public List<I_Object> I_ObjectList;
  98.     public bool IsOfficial;
  99.     /// <summary>
  100.     /// Collect level data to be saved.
  101.     /// No information is saved yet.
  102.     /// </summary>
  103.     /// <param name="name">Name of the level.</param>
  104.     /// <param name="author">Creator of the level.</param>
  105.     /// <param name="FileSystem">File system being used.</param>
  106.     /// <param name="ObjectList">All the objects with the tag "EditorPiece"</param>
  107.     /// <param name="OfficialLevel">Is this level official?</param>
  108.     public LevelData(string name, string author, int FileSystem, GameObject[] ObjectList, bool OfficialLevel)
  109.     {
  110.         LevelName = name;
  111.         LevelAuthor = author;
  112.         FileVersion = FileSystem;
  113.         IsOfficial = OfficialLevel;
  114.  
  115.         I_ObjectList = new List<I_Object>();
  116.  
  117.         foreach (GameObject Individual in ObjectList)
  118.         {
  119.             E_ComponentControl I_CC = Individual.GetComponent<E_ComponentControl>();
  120.             I_Object Object = new I_Object();
  121.  
  122.             // Apply all information to this object
  123.             Object.Name = I_CC.componentName;
  124.             Object.Color = I_CC.componentColor;
  125.             Object.Orientation = I_CC.componentOrientation;
  126.             Object.posX = E_FileScript.Vector3_To_Float(I_CC.componentPosition, E_FileScript.xyz.X);
  127.             Object.posY = E_FileScript.Vector3_To_Float(I_CC.componentPosition, E_FileScript.xyz.Y);
  128.             Object.posZ = E_FileScript.Vector3_To_Float(I_CC.componentPosition, E_FileScript.xyz.Z);
  129.             Object.Size = I_CC.componentSize;
  130.             Object.StartPowered = I_CC.componentStartPowered;
  131.  
  132.             I_ObjectList.Add(Object);
  133.         }
  134.     }
  135. }
  136.  
  137. [Serializable]
  138. public class I_Object
  139. {
  140.     public string Name;
  141.     public ComponentList.Components Type;
  142.     public EditorGlobals.EditorColor Color;
  143.     public EditorGlobals.Orientation Orientation;
  144.     public float posX;
  145.     public float posY;
  146.     public float posZ;
  147.     public int Size;
  148.     public bool StartPowered;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement