@@ -7,64 +7,90 @@ namespace Data {
7
7
8
8
public class DataController : MonoBehaviour
9
9
{
10
- public static readonly string DATA_SCORE = "score" ;
11
- public static readonly string DATA_HIGHSCORE = "highscore" ;
12
-
10
+ private static readonly string DATA_SCORE = "score" ;
11
+ private static readonly string DATA_HIGHSCORE = "highscore" ;
13
12
private static readonly int DEFAULT_INT = 0 ;
14
- private static readonly float DEFAULT_FLOAT = 0.0F ;
15
- private static readonly string DEFAULT_STRING = "" ;
16
13
17
14
public bool debug ;
18
15
19
- #region Public Functions
20
- public T GetData < T > ( string _data , bool _createIfNull = true ) {
21
- if ( ! DataExists ( _data ) ) {
22
- string _warning = "Data [" + _data + "] does not exist." ;
23
- if ( _createIfNull ) {
24
- _warning += "Creating.." ;
25
- LogWarning ( _warning ) ;
26
- } else {
27
- _warning += "Returning default.." ;
28
- LogWarning ( _warning ) ;
29
- return default ( T ) ;
30
- }
16
+ #region Properties
17
+ public int Highscore {
18
+ get {
19
+ return GetInt ( DATA_HIGHSCORE ) ;
20
+ }
21
+ private set {
22
+ SaveInt ( DATA_HIGHSCORE , value ) ;
31
23
}
24
+ }
32
25
33
- if ( typeof ( T ) == typeof ( int ) ) {
34
- return PlayerPrefs . GetInt ( _data , DEFAULT_INT ) ;
35
- } else if ( typeof ( T ) == typeof ( float ) ) {
36
- return PlayerPrefs . GetFloat ( _data , DEFAULT_FLOAT ) ;
37
- } else if ( typeof ( T ) == typeof ( string ) ) {
38
- return PlayerPrefs . GetString ( _data , DEFAULT_STRING ) ;
26
+ public int Score {
27
+ get {
28
+ return GetInt ( DATA_SCORE ) ;
29
+ }
30
+ set {
31
+ SaveInt ( DATA_SCORE , value ) ;
32
+ int _score = this . Score ;
33
+ if ( _score > this . Highscore ) {
34
+ this . Highscore = _score ;
35
+ }
39
36
}
40
-
41
- return default ( T ) ;
42
37
}
38
+ #endregion
39
+
40
+ #region Unity Functions
41
+ #if UNITY_EDITOR
42
+ private void Update ( ) {
43
+ if ( Input . GetKeyUp ( KeyCode . R ) ) {
44
+ TestAddScore ( 1 ) ;
45
+ Log ( "[Test] Score = " + this . Score + " | Highscore = " + this . Highscore ) ;
46
+ }
47
+
48
+ if ( Input . GetKeyUp ( KeyCode . T ) ) {
49
+ TestAddScore ( - 1 ) ;
50
+ Log ( "[Test] Score = " + this . Score + " | Highscore = " + this . Highscore ) ;
51
+ }
43
52
44
- public void SaveData < T > ( string _data , T _val ) {
45
- if ( typeof ( T ) == typeof ( int ) ) {
46
- return PlayerPrefs . SetInt ( _data , _val ) ;
47
- } else if ( typeof ( T ) == typeof ( float ) ) {
48
- return PlayerPrefs . SetFloat ( _data , _val ) ;
49
- } else if ( typeof ( T ) == typeof ( string ) ) {
50
- return PlayerPrefs . SetString ( _data , _val ) ;
53
+ if ( Input . GetKeyUp ( KeyCode . Space ) ) {
54
+ TestResetHighscore ( ) ;
55
+ TestResetScore ( ) ;
56
+ Log ( "[Test] Score = " + this . Score + " | Highscore = " + this . Highscore ) ;
51
57
}
52
58
}
53
59
#endif
60
+ #endregion
54
61
55
62
#region Private Functions
56
- private bool DataExists ( string _data ) {
57
- return PlayerPrefs . HasKey ( _data ) ;
63
+ private void SaveInt ( string _data , int _value ) {
64
+ PlayerPrefs . SetInt ( _data , _value ) ;
65
+ }
66
+
67
+ private int GetInt ( string _data ) {
68
+ return PlayerPrefs . GetInt ( _data , DEFAULT_INT ) ;
69
+ }
70
+
71
+ private void Log ( string _msg ) {
72
+ if ( ! debug ) return ;
73
+ Debug . Log ( "[DataController]: " + _msg ) ;
58
74
}
59
75
60
76
private void LogWarning ( string _msg ) {
61
77
if ( ! debug ) return ;
62
78
Debug . LogWarning ( "[DataController]: " + _msg ) ;
63
79
}
64
- #endif
80
+ #endregion
65
81
66
82
#region Tests
83
+ private void TestAddScore ( int _delta ) {
84
+ this . Score += _delta ;
85
+ }
67
86
87
+ private void TestResetScore ( ) {
88
+ this . Score = 0 ;
89
+ }
90
+
91
+ private void TestResetHighscore ( ) {
92
+ this . Highscore = 0 ;
93
+ }
68
94
#endregion
69
95
}
70
96
}
0 commit comments