-
-
Notifications
You must be signed in to change notification settings - Fork 1
Examples
Hasan Bayat edited this page May 16, 2017
·
2 revisions
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace SaveGamePro.Examples
{
/// <summary>
/// Example - 01
/// Saving and Loading Cube Position.
/// </summary>
public class Example01 : MonoBehaviour
{
/// <summary>
/// The cube.
/// </summary>
[Tooltip ( "The cube we want to save it's position" )]
public Transform cube;
/// <summary>
/// The cube default position.
/// </summary>
[Tooltip ( "The cube default position. used when the user does not have any saved position." )]
public Transform defaultPosition;
/// <summary>
/// The toggle.
/// </summary>
[Tooltip ( "The save on destroy boolean toggle." )]
public Toggle toggle;
/// <summary>
/// The save on destroy boolean.
/// </summary>
[HideInInspector]
public bool saveOnDestroy = false;
/// <summary>
/// The settings.
/// We using it to give all our save/load functionally similar settings.
/// </summary>
protected SaveGameSettings settings;
/// <summary>
/// Initialization.
/// </summary>
void Awake ()
{
// Creating our settings. using file save location. (We check for the platform if the file save location available we use that, otherwise we use PlayerPrefs or Resources)
settings = new SaveGameSettings ( SaveGameLocation.File );
// Loading Cube position on awake. Uncomment it if you want to load cube position on start.
// LoadCubePosition ();
}
/// <summary>
/// Called when object enabled in hierarchy.
/// </summary>
void OnEnable ()
{
// Load save on destroy boolean.
saveOnDestroy = SaveGame.LoadIfExists<bool> ( "saveondestroy", settings );
// Set the toggle value to boolean.
toggle.isOn = saveOnDestroy;
}
/// <summary>
/// Called when object destroyed.
/// </summary>
void OnDestroy ()
{
// Whether user wants to save it's information in destroy or not.
if ( saveOnDestroy )
{
// Save Cube position if user wants to save it on destroy.
SaveCubePosition ();
}
// Save on destroy boolean.
SaveGame.Save<bool> ( saveOnDestroy, "saveondestroy", settings );
}
/// <summary>
/// Saves the cube position.
/// </summary>
public void SaveCubePosition ()
{
// Saving Cube Transform, but you can only save cube position using cube.transform.position instead of cube.transform and load it back using Load<Vector3>
SaveGame.Save<Transform> ( cube.transform, "cubePosition", settings );
}
/// <summary>
/// Loads the cube position.
/// </summary>
public void LoadCubePosition ()
{
// Check for cube saved position existent.
if ( SaveGame.Exists ( "cubePosition" ) )
{
// Load the cube position.
SaveGame.Load<Transform> ( "cubePosition", cube.transform );
}
else
{
// Giving default position to cube if we don't have any saved position.
cube.transform.position = defaultPosition.position;
}
}
/// <summary>
/// Called when Toggle value changed.
/// </summary>
/// <param name="value">If set to <c>true</c> value.</param>
public void OnToggleValueChanged ( bool value )
{
saveOnDestroy = value;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace SaveGamePro.Examples
{
/// <summary>
/// Example - 02
/// Using folders.
/// </summary>
public class Example02 : MonoBehaviour
{
/// <summary>
/// The save on score changed toggle.
/// </summary>
public Toggle saveOnScoreChangedToggle;
/// <summary>
/// The score text.
/// </summary>
public Text scoreText;
/// <summary>
/// The score to add.
/// </summary>
public InputField inputField;
/// <summary>
/// How much score to add each time AddScore called.
/// </summary>
public int scoreToAdd = 1;
/// <summary>
/// The score.
/// </summary>
protected int score;
/// <summary>
/// Initialization.
/// </summary>
void Awake ()
{
// Uncomment it, if you want to load score at start.
// LoadScore ();
}
/// <summary>
/// Called when object enabled in hierarchy.
/// </summary>
void OnEnable ()
{
scoreToAdd = SaveGame.LoadIfExists<int> ( "configurations/scoreToAdd" );
inputField.text = scoreToAdd.ToString ();
}
/// <summary>
/// Called once when object destroyed.
/// </summary>
void OnDestroy ()
{
// You can use any name casing that you like. but keep it clean for when you want to load it back.
SaveGame.Save<int> ( scoreToAdd, "configurations/scoreToAdd" );
}
/// <summary>
/// Adds the score.
/// </summary>
public void AddScore ()
{
score += scoreToAdd;
scoreText.text = "Score: " + score.ToString ();
}
/// <summary>
/// Saves the score.
/// </summary>
public void SaveScore ()
{
SaveGame.Save<int> ( score, "score" );
}
/// <summary>
/// Loads the score.
/// </summary>
public void LoadScore ()
{
score = SaveGame.LoadIfExists<int> ( "score" );
scoreText.text = "Score: " + score.ToString ();
}
/// <summary>
/// Called when input field value changed.
/// </summary>
/// <param name="text">Text.</param>
public void OnInputFieldValueChanged ( string text )
{
int value = 1;
// Check if the text is not empty.
if ( !string.IsNullOrEmpty ( text ) )
{
try
{
value = int.Parse ( text );
}
catch ( System.Exception )
{
Debug.LogWarning ( "SaveGamePro: Provide a valid number not a text. we using 1 for score to add value." );
}
}
else
{
Debug.LogWarning ( "SaveGamePro: Please Fill the form, don't keep it empty. we using 1 for score to add value." );
}
scoreToAdd = value;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SaveGamePro.Examples
{
/// <summary>
/// Example - 03
/// Saving an Array.
/// </summary>
public class Example03 : MonoBehaviour
{
/// <summary>
/// The transforms.
/// </summary>
[Tooltip ( "Specify the transforms that you want to save." )]
public Transform [] transforms;
/// <summary>
/// Saves the transforms.
/// </summary>
public void Save ()
{
SaveGame.Save<Transform> ( transforms, "transforms" );
}
/// <summary>
/// Loads the transforms.
/// </summary>
public void Load ()
{
SaveGame.Load1DArray<Transform> ( "transforms", transforms );
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace SaveGamePro.Examples
{
/// <summary>
/// Example - 04
/// Saving a Dictionary.
/// </summary>
public class Example04 : MonoBehaviour
{
/// <summary>
/// The achievements container.
/// </summary>
public Transform achievementsContainer;
/// <summary>
/// The achievement text prefab.
/// </summary>
public Text achievementTextPrefab;
/// <summary>
/// The achievements.
/// </summary>
public static Dictionary<string, bool> achievements = new Dictionary<string, bool> () { {
"Achievement 1",
false
}, {
"Achievement 2",
false
}, {
"Achievement 3",
false
}
};
/// <summary>
/// Initialization.
/// </summary>
void Awake ()
{
SetupAchievements ();
}
/// <summary>
/// Unlocks the achievement.
/// </summary>
/// <param name="key">Key.</param>
public void UnlockAchievement ( string key )
{
achievements [ key ] = !achievements [ key ];
SetupAchievements ();
}
/// <summary>
/// Saves data.
/// </summary>
public void Save ()
{
SaveGame.Save<string, bool> ( achievements, "achievements" );
}
/// <summary>
/// Loads data.
/// </summary>
public void Load ()
{
achievements = SaveGame.LoadDictionary<string, bool> ( "achievements" );
SetupAchievements ();
}
/// <summary>
/// Setups the achievements.
/// </summary>
public void SetupAchievements ()
{
ClearAchievements ();
foreach ( KeyValuePair<string, bool> achievement in achievements )
{
Text text = Instantiate<Text> ( achievementTextPrefab, achievementsContainer );
string unlocked = achievement.Value == true ? "Unlocked" : "Locked";
text.text = string.Format ( "{0}: {1}", achievement.Key, achievement.Value );
}
}
/// <summary>
/// Clears the achievements.
/// </summary>
public void ClearAchievements ()
{
for ( int i = 0; i < achievementsContainer.childCount; i++ )
{
Transform child = achievementsContainer.GetChild ( i );
Destroy ( child.gameObject );
}
}
}
}
If you need a specified example send it to us to create an example for that immediately.