What are my da ta storage options when using Unity with c#. My deployment target is windows only so this doesn’t need to be a cross platform solution. I have my data in a SQL compact DB at the mmoment. I’m about to waste my time writting an XML exporter for the data although i dont think that is a very good solution. What data storage options do I have?
Playprefs is the only storage option unity offers you AFAIK. (and it is just a data store).
There are some xml scripts on the wiki - check 'em out before doing anything.
Other than that you can use WWW to communicate to a web server, and if it is a desktop build you can use all the normal .net loveliness (a lot of which is disabled in webplayer, presumably mobile).
What exactly do you want to save?
I wish I could use whatever .net goodness I could dream up. I have a utility that creates quest info including dialog. Its too much data for the player prefs. I know there are people using some kind of local database for their games. My demo is an RPG and if I can’t get a more robust data solution up and running my project will be over before I really get started. I have tried including the SQL compact DLL files in my pproject and at run time Unity chokes on them complaining about native code.
Edit: webservice is no good. It needs tto be a local option. Imagine playing a game and needing to download every scrap of information lol. It would crappy. I appreciate your solutions though. I had considered those. Games can’t be really be relying on that for data support? Can they?
Hey shadow, you have as many options as .Net 2.0 offers as far as a standalone goes. You can use System.IO for standard file output, or you can use System.Xml if you need to use Xml. I’m sure Sql is available to you also, but, I’m not sure how you’d approach that.
Then I assume this is just for desktop?
Anything .Net related should be fine. there is plenty of stuff available specifically designed to hook .net and SQL together, so i’d be taking another look at that.
System.IO is available - so feel free to write your own file format (text or binary!)
Are you putting the dll’s in your ProgramFiles/Unity/Editor/ folder?
When I wrote the SQLite routines it spat the dummy if they weren’t included there.
Here is some database code I threw together using SQLite from various examples I found out there.
using UnityEngine;
using System.Collections;
using System;
using System.Data;
using System.Data.SQLite;
public class DatabaseControl : MonoBehaviour
{
// local variables
SQLiteConnection con = new SQLiteConnection("Data Source=gamedata.db;Version=3;New=False;Compress=True;");
SQLiteCommand cmd;
private string buildTableInfo;
private string sql;
// Use this for initialization
void Start ()
{
buildTableInfo = string.Empty;
sql = string.Empty;
StartCoroutine(SetupDatabase());
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.G))
{
StartCoroutine(ReadData("Male"));
}
if(Input.GetKeyUp(KeyCode.H))
{
StartCoroutine(ReadData("Female"));
}
if(Input.GetKeyUp(KeyCode.I))
{
StartCoroutine(WriteData("Steven","Male",1979));
}
}
IEnumerator SetupDatabase()
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "create table IF NOT EXISTS tblPEOPLE(NAME, SEX, DOB);";
cmd.ExecuteNonQuery();
// Test its creation or if it exists see row count
cmd.CommandText = "SELECT COUNT(*) FROM tblPEOPLE;";
int result = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
Debug.Log(result);
yield return new WaitForSeconds(0);
}
IEnumerator ReadData(String SEX)
{
con.Open();
SQLiteCommand readCommand = new SQLiteCommand("SELECT NAME,DOB FROM tblPEOPLE WHERE SEX=\"" + SEX+"\"",con);
SQLiteDataReader readData = readCommand.ExecuteReader();
string Name;
short DOB;
while (readData.Read())
{
Name = readData.GetString(0);
DOB = readData.GetInt16(1);
Debug.Log(Name + " - " + DOB);
}
con.Close();
yield return new WaitForSeconds(0);
}
IEnumerator WriteData(string NAME, string SEX,int DOB)
{
con.Open();
SQLiteCommand writeCommand = new SQLiteCommand("INSERT INTO tblPEOPLE VALUES(\"" + NAME + "\",\"" + SEX + "\","+DOB+")",con);
int linesAdded = writeCommand.ExecuteNonQuery();
Debug.Log(linesAdded);
con.Close();
yield return new WaitForSeconds(0);
}
void OnApplicationQuit()
{
// cleanup anything open
if (con != null)
{
con.Close();
con.Dispose();
}
if(cmd != null)
cmd.Dispose();
}
}
591454–21039–$SQLiteFiles.zip (694 KB)
Take my suggestion worth a grain of salt, since I’m somewhat noobish, but it seems like .txt (system.io) is the most space-effecient way to go. People here really seem to champion xml, but it’s kind of a space hog. As for you’re dialogues, couldn’t you simply create a class that contains all of the dialogues and reference them with an index number? If you save a dialog log, you can just list the order of the dialogue’s index in the file.
I’m doing a bit of IO in my project, and loading text data still doesn’t take much power. I read from a txt, store all of the data in arrays, and access it from there. The only real problem I can see with it is that it would be a giant pain to parse many multiple types of data whereas I hear xml can load classes and such.
XML is generally simple, human readable, and plenty of third party tools (even chrome!) can read/edit it.
It also has a lot of built in support.
And of course, it compresses well.
But creating your data-format is fine, and has various trade-offs, but that is why xml is generally spruiked on the forums.
XML isn’t very good at store related data. I designed my relational database as such, relational. And I would rather not have to parse large XML files and have them hanging around in memory, that’s slow to startup and it takes a lot of space once it’s in there. But if that is what people are doing, it’s really primitive, like using stone tools and bear skins…
@MrMetwurst, I’ll try SQLite, I was using SQLCe which is supported in .NET 2.0 When I included all my DLL files, Unity complains at RUNTIME not compile time. I fixed all the errors when I added all my DLL files and made references to them in the project so it would now how to compile it. Unity says that it cannot run native code (Just want to make that clear). I thought that I read some where that this is a limitation of Unity Free(?) Correct me if I am wrong. I’ll try adding them to ProgramFiles/Unity/Editor/ when I get home from work tonight and see if that fixes things. I have them inside of assets directory with everything my references pointing to the ones contained inside.
NOTE: sorry about some of the double letters on my words from previous posts, my “BetterKeyboard” on my Android does that and it’s too much of a pain to go back and fix. Don’t mistake stupid phone for stupid programmer
Hmm, yeah I don’t think it will work on Unity Free.
That could be your problem with trying to use SQL compact as well.
The native code error sounds like it would be caused by the Free version not allowing dll’s.
That’s a real shame. I don’t really have 1500 dollars to drop on a demo, especially one that might not get out of the testing phase. What a waste.
take a look at Siaqodb , it works on all platforms and for both Unity3D editions(Indie and Pro).
Hello,I’m making a RPG game on the Android and have some problems.In my game the NPCs will always follow the HERO.Multiple NPCs will form an irregular team advancing on the HERO.I used the “UnitySteer” and it work well at first.The problem is that when there are many NPCs because each NPC has its own CharacterController.it is too expensive to do the Collision Detection for CharacterController…I tried Rigidbody replaycing CharacterController but it will give a force to other NPC.
Do you know any solution to my problem?I want that crowd of NPCs follow the character and they should not overlap.Maybe I should write a script using some algorithm to control crowd of NPCs.Thank you^_^