Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by futureseeker 1 · Jan 08, 2011 at 02:36 AM · databasestandaloneloginmysqlhighscores

How can i Connect a database for my Log in form? (For windows Standalone)

I have read some answers here in Unity forums, but i still don't know where to begin adding a database. I have an experience in MySql. Can i use it in my Log in form in a standalone game? most of the answers I've read are like for Online or Web games, like using WWW. can i also use it for an offline standAlone game?

I hope someone can enlighten me about these things. Thanks a lot!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PrimeDerektive · Jan 08, 2011 at 03:10 AM 0
Share

I'm not sure I follow; what would be the point of a database-driven login in an offline game? By offline do you mean single player, as in, you want the player to have to log in to play single player? Either way, you still have to use the WWW class, and the $$anonymous$$ySQL database would have to accessible over the Internet.

avatar image fireDude67 · Jan 08, 2011 at 03:31 AM 0
Share

I think he means offline as in the files are stored on the users computer

avatar image PrimeDerektive · Jan 08, 2011 at 04:30 AM 0
Share

Well, that seems a little silly, as I don't think anyone would ever be able to play his game except him, because they'd have to have $$anonymous$$ySQL installed locally and the database and tables built and set up, and I don't think thats something he could make Unity do automatically. And even if he did do it locally, he'd still have to interface with it with WWW and his private ip/loopback address, as a local server, and still have to run some web code (php/python or whatever) to get the data. Derek Traver 0 secs ago

avatar image yoyo · Jan 08, 2011 at 04:43 AM 0
Share

(I suspect the OP means a windows standalone client -- native executable, not browser-hosted -- which connects to an online database.) Note that the System.Data namespace and friends should work, so you can open a SqlConnection and retrieve data into a DataSet. Let me know if it works, 'cuz I want to do the same thing! :-) http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=VS.90).aspx

avatar image futureseeker 1 · Jan 08, 2011 at 12:02 PM 0
Share

i actually don't mean a log in system, but something that can store someone's name, score, and level. Like when i type my name, i can load the level where i stopped.I'm really sorry if i can't express it so clear..should i try the codes posted which uses the WWW function?i'm quite confused on where to begin :S and i'm really confused o

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by PrimeDerektive · Jan 08, 2011 at 02:37 PM

Ohh Jeez! No, you do not have to go that crazy; if you just want to store simple things like the player's name, his score, and what level he was on, use PlayerPrefs; you don't need to use MySQL at all, that would be overkill.

See the PlayerPrefs docs for details: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

It is a very simple solution for saving simple data to the local computer in Unity, like ints, strings, and floats.

So in your game, when you want to save, you would do something like:

PlayerPrefs.SetString("PlayerName", "Derek");
PlayerPrefs.SetInt("PlayerScore", 1337);
PlayerPrefs.SetInt("CurrentLevel", 2);

Then when you start your game, you can retrieve all that stuff, and do whatever you want with it:

var playerName = PlayerPrefs.GetString("Player Score") var score = PlayerPrefs.GetInt("PlayerScore"); var currentLevel= PlayerPrefs.GetInt("CurrentLevel");

Application.LoadLevel(currentLevel);

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image futureseeker 1 · Jan 09, 2011 at 12:18 AM 0
Share

Ohh!Thanks a lot for this!i think this is the answer that i'm looking for. Sorry for being a noob..i think imma go and learn PlayerPrefs..the code posted was a great help,Thanks so much again, i hope i can get back here to ask help if i encounter one..you guys are great!THAN$$anonymous$$S!

avatar image futureseeker 1 · Jan 09, 2011 at 09:25 AM 0
Share

can i use playerPref for multiple player?i'm confused how to save the current game and still retrieve the data of the other players.i hope you can help me with this :)

avatar image PrimeDerektive · Jan 09, 2011 at 02:20 PM 0
Share

$$anonymous$$ultiple players on the same computer? Like different "saves"?

avatar image futureseeker 1 · Jan 10, 2011 at 12:57 PM 0
Share

Yup..just in the same computer..would it be possible to use playerPrefs for that?or does it store only data for a single player?Thanks a lot Derek!

avatar image
1
Best Answer

Answer by Statement · Jan 08, 2011 at 02:49 PM

Derek hit the spot.

I just want to contribute with some nice thoughts about savegame data. It can be beneficial to have a struct or class describing the data in a type safe manner, so it is easy to find the different settings with intellisense. Then you would have a load and save function to save or load to player prefs. You could even use reflection to automate the save/load process.

With abstracting the storage layer away, you can later change from PlayerPrefs to ini files or xml files or sql tables. Your code would work none the less.

Example:

public class Savegame { public string playerName; public int playerScore; public int currentLevel;

 public static Savegame Load()
 {
     Savegame loaded = new Savegame();
     loaded.Reload();
     return loaded;
 }

 public void Reload()
 {
     playerName = PlayerPrefs.GetString("playerName");
     playerScore = PlayerPrefs.GetInt("playerScore"); 
     currentLevel = PlayerPrefs.GetInt("currentLevel"); 
 }

 public void Save()
 {
     PlayerPrefs.SetString("playerName", playerName);
     PlayerPrefs.SetInt("playerScore", playerScore);
     PlayerPrefs.SetInt("currentLevel", currentLevel);
 }

}

Then you can use it such as

void Start()
{
    Savegame game = Savegame.Load();
    Application.LoadLevel(game.currentLevel);    
}

or

void Start()
{
    game.currentLevel = Application.currentLevel;
    game.Save();
}

I add yoyos comment here for formatting purposes.

public class Savegame
{
    public string playerName 
    { 
        get { return PlayerPrefs.GetString("playerName"); } 
        set { PlayerPrefs.SetString("playerName", value); } 
    }
    // and so on...
}
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image futureseeker 1 · Jan 09, 2011 at 12:20 AM 1
Share

Oh thanks for this statement!!i'm just quite confused because i use javascript, but i think this would be very helpful. imma go and learn PlayerPrefs first to eli$$anonymous$$ate my confusions..thanks again to the both of you!!THAN$$anonymous$$S! >_< i'll get back if i didn't get something..take care!

avatar image Statement · Jan 09, 2011 at 01:48 AM 0
Share

Just add a comment here if you need to get hold of me. I usually read all comment replies.

avatar image futureseeker 1 · Jan 09, 2011 at 05:50 AM 0
Share

can i use playerPref for multiple player?i'm confused how to save the current game and still retrieve the data of the other players.i hope you can help me with this :)

avatar image yoyo · Jan 09, 2011 at 06:35 AM 0
Share

You could also use C# property getters, for example

 public string playerName
 {
     get { return PlayerPrefs.GetString("playerName"); }
 }

(Sorry about the formatting, wish I could put code in comments ...)

avatar image Statement · Jan 09, 2011 at 01:32 PM 0
Share

@yoyo I'll put your comment into my post so it get formatted. @futureseeker This will only work on a local machine. So as long your multiplayer is like several profiles on a single computer or split screen etc it's fine. You can't access data on another computer with playerprefs. Simply prefix with player name. plr1_health, plr2_health etc...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How do I add login and register? 1 Answer

MySQL Database Highscore 0 Answers

Trying to get server side highscores working with MySQL. Apparently I need a Secret Key for my database. What is that? How do I get one? 1 Answer

Connection with MySQL 1 Answer

High Scores Online (PHP,MySQL) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges