Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ZDatabase - Unity Database

Discussion in 'Assets and Asset Store' started by merlin981, Nov 27, 2012.

  1. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Tired of those hard to use, long learning curve database packages for Unity? Need something quick and easy, that can store almost anything you can think of?

    ZDatabase supports:
    * Saving strings, int, float, Guid, Vector2, Vector3, Vector4, and more
    * Read / Write functions that are easy to use. No pre-setup required
    * Works on all platforms (including Consoles!). PC, Android, iOS, Wii, XBox 360, and PS3
    * Encryption functions available as well. Every functions has an encrypted alternative as well. Encryption uses Rijndael 128 bit encryption

    Demo scene included.

    If you need any additional data types added, feel free to send a PM or post in the forum, and I will include in the next release.

    http://u3d.as/content/rocket-games/zdatabase-unity-database/3G7
     
  2. TechnicalSam

    TechnicalSam

    Joined:
    Apr 24, 2012
    Posts:
    4
    Quite interested in this. I have a project which is going to have to do some data visualisation of integer data recorded every day for an Android and iOS app. I was wondering what sort of performance limitations i'm going to have when it comes to long term use...

    e.g. how many records can it store and 'SELECT * from recordedInts WHERE data > start and date < finish' and whats sort of response times will say 1000 records take to pull out?

    Sorry for the crappy SQL pseudo but its easier to convey my need.
     
  3. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Hello TechinalSam,

    That is a very good question. I put together a quick test to demonstrate write / read speed. I had it insert 100,000 records, then read the first, middle, and last records.

    Insert speed does decrease as the number of record increases. I noticed the insert speed decrease around 60K records. At this time, it was inserting 150 records in about 800ms.

    I ran this test on an Android Samsung Galaxy S2 phone. Speed should be comparable on a iPhone 4S.

    Total time to insert 100,000 records: 402,085ms (40.2 seconds)
    Read time for first record: 0ms
    Read time for 50,000th record: 15ms
    Read time for 100,000th record: 33ms

    The issue with the inserts is that the database has to be expanded to handle new records. All databases have to handle this, and I've done my best to optimize this for mobile devices.

    I'm available if you have any further questions.
     
  4. badawe

    badawe

    Joined:
    Jan 17, 2011
    Posts:
    297
    Works on web?
     
  5. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    +1

    Texture?
     
  6. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Yes, it works for web.

    It does not currently support Texture, but I can add that if requested.
     
  7. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    I've just uploaded a new version which supports Texture2D. It should be approved and in the asset store later today or tomorrow.
     
  8. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,811
    Does it support SQL syntax?
    If it supports web player, where and how is the actual data stored?

    I'm looking at this specifically for a web player solution.
     
  9. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    ZDatabase uses functions for each data type. I'm attaching the current documentation, so you can see how it works.
     

    Attached Files:

  10. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,811
    Ok I went through the documentation.
    Aside from offering encryption, which is fairly trivial to implement, what does this package offer over simply using playerprefs to read and write data values?
     
  11. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    The advantages are being able to quickly save / retrieve objects of different data types, without having to write your own custom serializers (since PlayerPrefs only gives you Int, Float, and String). The same advantages you will have with almost every PlayerPref alternative on the asset store.

    If you need to use SQL statements, there are a few good embedded SQL databases on the asset store. I'm providing ZDatabase as an alternative to PlayerPrefs, and to make it easier for people to save/retrieve objects.

    As I've already previously stated (and demonstrated above), if you need additional object types to be included, I'm happy to add them into new releases.
     
  12. liverolA

    liverolA

    Joined:
    Feb 10, 2009
    Posts:
    347
    is this used for local database/remote database?
     
  13. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    The database is stored on the same device as the game.
     
  14. rooster

    rooster

    Joined:
    May 22, 2009
    Posts:
    21
    I just bought this thinking it was a full database but it looks like you can only store one field per record. Is that correct? Is there any way that I can store a bunch of fields in each record? (other that faking it by encoding it myself).
     
  15. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Can you give me an example of what you're trying to do? Are you wanting to store an array / List to a field, or something else?

    I'm happy to add additional functionality and/or data types to ZDatabase.
     
  16. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Seems quite cool for single player games - and its cheap. Might buy it to save some time of doing it all myself!
     
  17. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
    dumb question i iknow but work with javascript variables?
     
  18. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Yes, it works with JavaScript as well.
     
  19. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Looks really good. Does this also work with Flash .
     
  20. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Yes, ZDatabase does work with Flash as well :D
     
  21. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Great, do you know how it performs on Flash.
     
  22. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    I assume you mean performance-wise?

    I had previously answered a question about performance on mobile devices (see page one of this thread). I re-ran the test on a laptop (quad core 2.6 GHz, Windows 7, 8 GB RAM).

    I had it insert 100,000 records, then read the first, middle, and last records.

    As I explained in the previous post, insert speed does decrease as the number of records increase. The issue with the inserts is that the database has to be expanded to handle new records. All databases have to handle this, and I've done my best to optimize this for mobile devices.

    Total time to insert 100,000 records: 154,042ms (15.4 seconds)
    Read time for first record: 0ms
    Read time for 50,000th record: 4ms
    Read time for 100,000th record: 9ms


    I'm available if you have any further questions.
     
  23. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Thanks.

    1. Are you including code to access the Database, some sort of SQL-like language to access the data ?

    2. What is the recommended best practice to store/retrieve a list of hierarchical data of different types ?
     
  24. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    it will be helpful if it works on web player builds to use textures and texts
     
  25. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305

    1. It is not SQL-like language, but a function for each object type. I linked the PDF documentation in the original post, and on the Asset Store page. For example, to write a Vector3, you would do:
    ZDatabase.Write("Name of Vector3 Object", Vector3);


    2. For lists, there are three choices:
    a. Serialize the data to a byte array, then save using the byte[] overload of Write. ZDatabase.Write("Data Name", byte[]);
    b. Save each item in the list individually. First, you save the length of the list, then loop through each item.
    ZDatabase.Write("List Length", List.Length);
    for (i=0; i<List.Length; i++)
    ZDatabase.Write(string.Format("List Item {0}", i), List);

    c. Let me know what type of list you have, and I can add a Write function to handle it.
     
  26. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    The last update of ZDatabase included support for Texture2D.
     
  27. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    So I purchased this database and now I'm wondering what the best way to input records is. Is there a way to use a web browser to do it, or do we have to do this all by hand? I guess I should have looked this up before I purchased, but I was under the impression there was something available for easy record input to build the database.

    If not, then what would be the best way to build the database, especially if I'm looking at a thousand records for items (weapons, armor, trinkets, etc.) as well as NPCs and the like? I read the documentation which is good at describing how to use the commands, but it doesn't really get into creating the database in the first place.
     
  28. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305

    The general method is to create a script that runs when the game loads (or perhaps delay running it until the player is at the start screen). The script will first check if the database is created, and if not, will create the default records.

    You could use something like:

    if (! ZDatabase.ReadBool("DefaultRecordsCreated", false))
    {
    // the default records have not been created, so begin creating them here

    // the final line should set the created flag, so this block will not run more than once
    ZDatabase.Write("DefaultRecordsCreated", true);
    }


    You can simply use the ZDatabase Write functions to create the records.
     
  29. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    So there's no way to interact with the database outside of the game? Entering that many items in Unity is going to be difficult and I wonder if I made a mistake in buying this.

    Can records be a combination of things?

    For instance:

    Item #: 1 (int)
    Name: Sword (string)
    Quality: rare (int)
    Damage Low End: 1 (int)
    Damage High End: 10 (int)
    Graphic: sprite (gui object)
    Model: sword prefab (prefab)

    I would know how to set this up in a normal database, but I'm at a loss as to how to do it here, and even finding records I'm not sure about.

    Sorry for the trouble, I should have done more research in the beginning.
     
  30. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305


    Yes, records can be combinations. Below is an excerpt from an private message between myself and a user of ZDatabase. You can use this same principle for creating complex records in ZDatabase.


    We'll create a class that only holds properties for each data item:

    Code (csharp):
    1.  
    2. public class PlayerStats
    3. {
    4.    public Guid UniqueID {get; set;}
    5.    public string Firstname {get; set; }
    6.    public string Lastname {get; set;}
    7.  
    8.    // etc....
    9. }
    10.  
    Then, we would add two functions to the class - serialize and deserialize, which will convert the class into a string (or binary array), and convert it back to the original class. Unity Pro supports true .NET serialization, but in general you want to avoid that since it takes much longer and includes more data than you need. Using the methods below works with both the Free and Pro versions of Unity...

    Code (csharp):
    1.  
    2. public string Serialize()
    3. {
    4.    return string.Format("{0}|{1}|{2}", UniqueID.ToString(), Firstname, Lastname);
    5. }
    6.  
    7. public static PlayerStats Deserialize(string serializedData)
    8. {
    9.    // split the string
    10.    string[] tempStr = serializedData.Split('|');
    11.  
    12.    // create the return object
    13.    PlayerStats retVal = new PlayerStats
    14.    {
    15.       UniqueID = new Guid(tempStr[0]),
    16.       Firstname = tempStr[1],
    17.       Lastname = tempStr[2]
    18.    };
    19.  
    20.    return PlayerStats;
    21. }
    22.  

    Once you have these, you can call Serialize to save one record to the database. When you need to read the data, just call Deserialize:

    Code (csharp):
    1.  
    2. List<PlayerStats> ListOfStatsToSave = new List<PlayerStats>();
    3.  
    4. // ListOfStatsToSave is populated first by the app / user
    5.  
    6. // hold the number of records in ListOfStatsToSave
    7. int StatCount = ListOfStatsToSave.Count;
    8.  
    9. // save the count to the database
    10. ZDatabase.Write("StatCount", StatCount);
    11.  
    12. // save the data to the database
    13. for (int i=0; i < StatCount; i++)
    14. {
    15.    ZDatabase.Write(string.Format("PlayerStat_{0}", i.ToString().Trim()), ListOfStatsToSave[i].Serialize());
    16. }
    17.  
    When you want to read the data later:

    Code (csharp):
    1.  
    2. // get the stat count
    3. int StatCount = ZDatabase.ReadInt("StatCount", 0);
    4.  
    5. // holds the list of stats
    6. List<PlayerStats> ListOfStats = new List<PlayerStats>();
    7.  
    8. // read data from the database
    9. for (int i=0; i < StatCount; i++)
    10. {
    11.    ListOfStats.Add(
    12.       PlayerStats.Deserialize(
    13.          ZDatabase.ReadString("string.Format("PlayerStat_{0 }", i.ToString().Trim())));
    14. }
    15.  

    Hope this helps
     
  31. madnesslabo

    madnesslabo

    Joined:
    Nov 8, 2012
    Posts:
    59
    Is Zdatabase able to delete key? Such as PlayerPrefs.DeleteAll or PlayerPrefs.DeleteKey
     
  32. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Good question. Previous versions did support removing any keys. However, I just submitted a new version which has two new functions - RemoveField and RemoveAllFields. These will remove a single record, and remove all records.

    The new build is in Asset Store review, and should be available later today or tomorrow.
     
  33. madnesslabo

    madnesslabo

    Joined:
    Nov 8, 2012
    Posts:
    59
    Thanks!
     
  34. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    Has this been removed from the Unity Store?
     
  35. eskimojoe

    eskimojoe

    Joined:
    Jun 4, 2012
    Posts:
    1,440

    It's not in Asset Store anymore.
     
  36. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    Strange. He just posted a week ago that a new build was coming. I wonder what happened....
     
  37. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    My apologies, there is a problem with my store account, but the Unity rep is working on it. Everything should be back up on Tuesday or Wednesday.

    Thank you for your patience.
     
  38. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Ok, the problem has been resolved and all assets are back online. The update for ZDatabase has also been published and should be available for download now.

    Thanks
     
  39. Michael316

    Michael316

    Joined:
    Aug 2, 2012
    Posts:
    21
    Does this work with the OUYA?
     
  40. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Yes, it does work on the OUYA.
     
  41. bravo

    bravo

    Joined:
    Dec 31, 2012
    Posts:
    6
    Hi merlin,
    also work with windows phone?
     
  42. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Yes, it works on Windows Phone and Windows 8 as well
     
  43. bravo

    bravo

    Joined:
    Dec 31, 2012
    Posts:
    6
    I've tried but during the windows phone build this error is returned:

    Error building Player: Exception: Error: type `System.Security.Cryptography.RijndaelManaged` doesn't exist in target framework.

    At the moment for me is not strictly necessary to use encryption. You can exclude the reference to the namespace of encryption and functions for platforms that do not support it? ( ex. with #if )
     
  44. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305

    Quick fix for this issue:
    • Create a folder called Plugins
    • Find System.Security.dll in the .NET Framework folder. (NOTE: Make sure you get the 2.0 version. It's usually found at: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Security.dll)
    • Copy System.Security.dll to the Plugins folder

    This should resolve the issue for you. Please let me know if it does not.
     
  45. bravo

    bravo

    Joined:
    Dec 31, 2012
    Posts:
    6
    Replacing the dll error change:

    Error building Player: Exception: Error: type `System.Security.SuppressUnmanagedCodeSecurityAttribute` doesn't exist in target framework.
    Error: type `System.Security.SuppressUnmanagedCodeSecurityAttribute` doesn't exist in target framework.
    Error: type `System.Security.Permissions.KeyContainerPermission` doesn't exist in target framework.
    Error: type `System.Security.Permissions.KeyContainerPermissionAccessEntry` doesn't exist in target framework.
    Error: type `System.Security.Permissions.KeyContainerPermission` doesn't exist in target framework.
    Error: type `System.Security.Permissions.KeyContainerPermissionFlags` doesn't exist in target framework.
    Error: type `System.Security.Permissions.KeyContainerPermissionAccessEntry` doesn't exist in target framework.
    Error: type `System.Security.Permissions.KeyContainerPermissionFlags` doesn't exist in target framework.

    System.Security.SuppressUnmanagedCodeSecurityAttribute is contained in mscorlib.dll that can not be overwritten because it is a dll name reserved.
    Any other suggestions?

    Thanks
     
  46. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Do you have .NET Framework 2.0 installed? Also, try changing your Unity project to .NET 2.0 (not .NET 2.0 subset, which is default).

    My first guess is that you don't have all of the .NET Framework 2.0 installed. ZDatabase doesn't use anything extra, only what comes with .NET Framework 2.0 and what is allowed by Unity.
     
  47. bravo

    bravo

    Joined:
    Dec 31, 2012
    Posts:
    6
    Hi Merlin,
    the problem is not in the framework installed, but the wp8 target framework (everything works in the editor, is the build on phone that fails).

    .NET API for Windows Phone does not support all namespaces of the "standard" .net 2.0 fwk:

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207211(v=vs.105).aspx

    The only solution that I see is to exclude unsupported functionalities for this platform or find an alternative way to do them.
     
  48. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    I see. Ok. I'll get a new build out later today (tomorrow at the latest), with a solution
     
  49. bravo

    bravo

    Joined:
    Dec 31, 2012
    Posts:
    6
    Fantastic!
    Thanks.
     
  50. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    I've published an updated package for ZDatabase. I added a bundled package which you can extract, for using ZDatabase without encryption.

    Please note, after you extract the No Encryption package, you'll need to delete the original ZDatabase folder (otherwise, you'll have duplicate name conflicts).

    The package should be approved by Unity in the next few days.