Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
8
Question by Travis 1 · Apr 21, 2010 at 06:24 PM · multiplayernetworkdatabasesqlmmo

How can I connect Unity to an SQL database in order to implement an MMO?

Hello, first of all I'd like to thank each and everyone who has dedicated their time into producing Unity, and tutorials, and to those who assist others with their needs.

Now then, as far as my question is concerned, what I am planning is creating a massive multi-player online (MMO), and I have a large map so far. I've read/watched most of the tutorials available, and I'm just waiting for my paycheck to invest into the Pro version of this; but I am curious as to how I'd be able to connect my client to a server, that will register play coordinates, items, stats, etc.

Any assistance on this subject would be great. Basically, I am interested mostly into connecting the client to a SQL database.

-- To make it easier for you/me to understand, I'm going to ask if you wouldn't mind explaining it, if the database was, "client".

Comment
Add comment
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

3 Replies

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

Answer by duck · Apr 21, 2010 at 07:14 PM

With this kind of set-up you would generally call the the unity app the "client", because it's the part of the system which the end-user uses to view and interact with the world.

Your client would never normally have direct access to the database. This would generally represent a significant security problem. What is more typical is that clients would talk to a dedicated multiuser server application using socket connections (for example, something like SmartFox server, or often - for MMOs - a custom written server app).

It's also the case with simpler database interactions - such as a server-side scoreboard - that your client would never access the database directly, for similar security reasons. Instead, your client (the game) would talk to specially written server-side scripts (eg, in PHP or ASP), which in turn would access the database themselves.

Therefore it's only ever server-side programs that are under your complete control which have authorisation to directly access the databases, and your clients (which are less trusted, because they're in your user's hands!) are restricted to making higher level requests via an API of your own design, which is restricted to relevant commands such as "SubmitScore", and "RetrieveScoreboard" in the case of scores, or things like "MoveToLocation", "TakeItem", "Say Message", etc for a multiplayer RPG.

This multiuser server would then deal with handling interactions in your world, and it would be responsible for interacting with the database behind the scenes to create, read, update and delete information from the DB such as user details and persistent world data.

For this reason, your Unity client need never know about the existence of such things as SQL, tables, records, etc :-) These are all things which should stay on your server, either as server scripts or within your multiuser server application.

Comment
Add comment · Show 6 · 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 Travis 1 · Apr 22, 2010 at 12:30 PM 0
Share

So, from my understanding you are telling me that I would need to direct the client to a configuration file (PHP / ASP) that in turn would allow the client to connect and store data via a database?

avatar image duck ♦♦ · Apr 22, 2010 at 01:04 PM 1
Share

No, your client would interact with a multiuser server application (like Smartfox Server). That server-side application would then deal with the database. It would act as a 'middle man' between your client game instances, and the database itself. The client would never connect to the DB, nor supply SQL commands.

avatar image BerggreenDK · Sep 05, 2011 at 07:29 AM 0
Share

@Travis - This is also called n-tier or 3-tier architecture when developing applications such as webapplications. Client -> Server -> Database (3 tiers)

avatar image mrliioadin · Dec 08, 2013 at 07:35 PM 0
Share

Would this structure still be recommended for offline games? I'm not doing an $$anonymous$$$$anonymous$$O, but rather a tile-based turn-based strategy game.

avatar image tanoshimi · Dec 08, 2013 at 08:19 PM 0
Share

@mrliioadin no. if you're offline, you'd have no way to connect to the server which interacts with the database (unless you're planning hosting the database locally as well?). I'd look into X$$anonymous$$L for storing local data, though you could also use sqllite.

Show more comments
avatar image
10

Answer by Island Bill · May 18, 2015 at 03:21 PM

Maybe I can help clarify things a bit here for the benefit of people who stumble in here from a Google search (I know windows and Mac users sometimes have a more difficult time with separating the various parts of a network or even workstation architecture ;) ).

It IS possible to have your game DIRECTLY connect to a database, but it is NOT a good idea. Your game script could, for instance directly connect to a mysql database and issue mysql commands and that would work, but you would be exposing your database to massive security issues.

There needs to be a layer between your gamers and the database. This layer can be written in PHP, Javascript, C, or just about any language that has the ability to open a socket connection to a running database server, and it can reside on any intermediate server you choose. It does NOT have to be Smartfox or ANY service that is currently offered (I've nothing against these solutions but if you can code just a little bit and follow basic security precautions, there really isn't a need).

Example: Add a name to a database

unitygame --> php script residing on an intermediate server --> mysql database

Lets break that down. The Unity game script passes information to be handled, say a gamer name, to a php script. The php script knows that this information is being passed, and it says to unity "OK hang on, let me forward that request". The php script opens a socket connection to the mysql database and says "please add this gamer's name to the database". Mysql then processes the request and replies with an update on that request. The php script then passes the status back to unity.

It sounds very complicated but really it isn't. All you're doing is putting an intermediary, sort of like and Executive Secretary, between the game client and the actual database. It's a go-between.

I hope this makes sense.

Comment
Add comment · 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
5

Answer by Daniel 15 · Feb 10, 2011 at 11:29 PM

I've made a tutorial, explaining everything, to connect and grab data of a database, chek it out! http://forum.unity3d.com/threads/77447-Starting-my-MMO-with-SQL-SERVER!-in-Javascript

Comment
Add comment · Show 2 · 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 followmyfaith666 · Nov 01, 2017 at 05:22 PM 0
Share

I have came across your tutorial before. As far as i am concerned, it is a nice tutorial BUT it does not deal with the middle man. All that is happening on your script is UNITY is trusted with information that the user can then get.

I think and its a guess the user needs to do an include dbconnection.php

or something

avatar image Carocrazy132 · Apr 26, 2020 at 07:45 PM 0
Share

@followmyfaith666 it's still the correct answer, it answers the question. The question was not "can I have general information on how server authority works". I appreciate people addressing that issue for the OP, but as a person googling, I would appreciate the question ASKED to be answered first, and that then they address what you think would be a better route for the person coding.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Host unity game with photon 0 Answers

if(SQL SELECT) { do something; } ???? 2 Answers

simple online storage for game? Any other way? 1 Answer

Best practice for a turn-based game 1 Answer

Database connection FPS game? 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