{"action":"created","issue":{"url":"https://api.github.com/repos/playgameservices/play-games-plugin-for-unity/issues/409","labels_url":"https://api.github.com/repos/playgameservices/play-games-plugin-for-unity/issues/409/labels{/name}","comments_url":"https://api.github.com/repos/playgameservices/play-games-plugin-for-unity/issues/409/comments","events_url":"https://api.github.com/repos/playgameservices/play-games-plugin-for-unity/issues/409/events","html_url":"https://github.com/playgameservices/play-games-plugin-for-unity/issues/409","id":57526251,"number":409,"title":"Where I have to put the info to save?","user":{"login":"elsaq","id":10977078,"avatar_url":"https://avatars.githubusercontent.com/u/10977078?v=3","gravatar_id":"","url":"https://api.github.com/users/elsaq","html_url":"https://github.com/elsaq","followers_url":"https://api.github.com/users/elsaq/followers","following_url":"https://api.github.com/users/elsaq/following{/other_user}","gists_url":"https://api.github.com/users/elsaq/gists{/gist_id}","starred_url":"https://api.github.com/users/elsaq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elsaq/subscriptions","organizations_url":"https://api.github.com/users/elsaq/orgs","repos_url":"https://api.github.com/users/elsaq/repos","events_url":"https://api.github.com/users/elsaq/events{/privacy}","received_events_url":"https://api.github.com/users/elsaq/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-12T22:00:22Z","updated_at":"2015-02-13T04:27:29Z","closed_at":null,"body":"I´m using official plugin:\r\n\r\nhttps://github.com/playgameservices/play-games-plugin-for-unity\r\n\r\nI have this code for saving and loading:\r\n\r\n     System.Action<bool> mAuthCallback;\r\n     \r\n     GameData slot0;\r\n     \r\n     bool mSaving;\r\n     private Texture2D mScreenImage;\r\n     \r\n     // Use this for initialization\r\n     void Start () {\r\n         \r\n         slot0 = new GameData(\"New game\");\r\n         mAuthCallback = (bool success) => {\r\n             \r\n             if (success) {\r\n                 Debug.Log(\"Authentication was successful!\");\r\n                 slot0.State = \"Click load or save\";\r\n             }\r\n             else {\r\n                 Debug.LogWarning(\"Authentication failed!\");\r\n             }\r\n             \r\n         };\r\n         \r\n         \r\n         PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()\r\n             .EnableSavedGames()\r\n                 .Build();\r\n         PlayGamesPlatform.InitializeInstance(config);\r\n         \r\n         // Activate the Play Games platform. This will make it the default\r\n         // implementation of Social.Active\r\n         PlayGamesPlatform.Activate();\r\n         \r\n         // enable debug logs (note: we do this because this is a sample; on your production\r\n         // app, you probably don't want this turned on by default, as it will fill the user's\r\n         // logs with debug info).\r\n         PlayGamesPlatform.DebugLogEnabled = true;\r\n         \r\n         //Login explicitly for this sample, usually this would be silent\r\n         PlayGamesPlatform.Instance.Authenticate(mAuthCallback, false);\r\n         \r\n         \r\n     }\r\n     \r\n     public void CaptureScreenshot() {\r\n         mScreenImage = new Texture2D(Screen.width, Screen.height);\r\n         mScreenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);\r\n         mScreenImage.Apply();\r\n     }\r\n \r\n     // Save Button\r\n \r\n     public void SaveButton () \r\n     {\r\n         int idx = slot0.Data.IndexOf(\"_\");\r\n         if (idx > 0) {\r\n             int val = Convert.ToInt32(slot0.Data.Substring(idx+1));\r\n             val++;\r\n             slot0.Data = \"Save_\" + val;\r\n         }\r\n         else {\r\n             slot0.Data = \"Save_0\";\r\n         }\r\n         mSaving = true;\r\n         CaptureScreenshot();\r\n         ((PlayGamesPlatform)Social.Active).SavedGame.ShowSelectSavedGameUI(\"Save game progress\",\r\n                                                                            4,true,true,SavedGameSelected);\r\n     }\r\n \r\n     // Load Button\r\n \r\n     public void LoadButton () \r\n     {\r\n         mSaving = false;\r\n         ((PlayGamesPlatform)Social.Active).SavedGame.ShowSelectSavedGameUI(\"Select game to load\",\r\n                                                                            4,false,false,SavedGameSelected);\r\n     }\r\n     \r\n     \r\n     public void SavedGameSelected(SelectUIStatus status, ISavedGameMetadata game) {\r\n         \r\n         if (status == SelectUIStatus.SavedGameSelected) {\r\n             string filename = game.Filename;\r\n             Debug.Log(\"opening saved game:  \" + game);\r\n             if(mSaving && (filename == null || filename.Length == 0)) {\r\n                 filename = \"save\" + DateTime.Now.ToBinary();\r\n             }\r\n             if (mSaving) {\r\n                 slot0.State = \"Saving to \" + filename;\r\n             }\r\n             else {\r\n                 slot0.State = \"Loading from \" + filename;\r\n             }\r\n             //open the data.\r\n             ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(filename,\r\n                                                                                              DataSource.ReadCacheOrNetwork,\r\n                                                                                              ConflictResolutionStrategy.UseLongestPlaytime,\r\n                                                                                              SavedGameOpened);\r\n         } else {\r\n             Debug.LogWarning(\"Error selecting save game: \" + status);\r\n         }\r\n         \r\n     }\r\n     \r\n     public void SavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {\r\n         if(status == SavedGameRequestStatus.Success) {\r\n             if( mSaving) {\r\n                 slot0.State = \"Opened, now writing\";\r\n                 byte[] pngData = (mScreenImage!=null) ?mScreenImage.EncodeToPNG():null;\r\n                 Debug.Log(\"Saving to \" + game);\r\n                 byte[] data = slot0.ToBytes();\r\n                 TimeSpan playedTime = slot0.TotalPlayingTime;\r\n                 SavedGameMetadataUpdate.Builder builder =  new \r\n                     SavedGameMetadataUpdate.Builder()\r\n                         .WithUpdatedPlayedTime(playedTime)\r\n                         .WithUpdatedDescription(\"Saved Game at \" + DateTime.Now);\r\n                 \r\n                 if (pngData != null) {\r\n                     Debug.Log(\"Save image of len \" + pngData.Length);\r\n                     builder = builder.WithUpdatedPngCoverImage(pngData);\r\n                 }\r\n                 else {\r\n                     Debug.Log (\"No image avail\");\r\n                 }\r\n                 SavedGameMetadataUpdate updatedMetadata  = builder.Build();\r\n                 ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game,updatedMetadata,data,SavedGameWritten);\r\n             } else {\r\n                 slot0.State = \"Opened, reading...\";\r\n                 ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game,SavedGameLoaded);\r\n             }\r\n         } else {\r\n             Debug.LogWarning(\"Error opening game: \" + status);\r\n         }\r\n     }\r\n     \r\n     public void SavedGameLoaded(SavedGameRequestStatus status, byte[] data) {\r\n         if (status == SavedGameRequestStatus.Success) {\r\n             Debug.Log(\"SaveGameLoaded, success=\" + status);\r\n             slot0 = GameData.FromBytes(data);\r\n         } else {\r\n             Debug.LogWarning(\"Error reading game: \" + status);\r\n         }\r\n     }\r\n     \r\n     \r\n     public void SavedGameWritten(SavedGameRequestStatus status, ISavedGameMetadata game) {\r\n         if(status == SavedGameRequestStatus.Success) {\r\n             Debug.Log (\"Game \" + game.Description + \" written\");\r\n             slot0.State = \"Saved!\";\r\n         } else {\r\n             Debug.LogWarning(\"Error saving game: \" + status);\r\n         }\r\n     }\r\n     \r\n     \r\n     public class GameData  {\r\n         \r\n         private TimeSpan mPlayingTime;\r\n         private DateTime mLoadedTime;\r\n         string mData;\r\n         string mState;\r\n         \r\n         static readonly string HEADER = \"GDv1\";\r\n         \r\n         public GameData(string data) {\r\n             \r\n             mData = data;\r\n             mState = \"Initialized, modified\";\r\n             mPlayingTime = new TimeSpan();\r\n             mLoadedTime = DateTime.Now;\r\n             \r\n         }\r\n         \r\n         public TimeSpan TotalPlayingTime {\r\n             get {\r\n                 TimeSpan delta = DateTime.Now.Subtract(mLoadedTime);\r\n                 return mPlayingTime.Add(delta);\r\n             }\r\n         }\r\n         \r\n         public override string ToString () {\r\n             string s = HEADER + \":\" + mData;\r\n             s += \":\" + TotalPlayingTime.TotalMilliseconds;\r\n             return s;\r\n         }\r\n         \r\n         public byte[] ToBytes() {\r\n             return System.Text.ASCIIEncoding.Default.GetBytes(ToString());\r\n         }\r\n         \r\n         \r\n         public static GameData FromBytes (byte[] bytes) {\r\n             return FromString(System.Text.ASCIIEncoding.Default.GetString(bytes));\r\n         }\r\n         \r\n         public static GameData FromString (string s) {\r\n             GameData gd = new GameData(\"initializing from string\");\r\n             string[] p = s.Split(new char[] { ':' });\r\n             if (!p[0].StartsWith(HEADER)) {\r\n                 Debug.LogError(\"Failed to parse game data from: \" + s);\r\n                 return gd;\r\n             }\r\n             gd.mData = p[1];\r\n             double val = Double.Parse(p[2]);\r\n             gd.mPlayingTime = TimeSpan.FromMilliseconds(val>0f?val:0f);\r\n             \r\n             gd.mLoadedTime = DateTime.Now;\r\n             gd.mState = \"Loaded successfully\";\r\n             return gd;\r\n         }\r\n         \r\n         public string Data {\r\n             get {\r\n                 return mData;\r\n             }\r\n             \r\n             set {\r\n                 mData = value;\r\n                 mState += \", modified\";\r\n             }\r\n             \r\n         }\r\n         \r\n         public string State {\r\n             get {\r\n                 return mState;\r\n             }\r\n             \r\n             set {\r\n                 mState = value;\r\n             }\r\n         }\r\n         \r\n     }\r\n \r\nWith this I only save and load blank info, so nothing is saved, can anyone help me? I would like to know where I have to put the info that I want to be saved, for example, a float.\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/playgameservices/play-games-plugin-for-unity/issues/comments/74203616","html_url":"https://github.com/playgameservices/play-games-plugin-for-unity/issues/409#issuecomment-74203616","issue_url":"https://api.github.com/repos/playgameservices/play-games-plugin-for-unity/issues/409","id":74203616,"user":{"login":"claywilkinson","id":8752378,"avatar_url":"https://avatars.githubusercontent.com/u/8752378?v=3","gravatar_id":"","url":"https://api.github.com/users/claywilkinson","html_url":"https://github.com/claywilkinson","followers_url":"https://api.github.com/users/claywilkinson/followers","following_url":"https://api.github.com/users/claywilkinson/following{/other_user}","gists_url":"https://api.github.com/users/claywilkinson/gists{/gist_id}","starred_url":"https://api.github.com/users/claywilkinson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/claywilkinson/subscriptions","organizations_url":"https://api.github.com/users/claywilkinson/orgs","repos_url":"https://api.github.com/users/claywilkinson/repos","events_url":"https://api.github.com/users/claywilkinson/events{/privacy}","received_events_url":"https://api.github.com/users/claywilkinson/received_events","type":"User","site_admin":false},"created_at":"2015-02-13T04:27:29Z","updated_at":"2015-02-13T04:27:29Z","body":"This sample is using the class GameData to hold the information that will be saved and is very trivial.  The way it works is the class overrides the ToString() method to create a string representing the data to save.   This is called by ToBytes() which gets the string and then returns the byte array to save using the SavedGame API.  \r\n\r\nLoading is done in reverse - the byte array is received in the callback SavedGameLoaded(), and then FromBytes() is called, which creates a string from the bytes, and then passes the string to FromString() which splits the string on the : character (which is used in ToString()) and then converts each field  to the correct type.\r\n\r\nA robust solution could use something easy, but not very space efficient, like a JSON string. which can provide a more automatic conversion from an object to a string (or bytes)."}}