Looksky

Cascading Save File

Oct 23rd, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. /* Simple example of how to make a cascading save file in text.
  2.  * Meaning that instead of having one class that saves everything (translates all necessary information in text)
  3.  * you let every single class save it's own bit of information, making it easier to debug for an instance.
  4.  * The code was written for Unity3D (C#).
  5.  */
  6.  
  7. using UnityEngine;
  8. using System.Collections;
  9. using System.Text;
  10.  
  11. public class LevelPack : ScriptableObject
  12. { // a level pack is a scriptable object containing all the level prefabs that belongs to this particular level pack
  13.     [SerializeField]private string nameOfPack;
  14.     public string NameOfPack { get { return nameOfPack; } }
  15.  
  16.     [SerializeField]private Level[] levelsOfThisPack;
  17.     public Level[] LevelsOfThisPack { get { return levelsOfThisPack; } }
  18.  
  19.     public bool Unlocked = true;
  20.  
  21.     public string GetSaveData()
  22.     {
  23.         StringBuilder sb = new StringBuilder();
  24.  
  25.         int boolToInt = Unlocked == true ? 1 : 0;
  26.         sb.Append(boolToInt.ToString());
  27.  
  28.         int length = levelsOfThisPack.Length;
  29.         for(int i = 0 ; i < length; i++)
  30.         {
  31.             sb.Append("|");
  32.             Level level = levelsOfThisPack[i];
  33.             sb.Append(i);
  34.             sb.Append("=");
  35.             sb.Append(level.GetSaveData());
  36.         }
  37.  
  38.         return sb.ToString();
  39.     }
  40.  
  41.     public void SetSaveData(string saveString)
  42.     {
  43.         string[] stringArray = saveString.Split('|');
  44.  
  45.         int length1 = stringArray.Length;
  46.         if(length1 >= 1)
  47.         {
  48.             int unlocked;
  49.             if(int.TryParse(stringArray[0], out unlocked))
  50.             {
  51.                 if(unlocked == 0)
  52.                 {
  53.                     Unlocked = false;
  54.                 }
  55.                 else
  56.                 {
  57.                     Unlocked = true;
  58.                 }
  59.             }
  60.             int levelLength = levelsOfThisPack.Length;
  61.  
  62.             for(int i = 1; i < length1; i++)
  63.             {
  64.                 string[] stringArray2 = stringArray[i].Split('=');
  65.                 int length2 = stringArray2.Length;
  66.                 if(length2 == 2)
  67.                 {
  68.                     int index;
  69.                     if(int.TryParse(stringArray2[0], out index))
  70.                     {
  71.                         if(index >= 0 && index < levelLength)
  72.                         {
  73.                             levelsOfThisPack[index].SetData(stringArray2[1]);
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. using UnityEngine;
  83. using System.Collections;
  84. using System.Text;
  85.  
  86. public class Level : MonoBehaviour
  87. { // a monobehaviour attached to the level prefab (in this case: the prefab usually consists only out of two transforms, that indicate begin and end)
  88.     public bool Unlocked = false;
  89.     public bool Completed = false;
  90.  
  91.     public int Score = 0;
  92.  
  93.     public string GetSaveData()
  94.     {
  95.         StringBuilder sb = new StringBuilder();
  96.  
  97.         int boolToInt = Unlocked == true ? 1 : 0;
  98.         sb.Append(boolToInt.ToString());
  99.  
  100.         sb.Append("_");
  101.  
  102.         boolToInt = Completed == true ? 1 : 0;
  103.         sb.Append(boolToInt.ToString());
  104.  
  105.         sb.Append("_");
  106.  
  107.         sb.Append(Score.ToString());
  108.  
  109.         return sb.ToString();
  110.     }
  111.  
  112.     public void SetData(string saveString)
  113.     {
  114.         string[] stringArray = saveString.Split('_');
  115.  
  116.         if(stringArray.Length == 3)
  117.         {
  118.             int unlocked;
  119.             if(int.TryParse(stringArray[0], out unlocked))
  120.             {
  121.                 if(unlocked == 0)
  122.                 {
  123.                     Unlocked = false;
  124.                 }
  125.                 else
  126.                 {
  127.                     Unlocked = true;
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 Unlocked = false;
  133.             }
  134.  
  135.             if(int.TryParse(stringArray[1], out unlocked))
  136.             {
  137.                 if(unlocked == 0)
  138.                 {
  139.                     Completed = false;
  140.                 }
  141.                 else
  142.                 {
  143.                     Completed = true;
  144.                 }
  145.             }
  146.             else
  147.             {
  148.                 Completed = false;
  149.             }
  150.  
  151.             if(!int.TryParse(stringArray[2], out Score))
  152.             {
  153.                 Score = 0;
  154.             }
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment