this's a book and that's ais this dictionaryy.哪里有错误

& March 23, 2010Posted in: , ,
In this part of the tutorial we’ll have a look at how to load the some book information in the JSON format into Unity. For good comparison between JSON and XML, this file will describe the same books and uses roughly the same structure. Take a look at how that looks:
&title&:&Papervision3D Essentials&,
&image&:&http:\/\/\/files\/2010\/UnityExternalJSONXML\/Papervision3DEssentials.jpg&,
&authors&:
&author&:[
{&id&:&1&,&name&:&Paul Tondeur&},
{&id&:&2&,&name&:&Jeff Winder&}
&description&:&&p&Create interactive &b&Papervision3D&\/b& applications with stunning effects and powerful animations&\/p&&
&title&:&Unity Game Development Essentials&,
&image&:&http:\/\/\/files\/2010\/UnityExternalJSONXML\/UnityGameDevelopmentEssentials.jpg&,
&authors&:
&author&:[
{&id&:&3&,&name&:&Will Goldstone&}
&description&:&&p&Build fully functional, professional 3D games with realistic environments, sound, &i&dynamic effects&\/i&, and more!&\/p&&
This file can also be found on
and will be used later on in this tutorial.
JSON isn’t as integrated in C# development as XML is. Furtunatly the
lists external libraries that are available for a variety of languages. Including a couple C# projects. After having a glance at all, I found out that I liked
the most. It’s usage is quite straight forward. The source of this project can be found on found on SVN. After checking this project out from SVN, you’ll end up with a couple of classes and make files. From these files, a dll should be created for use in Unity. This might sound a bit frightening when you’re new to C Sharp development. Therefore
for you (also included in the download at the end of this article). You just need to copy this into the asset folder of your Unity project and from then on you’ll have access to all classes inside of this dll. In order to use them in your scripts, you need to set the using directive on top of your class to:
using UnityE
Just like with XML, we can load the JSON file by using the WWW class.
WWW www = new WWW(&/files/2010/UnityExternalJSONXML/books.json&);
Once the data is loaded from the provided URL, we need to convert the loaded string into an object of the JsonData type. We can use a static method of the JsonMapper class to achieve this:
JsonData jsonBooks = JsonMapper.ToObject(jsonString);
Now we have the string converted into an object, we can easily access the objects that have been defined in the JSON file. So let’s say we want access the book object and show how many books are nested:
Debug.Log(jsonBooks[&book&].Count);
That looks easy, right? jsonBooks["book"] holds an array of books, so in order to loop through all books, we can use a simple for loop:
for(int i = 0; i&jsonBooks[&book&].C i++)
In this example, it will loop through the two books. While looping through them, we can print the book title on the screen:
Debug.Log(jsonBooks[&book&][i][&title&].ToString());
Notice the ToString() method call at the end of the selected object. This will convert the object into a string. C# is more strict with this as compared to ActionScript, so never forget to call this to prevent errors.
Now let’s say you want to access the book ID and treat it as in integer. You first need to convert the object to a string and then convert it to an integer.
Debug.Log(Convert.ToInt16(jsonBooks[&book&][i][&id&].ToString()));
Nested inside an book object are the authors. Looping through them is quite easy, as we can you access these nested elements. Look at how to loop through the authors and print their names on the screen:
for(int j = 0; j&jsonBooks[&book&][i][&authors&][&author&].C j++)
Debug.Log(jsonBooks[&book&][i][&authors&][&author&][j][&name&].ToString());
I’ve recreated the same example as used in part 1 of this tutorial, only this time with a JSON file as the data source. The complete source can be found in the download, however have a look at the following code to see how LitJSON could be used in production:
using UnityE
using LitJ
using System.C
public class LoadJSON : MonoBehaviour
IEnumerator Start()
//Load JSON data from a URL
string url = &/files/2010/UnityExternalJSONXML/books.json&;
WWW www = new WWW(url);
//Load the data and yield (wait) till it's ready before we continue executing the rest of this method.
if (www.error == null)
//Sucessfully loaded the JSON string
Debug.Log(&Loaded following JSON string& + www.data);
//Process books found in JSON file
ProcessBooks(www.data);
Debug.Log(&ERROR: & + www.error);
//Converts a JSON string into Book objects and shows a book out of it on the screen
private void ProcessBooks(string jsonString)
JsonData jsonBooks = JsonMapper.ToObject(jsonString);
for(int i = 0; i&jsonBooks[&book&].C i++)
book = new Book();
book.id = Convert.ToInt16(jsonBooks[&book&][i][&id&].ToString());
book.title = jsonBooks[&book&][i][&title&].ToString();
book.image = jsonBooks[&book&][i][&image&].ToString();
book.description = jsonBooks[&book&][i][&description&].ToString();
book.authors = new ArrayList();
for(int j = 0; j&jsonBooks[&book&][i][&authors&][&author&].C j++)
book.authors.Add(jsonBooks[&book&][i][&authors&][&author&][j][&name&].ToString());
LoadBook(book);
//Finds book object in application and send the Book as parameter.
//Currently only works with two books
private void LoadBook(Book book)
GameObject bookGameObject = GameObject.Find(&Book& + book.id.ToString());
bookGameObject.SendMessage(&LoadBook&, book);
This gives an idea how to handle JSON files in Unity.
Just like with XML this comes with a disadvantage in terms of filesize. It’s size footprint to the Unity file is about equal to XML: +/- 850kb.
Conclusion
I think it’s quite hard to say which file format is better. Both XML and JSON add about the same amount of kilobytes to the Unity file. I’ve not tested which of these is more efficient performance wise. It might be worth checking that out before you’ll be working with large amounts of data. In case that doesn’t matter, I think it’s just a matter of personal taste. I think I kind of prefer to use JSON, as this reminds me of handling XML files with ActionScript in the AS2 days.
But give it a try yourself and see what works best for you.
Downloads / Examples
18 Comments
Comments Closed
Recent Posts

我要回帖

更多关于 this that 的文章

 

随机推荐