Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Simple example of how to make a cascading save file in text.
- * Meaning that instead of having one class that saves everything (translates all necessary information in text)
- * you let every single class save it's own bit of information, making it easier to debug for an instance.
- * The code was written for Unity3D (C#).
- */
- using UnityEngine;
- using System.Collections;
- using System.Text;
- public class LevelPack : ScriptableObject
- { // a level pack is a scriptable object containing all the level prefabs that belongs to this particular level pack
- [SerializeField]private string nameOfPack;
- public string NameOfPack { get { return nameOfPack; } }
- [SerializeField]private Level[] levelsOfThisPack;
- public Level[] LevelsOfThisPack { get { return levelsOfThisPack; } }
- public bool Unlocked = true;
- public string GetSaveData()
- {
- StringBuilder sb = new StringBuilder();
- int boolToInt = Unlocked == true ? 1 : 0;
- sb.Append(boolToInt.ToString());
- int length = levelsOfThisPack.Length;
- for(int i = 0 ; i < length; i++)
- {
- sb.Append("|");
- Level level = levelsOfThisPack[i];
- sb.Append(i);
- sb.Append("=");
- sb.Append(level.GetSaveData());
- }
- return sb.ToString();
- }
- public void SetSaveData(string saveString)
- {
- string[] stringArray = saveString.Split('|');
- int length1 = stringArray.Length;
- if(length1 >= 1)
- {
- int unlocked;
- if(int.TryParse(stringArray[0], out unlocked))
- {
- if(unlocked == 0)
- {
- Unlocked = false;
- }
- else
- {
- Unlocked = true;
- }
- }
- int levelLength = levelsOfThisPack.Length;
- for(int i = 1; i < length1; i++)
- {
- string[] stringArray2 = stringArray[i].Split('=');
- int length2 = stringArray2.Length;
- if(length2 == 2)
- {
- int index;
- if(int.TryParse(stringArray2[0], out index))
- {
- if(index >= 0 && index < levelLength)
- {
- levelsOfThisPack[index].SetData(stringArray2[1]);
- }
- }
- }
- }
- }
- }
- }
- using UnityEngine;
- using System.Collections;
- using System.Text;
- public class Level : MonoBehaviour
- { // a monobehaviour attached to the level prefab (in this case: the prefab usually consists only out of two transforms, that indicate begin and end)
- public bool Unlocked = false;
- public bool Completed = false;
- public int Score = 0;
- public string GetSaveData()
- {
- StringBuilder sb = new StringBuilder();
- int boolToInt = Unlocked == true ? 1 : 0;
- sb.Append(boolToInt.ToString());
- sb.Append("_");
- boolToInt = Completed == true ? 1 : 0;
- sb.Append(boolToInt.ToString());
- sb.Append("_");
- sb.Append(Score.ToString());
- return sb.ToString();
- }
- public void SetData(string saveString)
- {
- string[] stringArray = saveString.Split('_');
- if(stringArray.Length == 3)
- {
- int unlocked;
- if(int.TryParse(stringArray[0], out unlocked))
- {
- if(unlocked == 0)
- {
- Unlocked = false;
- }
- else
- {
- Unlocked = true;
- }
- }
- else
- {
- Unlocked = false;
- }
- if(int.TryParse(stringArray[1], out unlocked))
- {
- if(unlocked == 0)
- {
- Completed = false;
- }
- else
- {
- Completed = true;
- }
- }
- else
- {
- Completed = false;
- }
- if(!int.TryParse(stringArray[2], out Score))
- {
- Score = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment