Skip to content

Commit 5df4ca8

Browse files
committed
final work for article
1 parent 115498d commit 5df4ca8

46 files changed

Lines changed: 3548 additions & 805 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ sysinfo.txt
2828
# Builds
2929
*.apk
3030
*.unitypackage
31+
Assets/saves/
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
165 KB
Binary file not shown.

Assets/1_PlayerPrefs_Example/Audio/Space Cadet.ogg.meta

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using UnityEngine;
2+
3+
namespace EX1
4+
{
5+
/// <summary>
6+
/// Singleton - There should only ever be one DataService and it should persist
7+
/// between scene loads.
8+
/// This class is responsible for loading/saving data.
9+
/// </summary>
10+
public class DataService : MonoBehaviour
11+
{
12+
private static DataService _instance = null;
13+
public static DataService Instance
14+
{
15+
get
16+
{
17+
// If the instance of this class doesn't exist
18+
if (_instance == null)
19+
{
20+
// Check the scene for a Game Object with this class
21+
_instance = FindObjectOfType<DataService>();
22+
23+
// If none is found in the scene then create a new Game Object
24+
// and add this class to it.
25+
if (_instance == null)
26+
{
27+
GameObject go = new GameObject(typeof(DataService).ToString());
28+
_instance = go.AddComponent<DataService>();
29+
}
30+
}
31+
32+
return _instance;
33+
}
34+
}
35+
36+
public PlayerPrefsHandler prefs { get; private set; }
37+
38+
// When the scene first runs ensure that there is only one
39+
// instance of this class. This allows us to add it to any scene and
40+
// not conflict with any pre-existing instance from a previous scene.
41+
private void Awake()
42+
{
43+
if (Instance != this)
44+
{
45+
Destroy(this);
46+
}
47+
else
48+
{
49+
DontDestroyOnLoad(gameObject);
50+
51+
prefs = new PlayerPrefsHandler();
52+
prefs.RestorePreferences();
53+
// In Unity 5.4 OnLevelWasLoaded has been deprecated and the action
54+
// now occurs through this callback.
55+
#if UNITY_5_4_OR_NEWER
56+
SceneManager.sceneLoaded += OnLevelWasLoaded;
57+
#endif
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Ensure that the player preferences are applied to the new scene.
63+
/// </summary>
64+
// In Unity 5.4 OnLevelWasLoaded has been deprecated and the action
65+
// now occurs through 'SceneManager.sceneLoaded' callback.
66+
void OnLevelWasLoaded()
67+
{
68+
prefs.RestorePreferences();
69+
}
70+
}
71+
}

Assets/_Scripts/Editor/NewBehaviourScript.cs.meta renamed to Assets/1_PlayerPrefs_Example/DataService.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Assets/_Scripts/Editor/ClearPlayerPrefs.cs.meta renamed to Assets/1_PlayerPrefs_Example/Editor/ClearPlayerPrefs.cs.meta

File renamed without changes.

0 commit comments

Comments
 (0)