• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ryzokuken · Mar 26, 2015 at 01:55 AM · c#unity 5databasedata

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?


QUESTION: 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[i].name);
     PlayerPrefs.SetInt(i.ToString()+"id", bookslist[i].id);
     ...
     <blah blah>
 }


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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hrungdak · Mar 26, 2015 at 07:06 AM

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[i].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:

 <Book>
   <Name>BookName</Name>
   <Id>123</Id>
   <ISBN>bookISBN</ISBN>
 </Book>

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;
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by speedocreedo · Mar 27, 2015 at 06:21 AM

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: https://www.assetstore.unity3d.com/en/#!/content/24239

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A real head-scratcher 0 Answers

SocketException: No connection could be made because the target machine actively refused it. 0 Answers

Importing large amounts of data 0 Answers

How to get specific data from CURL in unity 0 Answers

Bool condition is always resulting in false even when it seems impossible to be false even using debug lines to check 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges