Use a simple Database...

I am really sorry guys, if the use of unity for anything else than game-making pisses you off.

  • STORY:
    Our programming teacher had asked us to make a program for library management, and the deadline was coming really close. On the other hand, I was looking for something to do with unity, and suddenly it struck me, why don’t I… well, you know what happened. Don’t you?

So, thanks to the new unity UI system, I made a decent UI system, and although I did not know how to use the UI.Button, I used OnGUI Button for that!

The main question arises, I want the ARRAYS (OR LISTS PREFERRABLY [i meant System.Collections.Generic.List <>]) OF TWO OF MY BASECLASSES (BASEBOOK and BASESTUDENT) to be saved on a database on the disk, where I can load it when I come back! (what use is a library manager if you need to create the records everytime you open the program?)

I thought PlayerPrefs could be used in this way, tell me if my approach is correct, or if there is a simpler way.

// Save Program
PlayerPrefs.SetInt ("length", bookslist.length);
for (int i=0;i<bookslist.length;i++) {
    PlayerPrefs.SetString(i.ToString() + "name", bookslist*.name);*

PlayerPrefs.SetInt(i.ToString()+“id”, bookslist*.id);*

And use GetInt to get length and iterate over length to get the new objects to generate the list of objects.
Please tell me if there is a better or easier way. Thanks in advance!

You can store a complete book in one string if you use XML to describe it. That may look like this:

PlayerPrefs.SetString(i.ToString(), bookslist*.ToXml());*

And the ToXml() in the books class:
public string ToXml()
XElement root = new XElement(“Book”);
root.Add(new XElement(“Name”, book.Name));
root.Add(new XElement(“Id”, book.Id.ToString()));
root.Add(new XElement(“ISBN”, book.ISBN.ToString()));

// …

return root.ToString();
This gives you a string like:


that you can save in PlayerPrefs. So you have one line for the complete book description. To get a book object from xml you go this way:
public BookObject FromXml(string xmlString)
XDocument doc = XDocument.Parse(xmlBook);
BookObject book = new BookObject();
book.Name = doc.Element(“Name”).Value();
book.Id = (int)(doc.Element(“Id”).Value());
book.ISBN = doc.Element(“ISBN”).Value();

// …

return book;

If you don’t mind using 3rd party plugins, you may want to look at the Game Data Editor from the Unity Assets Store. It’s got a free and pro version and it’s been a great time saver for managing my projects data.

Not sure if it is what you are looking for as it stores data in a text file and you can also import data into it using excel files but i guess it’s worth a look. Here’s the link to its free version: Unity Asset Store - The Best Assets for Game Making