void Start () { mPostCountData = PrefsManager.Instance.GetPostCountData (); WWWClient wwwClient = new WWWClient (this, URL); wwwClient.OnSuccess = (WWW www) => { Dictionary<string,object> dic = (Dictionary<string,object>)Json.Deserialize (www.text); double unixTime = (double)dic ["st"]; DateTime dtNow = UNIX_EPOCH.AddSeconds ((long)unixTime).ToLocalTime (); if (string.IsNullOrEmpty (mPostCountData.PostDate)) { mPostCountData.PostDate = dtNow.ToString (); PrefsManager.Instance.SavePostCountData (mPostCountData); return; } DateTime dtPost = DateTime.Parse (mPostCountData.PostDate); TimeSpan ts = dtNow - dtPost; if (ts.Days >= 1) { // reset postData mPostCountData.PostCount = 0; mPostCountData.PostDate = dtNow.ToString (); PrefsManager.Instance.SavePostCountData (mPostCountData); } }; wwwClient.GetData (); }
public PostCountData GetPostCountData () { string json = PlayerPrefs.GetString (POST_COUNT_DATA); Debug.Log ("json = " + json); PostCountData postCountData = new PostCountData (); if (json == "") { postCountData.PostCount = 0; postCountData.PostDate = ""; return postCountData; } Dictionary<string,object> dictionary = (Dictionary<string,object>)Json.Deserialize (json); Debug.Log ("2"); long postCount = (long)dictionary [POST_COUNT_KEY]; Debug.Log ("3"); string postDate = dictionary [POST_DATE_KEY].ToString (); Debug.Log ("4"); postCountData.PostCount = (int)postCount; postCountData.PostDate = postDate; Debug.Log ("return"); return postCountData; }
public void SavePostCountData (PostCountData postCountData) { Dictionary<string,object> dictionary = new Dictionary<string, object> (); dictionary.Add (POST_DATE_KEY, postCountData.PostDate); dictionary.Add (POST_COUNT_KEY, postCountData.PostCount); string json = Json.Serialize (dictionary); Debug.Log ("json = " + json); PlayerPrefs.SetString (POST_COUNT_DATA, json); PlayerPrefs.Save (); }