@@ -7,7 +7,65 @@ namespace Data {
77
88 public class DataController : MonoBehaviour
99 {
10-
10+ public static readonly string DATA_SCORE = "score" ;
11+ public static readonly string DATA_HIGHSCORE = "highscore" ;
12+
13+ private static readonly int DEFAULT_INT = 0 ;
14+ private static readonly float DEFAULT_FLOAT = 0.0F ;
15+ private static readonly string DEFAULT_STRING = "" ;
16+
17+ public bool debug ;
18+
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+ }
31+ }
32+
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 ) ;
39+ }
40+
41+ return default ( T ) ;
42+ }
43+
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 ) ;
51+ }
52+ }
53+ #endif
54+
55+ #region Private Functions
56+ private bool DataExists ( string _data ) {
57+ return PlayerPrefs . HasKey ( _data ) ;
58+ }
59+
60+ private void LogWarning ( string _msg ) {
61+ if ( ! debug ) return ;
62+ Debug . LogWarning ( "[DataController]: " + _msg ) ;
63+ }
64+ #endif
65+
66+ #region Tests
67+
68+ #endregion
1169 }
1270 }
1371}
0 commit comments