Search Unity

Unity and Database Access

Discussion in 'Scripting' started by cporter, Dec 11, 2008.

  1. cporter

    cporter

    Joined:
    Nov 6, 2008
    Posts:
    3
    Anyone know how I might go about accessing a SQL Server Database from within Unity GUI. For example I would like to creat a walkthrough of a store and if the user interacts with an object it acesses data such as pricing, availability, and or other attributes.
     
  2. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
  3. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    What you probably don't want to do (at least I would highly not recommend doing it) is having a Unity Web player directly access a database. That would mean that you'd have to open the port your database listens to to the Internet, and that's simply something that you don't do (besides the security risks, you're out of luck immediately if people sit behind firewalls which have those ports closed).

    So, accessing the database directly from a Unity application is only relevant in two cases: Closed, controlled environments (e.g. database and clients residing inside a client's network, no access from outside needed), or if you have a Unity based game server. I have the latter case, and this works very well (after some struggles ;-) ). You can basically go with the SQL examples from Mono; just make sure to use the relevant DLL from the Mono package that comes with Unity, and not any other one (that can give you nasty crashes). Not sure if I posted my code in some of the threads that Randy mentioned - but if you know database programming with .NET, it shouldn't be a big issue (well, it depends - since .NET 2.0 there's a lot of stuff "hidden under the hood", and if you're doing db-access from that level, it won't help you much).

    However, if you talk about a walkthrough of a store, you'd probably set up a Web server that accesses the database and provides an HTTP-based interface to the data, for the Web player. So, you'd be mainly using the WWW class on Unity's end, either have a simple proprietary text-based protocol or use some sort of XML-based protocol (here, you need to be careful with Web player filesize if you use the .NET/Mono built-in XML-classes).

    On the Webserver's end, you'd probably have some ASP.NET based solution (that's likely if you are asking for SQL Server Database access because then it's all in the Windows and .NET world, which has advantages ;-) ).

    How you display the data via UnityGUI is yet another story - but you'd just do it as you would display any other kind of data, too.

    What should be obvious from this posting is that there's a long way between UnityGUI and the data in the database, with many design-decisions that have to be made - but it can be handled quite well ;-)
     
  4. joe89

    joe89

    Joined:
    Jun 3, 2013
    Posts:
    4
  5. Calum1015

    Calum1015

    Joined:
    Feb 24, 2015
    Posts:
    1
    Sorry, old thread but it really isn't hard to hook up the data from the database to UnityGUI. You can query you're requested data from the database, then parse it as a string or integer or float, anything you need, and declare a variable with this fetched data. Then you use that variable to display the data in a GUI.
     
  6. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Well, the issue is not whether it's difficult or easy but whether it makes sense. And opening a database up to the public Internet (which is what you'll need to directly access the database from a Web player) is usually not a good idea for security reasons.