- Home /
How to save a progress by user with diferent users
I have a problem I need to have a login System of users , but I can´t relate the Player.Prefs with the screen that display the list of player registered and new users ...... someone that had make somthing like that ?? I need to save the progress of each user....
thanks
Answer by aldonaletto · Jan 04, 2012 at 01:23 AM
You could add the user name or code to the key when saving and restoring its value. If you want to save the score, for instance:
// save the score:
PlayerPrefs.SetFloat(username+".Score", score);
// retrieve the score:
score = PlayerPrefs.GetFloat(username+".Score", 0);
EDITED: You can save multiple users data this way:
1- Save the usernames with keys like "User"+num. To retrieve all registered users, do something like this:
var numUsers = PlayerPrefs.GetInt("NumUsers", 0); // how many registered users?
var usernames = new String[numUsers+1]; // create the user name array...
for (var n = 1; n <= numUsers; n++){
usernames[n] = PlayerPrefs.GetString("User"+n, ""); // and load them
}
2- Show a GUI menu with the users, and let the player select his user name or code;
3- Use the user name or code to find all other user info you need (health, points, weapons, position etc.), always using keys composed by the user name or code plus the info name, like this:
health = PlayerPrefs.GetFloat(username+".Health", 100);
points = PlayerPrefs.GetInt(username+".Points", 0);
hasShotgun = PlayerPrefs.GetInt(username+".Shotgun", 0);
hasMachinegun = PlayerPrefs.GetInt(username+".MachineGun", 0);
...
Obviously, you must save the data with the same format. When an user is registered, for instance, save its data like this:
var numUser = PlayerPrefs.GetInt("NumUsers", 0);
numUser += 1; // count this user
PlayerPrefs.SetInt("NumUsers", numUser); // update users count
PlayerPrefs.SetString("User"+numUser, username);
...
but when the user return how select again their account?? to still playing?
Take a look at my answer: I edited it to include other details about multiple users.
Hello, I would need help to create several "usernames" via UI of Unity, I am currently looking for a solution by consulting all the possibilities like your written here but I do not come to register "usernames" of the player as well as his backup Who matches his nickname?? I would like to do it via UI ie: "InputFiel" > "button + text" > Clicks the nickname match the player > resets his last backup... With the "serializable" mode
If you can look at my question is to be able to help me I would recognize you thanks in advance
hiii... I am new to unity.. I am a flash developer... but now a days it seems to be unity is BEST for using 3d effects, I am developing games for global or social purpose... but i am facing a lot of problems to save my scores.. so i am searching for the same... After R&D I found some samples and tutorials...... check these samples and tutorials, and tell me if you are familiar with all that. Guys if You know anyThing about my problem... then please let me know... thanxs... Gamers waiting for you reply.
@moghes I have converted this to C# BUT I am receiving an error on this line PlayerPrefs.SetString("User"+numUser, username); --------- Visual Studio is telling me "username does not exist in the current context". I do not logically understand how or where to add a "username" statement (OR whatever it is called since SO$$anonymous$$E other people have chosen to pick on me because I am VERY new to coding still). Could you PLEASE help/explain?
username is a string variable to which you assign the user name when the user types the desired name. Use some sort of GUI in order to obtain the user name - kind of:
using UnityEngine;
using System.Collections;
public class RegisterUser : $$anonymous$$onoBehaviour
{
string username = "";
void OnGUI()
{
GUI.Label (new Rect (10, 10, 150, 20), "User Name:");
// User enters username into this text field
username = GUI.TextField(new Rect(10, 30, 200, 20), username, 25);
if (GUI.Button(new Rect(150,60,50,25), "ok")){
int numUser = PlayerPrefs.GetInt("NumUsers", 0);
numUser += 1; // count this user
PlayerPrefs.SetInt("NumUsers", numUser); // update users count
PlayerPrefs.SetString("User"+numUser, username);
}
}
}
That's a very simple GUI code, just an example (and a very ugly one!). You should elaborate it a lot more in order to get something useful. Attach this script to some object in scene.
Answer by nik_bluebird · Apr 19, 2019 at 11:39 AM
Best way to storing data is Binary Files. Bcoz it is most secure, as data store in terms of zeros and ones.
For more information, go through this link:
https://unity3d.com/learn/tutorials/topics/scripting/introduction-saving-and-loading
I hope it helps.
Your answer
Follow this Question
Related Questions
Set int to object from list? 0 Answers
Why is JSON not saving my Class? 2 Answers
List Contains false after playmode 0 Answers
Saving/Loading class list with SimpleJSON,Saving class list with SimpleJSON 1 Answer
save load list 3 Answers