Tutorial: Loading and parsing external XML and JSON files with Unity
2015-10-19 12:28
495 查看
原文:http://blog.paultondeur.com/2010/03/23/tutorial-loading-and-parsing-external-xml-and-json-files-with-unity-part-1-xml/
Tutorial:
Loading and parsing external XML and JSON files with Unity – Part 1: XML
As being a Flash Developer for many years, I’m very used to just quickly loading and parsing an external XML file. Although this is possible with Unity, I found out there’s very limited information about this subject available in a Unity context.While investigating this subject a while ago, I also decided to look into the JSON format as a data container, to see which works best for me. Naturally I’d prefer to use XML over JSON, but it turns out that both comes with their cons and pros, which makes
it hard to say which works best.
In this two part tutorial I’ll show how you could load an XML or JSON file into Unity and parse it’s information. Both the XML file and JSON file are containers for the same information, which makes it easier to compare the differences between the two.
XML
The XML file I’ve created contains a couple of books. In order to cover different ways to work with XML, it contains a variety of data, like attributes, regular nodes and childnodes, integers, strings and CDATA blocks.
will be loaded later on in this tutorial.
XML is not new to C#. In fact, it’s quite integrated within the language. When you create a new application in Visual Studio, you have access to the System.Xml namespace by default. Within this namespace, lots
of classes can be found for creating, reading and modifying XML documents. Accessing these classes in Unity however, doesn’t work out of the box. This is because this namespace isn’t available within Unity. The solution for this is relatively simple; We just
need to reference the System.Xml dll in Unity. As Unity is build upon the Mono
Framework, this file is provided with your local Unity install. You can find these files at the following locations, depending on the machine you’re running on:
MAC: Go to /Applications/Unity/ and right-click on Unity.app -> Show Package Contents. Then browse to “Contents/Frameworks/Mono.framework/”. In this folder you’ll find lot’s of dll files. In this case, just copy System.Xml.dll
Windows: Go to C:\Program Files\Unity\Editor\Data\Frameworks\Mono.framework and copy System.Xml.dll from here.
The next thing to do is paste the dll into your assets folder of a Unity project. Next time you compile this project, the System.Xml namespace is available to you from within the Unity player.
To load any external content from a webserver with Unity, we can use the WWW class. (can be compared to a URLRequest in Flash).
expression. In our case we want to select all books, which are nested inside the books element. Selecting nodes returns an XmlNodeList, which will be used to loop through all results. Take a look at how to select all book nodes and loop through them:
download at the bottom of this article. But the class that handles loading and parsing the XML looks as follows:
was about an 850kb difference between having an empty Unity application and an application that is using the System.Xml dll.
In the next part of this tutorial I’ll demonstrate how you could achieve the same results with a JSON file as data container.
Download the source files
for this complete tutorial (part1 and part2) here.
A working demonstration of this example
with XML can be found here.
Continue
reading this tutorial in part 2 about loading and parsing a JSON file.
Tutorial: Loading and parsing external XML and JSON files with Unity – Part 2: JSON
原文:http://blog.paultondeur.com/2010/03/23/tutorial-loading-and-parsing-external-xml-and-json-files-with-unity-part-2-json/
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 lookat how that looks:
will be used later on in this tutorial.
JSON isn’t as integrated in C# development as XML is. Furtunatly the JSON documentation website 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 LitJSON 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 I’ve
already created this dll 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:
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.
in production:
live example of this can be viewed here.
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
Download XML and JSON
example files
Download LitJSON dll file
View XML Example
View JSON Example
Go
to part 1 of this tutorial
相关文章推荐
- Unity3D——初探认识
- Unity Shader 顶点和片段着色器(Vertex and Fragment Shader)
- unity脚本运行顺序具体的解释
- unity3d 有限状态机
- 四元数介绍
- 3D四元数与欧拉角的转换
- 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)
- 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)
- Unity3D-移动
- UNITY 5.2 Component类
- 理解Unity3d的ForceMode | Understanding ForceMode in Unity3D
- Unity基础小案例---射击小球
- unity, windows: Unhandled Exception: System.UnauthorizedAccessException: Access to the path "XXX\Temp\Assembly-CSharp.dll.mdb" is denied
- unity 右键关闭
- unity3d仿仙剑角色控制
- unity3d游戏物体跟着鼠标方向移动
- UNITY 5.2 Object类
- Unity3D 游戏的碰撞
- Unity3D 创建一个简单的2D游戏
- Unity Assets目录下的特殊文件夹名称