Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ZDatabase - Unity Database

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

  1. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    I'm finally getting around to messing around with the database and I have a couple of questions.

    1. Can I have multiple instances of a database? For example: Quest DB and Item DB. If so, how is this done?

    2. How can I change information about the DB? Such as passwords, DB name, etc. I see the ZDatabase file there, but I'm not sure if I should mess with it or not. There's nothing in the documentation about it, either.

    These are my main concerns at the moment, I may have more as I play around with it.

    Any information you can provide would be greatly appreciated.
     
  2. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Thanks for trying ZDatabase.

    1. There is only one instance of the database. You can differentiate your data by using prefixes (or suffixes) with your field names. For example, "Quest_XXXX", and "Item_XXXX". If you read my previous posts on the speed of ZDatabase, you'll see very little overhead is gained, even with 100K+ items.

    2. There is a function called ChangeDBKey(byte[] newKey), which will allow you to change the encryption password on the database. The documentation covers all the nitty gritty details.
     
  3. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    Okay. What if I want to copy the database to another project? I'm using another program to enter values into the database as I don't want to clutter up my original project with a lot of extra stuff.

    How about setting up ranges? For example, quests are all DB ranges from 10001 to 20000. Items are from 1 to 10000. Maybe NPCs or something would be from 20001 to 30000. I like things being organized, and I'd prefer to keep things simple if possible.

    Sorry for the crazy questions, I'm trying to figure this all out and I haven't messed with databases in a long time.
     
  4. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Yes, you can set up field ranges. You could name the fields something like "Quest_10001", "Quest_10002",etc. I do something similar for my own games.
     
  5. drastick

    drastick

    Joined:
    Aug 16, 2011
    Posts:
    12
    Two quick questions...

    1. Is this a wrapper around PlayerPrefs?
    2. The answer to 1. is no, where does the data save to when written to disk, registry? If file, where is it stored and can you specify a custom name for it?

    Thanks!
     
  6. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    506
    I'm wondering the same thing. I want to copy the DB to another project and knowing this information would be great.
     
  7. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    ZDatabase does save to the PlayPrefs file
     
  8. vanhouten777

    vanhouten777

    Joined:
    Apr 30, 2012
    Posts:
    138
    Sir,

    How do we create a database?
    Regards,
    vanhouten777.
     
  9. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    It's created automatically the first time you use any of the Write functions
     
  10. launemax

    launemax

    Joined:
    Feb 14, 2013
    Posts:
    16
    Hy there! Amazing asset :)

    I got two questions:
    1) How much data can I store in a field? For example in a byte[]-field? Is there unlimited space for data or does it get truncated?
    2) How long does it take to read the data (for example my big byte[]-field)? Is it asynchronous or do my app freeze until its done?

    Thanks for your support.
    Happy Holidays!
    Best wishes from Austria ;)
     
  11. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    As far as I can determine, there is no size limit for a data field, other than those imposed by the data type itself.

    Reading speed is variable, and depends on two factors: a) how many records are in the database, and b) how big the record itself is.

    I previously posted my read speed test results, and there really is no appreciable performance difference until around 100K records.

    Yes, the read is sequential. If you need to read async, then I would recommend creating a CoRoutine to do the read. However, please note that even with a very large database, the max time to read was about 2 seconds in the absolute worst case scenario (large record, at the "end" of the database recordset).

    If you're reading multiple large records, I would recommend showing a progressbar of some type to the user, so they know you're game is doing work.

    Hope this answered your questions.

    Happy New Year
     
  12. launemax

    launemax

    Joined:
    Feb 14, 2013
    Posts:
    16
    Yep. Thanks :)
    Happy New Year to you too
     
  13. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    I have released an for ZDatabase - its being reviewed by Unity and should be approved in the next few days.

    This update includes new encryptions supported on all Unity platforms (including WP8), and improved compression of certain data types (Vectors, bytes, etc).

    Because of these changes, you'll need to run an Conversion script in your game clients before you can use the new functions. I've included documentation in the new package, and will also attach it here.

    If you have any questions, please feel free to post here or email me. Thanks.
     

    Attached Files:

  14. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Hey all,

    I've published a new asset called "Extension Methods for Unity", which has over 800 extension methods for Unity and general C# development. Methods were submitted by the Unity community, gleaned from third party websites, and developed by me during my game development career.

    Tested on these platforms: PC, Mac, Android, iOS, WP8, and Web Player. Should work on all Unity supported platforms.

    Methods for:
    * Targeting
    * Layers
    * Movement and Rotation
    * GameObjects and Components
    * Spawning Objects and Prefabs
    * Serialization
    * Exceptions
    * Lists and Arrays
    * Cryptography (WP8 safe)
    * Numbers and Conversions
    * Strings
    * Many more!

    Documentation

    Extension Methods for Unity on the Asset Store
     
  15. Kiori

    Kiori

    Joined:
    Jun 25, 2014
    Posts:
    161
    Do you ever plan on adding GUI/no coding interface to you asset?
    I was particularly looking for something that would save time with coding. and these types of tasks are so much easier in a gfx interface.
     
  16. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Hi Kiori,
    To be honest, I hadn't considered it until you asked.

    I could see an advantage for it. I'll put it on my list, but I probably won't be able to work on it until next month.

    Thanks,
    Philip
     
  17. Kiori

    Kiori

    Joined:
    Jun 25, 2014
    Posts:
    161
    Thanks! I Mean its not that hard to implement, and it could greatly speed the workflow. Plus it could be a top feature for the asset.
    If you do it, be sure to include the option of creating/editing lists or connected/tagged values, and not just individual variables floating around.
     
  18. mikusu11

    mikusu11

    Joined:
    Mar 5, 2013
    Posts:
    5
    Hi.

    Please tell me how to migrate v1 of data to v2.

    "Migration script (ZDConvertToV2.ConvertEncString described in the document)" can not be found.

    Do'll be of "ZDConvertTo15"?

    Or ZDConvertToV2.ConvertEncString I'll be a function of the purchaser itself created in the original?

    I want to read the data of v1 in v2.
     
  19. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Might be a typo in the doc, the class is "ZDConvertToV2".

    You'll need to convert each field one time, using the ZDConvertToV2 functions.
     
  20. mikusu11

    mikusu11

    Joined:
    Mar 5, 2013
    Posts:
    5
    Thank you for answer.

    But I tried to effective the following script did not work.

    I'm sorry understanding is poor.

    If it is good, Could you provide a sample of the script.

    -----
    //[ condition ]
    //after save v1-data

    //***example1
    //error console | CS0103: The name `ZDConvertToV2' does not exist in the current context
    if (ZDatabase.ReadEncString ("v1-data") == "") {
    ZDConvertToV2.ConvertEncString("v1-data");

    string s = ZDatabase.ReadEncString ("v1-data");
    ZDatabase.WriteEnc ("v1-data", s);
    }

    //***example2
    // no error, but not convert
    ZDConvertTo15.ConvertEncString("v1-data");
    -----
     
  21. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    HI Mikusu,

    Apologies for the delay - I've been out of the office for the holidays.

    If you are upgrading from a previous version of ZDatabase, you could do something like this:

    Let's say you have two fields you already saved with the older version:
    ZDatabase.WriteEncString("v1_string", "string value");
    ZDatabase.WriteEncInt("v1_int", 1);​

    In your upgrade script, I would first check if an upgrade has already happened, and if not, then perform the upgrade, like this:

    if (! ZDatabase.ReadBool("HasUpgradedZDatabase", false))
    {
    // upgrade string
    ZDConvertToV2.ConvertEncString("v1_string");

    // upgrade int
    ZDConvertToV2.ConvertEncInt("v1_int");

    // set the upgrade flag, so this only runs the first time
    ZDatabase.WriteBool("HasUpgradedZDatabase", true);
    }​

    That script, above, should run before any of your code that uses ZDatabase.

    After that script runs, the rest of your original code would remain the same.
     
  22. ibompuis

    ibompuis

    Joined:
    Sep 13, 2012
    Posts:
    99
    Hi merlin981

    i juste updated to V2 but don't work on IOS 8.1.2 :

    Code (CSharp):
    1. // First time create defaul race value
    2.         if (! ZDatabase.ReadBool("DefaultRecordsCreated", false))
    3.         {
    4.             // the default records have not been created, so begin creating them here
    5.             ZDatabase.Write("raceOne", true);
    6.             ZDatabase.Write("raceTwo", false);
    7.             ZDatabase.Write("raceThree", false);
    8.  
    9.             ZDatabase.Write("tmpRace", 1);
    10.  
    11.             // the final line should set the created flag, so this block will not run more than once
    12.             ZDatabase.Write("DefaultRecordsCreated", true);
    13.         }
    14.  
    15.         btnRace02.GetComponent<UIButton>().isEnabled = false;
    16.         btnRace03.GetComponent<UIButton>().isEnabled = false;
    17.  
    18.         if ( ZDatabase.ReadBool("raceTwo", false) == true)
    19.         {
    20.             btnRace02.GetComponent<UIButton>().isEnabled = true;
    21.         }
    22.  
    23.         if ( ZDatabase.ReadBool("raceThree", false) == true )
    24.         {
    25.             btnRace02.GetComponent<UIButton>().isEnabled     = true;
    26.             btnRace03.GetComponent<UIButton>().isEnabled     = true;
    27.         }
    in other script :

    Code (CSharp):
    1. void SetNextRace ()
    2.     {
    3.         tmpRaceNum = ZDatabase.ReadInt("tmpRace", 1);
    4.         Debug.Log("Current race number "+tmpRaceNum);
    5.         switch (tmpRaceNum)
    6.         {
    7.         case 1:
    8.             data.SetBestTime1(GetStringBestTimeLaps());
    9.             ZDatabase.Write("tmpRace", 2);
    10.             ZDatabase.Write("raceTwo", true);
    11.             break;
    12.         case 2:
    13.             data.SetBestTime2(GetStringBestTimeLaps());
    14.             ZDatabase.Write("tmpRace", 3);
    15.             ZDatabase.Write("raceThree", true);
    16.             break;
    17.         case 3:
    18.             data.SetBestTime3(GetStringBestTimeLaps());
    19.             Debug.Log("All race completed");
    20.             break;
    21.         default:
    22.             //..
    23.             break;
    24.         }
    25.         // Open win panel
    26.         ViewScore();
    When I finish course can't open course number 2 but :

    ZDatabase.Write("tmpRace", 2);
    ZDatabase.Write("raceTwo", true);

    is called.


    Works fine with version 1.4

    Thanks
     
  23. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Hi Stephanie,

    Three questions

    Did you run the upgrade script before attempting your reads?

    Are you getting any exceptions?

    If so, can you please list them?

    Thanks,
    Philip
     
  24. ibompuis

    ibompuis

    Joined:
    Sep 13, 2012
    Posts:
    99
    Thx Philip,

    I don't have upgraded because i don't use encrypt function maybe this this my mistake ?
    Upgrade is obligatory ?

    And no exception or error, just nothing are saved.
     
  25. ibompuis

    ibompuis

    Joined:
    Sep 13, 2012
    Posts:
    99
    Another strange error, if i wrote :

    Code (CSharp):
    1. ZDConvertToV2.ConvertEncString("v1_string");
    The name ZDConvertToV2 does not exist in the current context
     
  26. ibompuis

    ibompuis

    Joined:
    Sep 13, 2012
    Posts:
    99
    Ok tried to update :

    Code (CSharp):
    1. if (!ZDatabase.ReadBool("HasUpgradedZDatabase", false))
    2.         {
    3.             // upgrade string
    4.             ZDConvertTo15.ConvertEncBool("raceOne");
    5.             ZDConvertTo15.ConvertEncBool("raceTwo");
    6.             ZDConvertTo15.ConvertEncBool("raceThree");
    7.            
    8.             // upgrade int
    9.             ZDConvertTo15.ConvertEncInt("tmpRace");
    10.  
    11.             // upgrade string
    12.             ZDConvertTo15.ConvertEncString("bestTime1");
    13.             ZDConvertTo15.ConvertEncString("bestTime2");
    14.             ZDConvertTo15.ConvertEncString("bestTime3");
    15.            
    16.             // set the upgrade flag, so this only runs the first time
    17.             ZDatabase.Write("HasUpgradedZDatabase", true);
    18.         }
    Error :

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. ZDatabase_v14.ReadEncBool (System.String fieldName)
    3. ZDConvertTo15.ConvertEncBool (System.String fieldName)
    4. LogicInterfacesHome.InitRaceChoice () (at Assets/_MAIN_PROJECT/_SCRIPTS/LogicInterfacesHome.cs:137)
    5. LogicInterfacesHome.OpenRaceChoiceGame (UnityEngine.GameObject go) (at Assets/_MAIN_PROJECT/_SCRIPTS/LogicInterfacesHome.cs:126)
    6. UIEventListener.OnClick () (at Assets/NGUI/Scripts/Internal/UIEventListener.cs:42)
    7. UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
    8. UICamera:Notify(GameObject, String, Object) (at Assets/NGUI/Scripts/UI/UICamera.cs:1079)
    9. UICamera:ProcessTouch(Boolean, Boolean) (at Assets/NGUI/Scripts/UI/UICamera.cs:1851)
    10. UICamera:ProcessMouse() (at Assets/NGUI/Scripts/UI/UICamera.cs:1429)
     
  27. wayneh654

    wayneh654

    Joined:
    Nov 25, 2012
    Posts:
    15
    Looking at ZDatabase.
    Hope this is not a stupid question. If a game is close on say a iPhone or Android, that is, the app is sent to the background while they answer a phone call or do something else, will the data in the database be lost when they return again?
    What if the phone\device crash, or it is rebooted, will the data be lost. That is, is it save somewhere on the phone that Unity3D can then retrieve this data?

    Thank you merlin in advanced, Wayne
     
  28. supergator

    supergator

    Joined:
    Apr 25, 2015
    Posts:
    2
    Hi. I'm hoping that your ZDatabase plug-in might be the solution for a project that I am working on.

    I have created a BIM (building information model) using Autodesk Revit and have imported it (via 3DS Max) into Unity 3D. Now I want users of the Unity model to be able to access information regarding building components within the model. For example, if the user wanted to know the specifications (manufacturer, model #, finish, etc) for a specific light fixture, he could click on the object and a pop-up type window would display that information.

    Because the sheer volume of information for all the various building components is voluminous, it would not be practical to include the data directly in the code (I don't think). However, the information is available in a database format.

    What I am hoping that I can do is link the various building components within the Unity model to the database file. When the user clicks on an object, Unity accesses the database and displays the correct and relevant information.

    Would ZDatabase be helpful to me?

    Thanks.
     
  29. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Hi Supergator,

    Yes, ZDatabase can store the information you need for your BIM app.

    The documentation details what data types can be stored. Anything not explicitly provided can either be serialized to byte array (which I recommend, since you can also compress it), or custom data types can be added.

    Thanks,
    Philip
     
  30. Galric

    Galric

    Joined:
    Apr 28, 2015
    Posts:
    1
    HI! merlin!
    I have a question!
    Is there a way to backup data stored on ZDatabase?
     
  31. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Hi Galric,

    ZDatabase uses the same data store as PlayerPrefs, so using the same methods of archiving PlayerPrefs will also archive ZDatabase.
     
  32. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Is this still alive? I notice the doc link in Asset Store is broken?
    I'm looking for a DB solution for saving at the moment daily records such as text, number etc searchable by date