Unity3d reading plist or XML files

Source: Internet
Author: User

This series of articles prepared by Aimar_johnny, welcome reprint, reproduced Please indicate the source, thank you.

http://blog.csdn.net/lzhq1982/article/details/12949827

The previous article introduced the cooperative asynchronous loading game scene, but the front desk did not work, wasted. Some games in the loading will be some of the game tips (tips) introduced to the players, I say here is how to achieve.

Speaking of small hints, in fact, there is a configuration file in the background, there are a lot of prompt statements, the program at a certain time interval random read a bar, displayed in the interface, very simple. Here comes the configuration file, the pattern is more, used to do cocos2d-x game, the most used is the plist file and CSV file, unity with the XML and Exel files, then I can use unity also read plist files and CSV file, of course, you can, This article I first read the plist file.

Actually reading the plist file is very simple, because the plist file is an XML file, so as long as the file extension to change it, the. plist to. xml, and then read the XML read it on the line, haha, don't squirt me. So I'll focus on how to read XML.

Let's take a look at my plist file contents:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>tips</key>
  6. <array>
  7. <string> re-challenge the passed level, you can also get coins </string>
  8. <string> A level does not pass? What you lack is just the rank of heroes and soldiers! </string>
  9. <string> Priority upgrade of Castle level, strength recovery speed, will bring unexpected effects </string>
  10. <string> Choose the right time to use hero skills or city defense props to increase your chances of winning </string>
  11. <string> As many soldiers as possible at once, maximizing your legion combat effectiveness </string>
  12. <string> The right time to use hero skills or city defense props can increase your chances of winning </string>
  13. <string> Earn a lot of diamonds, and when you are missing diamonds, take a look at the achievements list </string>
  14. <string> Each class is unique, not just for advanced classes to show Space </string>
  15. <string> When a continuous barrier fails, a tactical or troop-contributing approach can change </string>
  16. <string> Keeping soldiers consistent with enemy levels is a good idea </string>
  17. <string> Achievements to get a lot of diamonds, the lack of diamonds may wish to look at the list of achievements </string>
  18. <string> Want to see the status on the map in battle, just swipe left and right on the screen. </string>
  19. </Array>
  20. </dict>
  21. </plist>

This is the "space-time territory" of the loading interface to use tips, at that time with the plist, open with an XML file, in fact, is an XML, this is good to do, unity already has a formed interface to read XML. Let's start with the code.

[CSharp]View Plaincopy
  1. void LoadXml ()
  2. {
  3. string filePath = Application.datapath + @"/resource/settings/tips.xml";
  4. if (file.exists (FilePath)) {
  5. XmlDocument xmldoc = new XmlDocument ();
  6. Xmldoc.load (FilePath);
  7. XmlNodeList node = xmldoc.selectsinglenode ("plist").  ChildNodes;
  8. foreach (XmlElement nodeList in node) {
  9. foreach (XmlElement xe in nodeList) {
  10. if (XE. Name = = "Array") {
  11. int i = 0;
  12. _tips = new string[xe.  Childnodes.count];
  13. foreach (XmlElement xe1 in XE. ChildNodes) {
  14. Debug.Log (xe1. InnerText);
  15. _tips[i] = xe1. InnerText;
  16. i++;
  17. }
  18. Break ;
  19. }
  20. }
  21. }
  22. }
  23. }

Combined with the above configuration file, you will easily understand how to use it. Application.datapath is the Assets folder directory, first determine whether the file exists, if present, create a xmldocument,load load file, the root node is <plist> loop through <plist> Child nodes, here are only <dict> and then loop through <dict> 's child nodes, here are two, one is <key> and the other is <array> so judge whether the name is an array, Yes, create an array of strings, have arrays of so many elements of array, and then read each one from the array and save them in the array. We can then find it randomly in the array. This code is called in start and is read only once. Remember, do not try to always query external files, so the efficiency will be very low, early cache up is a good choice.

After reading, I want to load the loading program, I use the Ngui, in the bottom there layout a label, not clear how the layout of See me this article: Unity3d study notes (eight)--ngui production game interface, here are as follows:

Where label to use Chinese font, do not know how to create a Chinese font to see me this article: Unity3d study notes (10)--ngui make Chinese fonts.

At the end of the program, the cache string is read at intervals.

[CSharp]View Plaincopy
  1. Private string [] _tips;
  2. Public UILabel _tipslabel;
  3. Private float _lasttime = 0.0f;
  4. Private const int wait_time = 2;
  5. void Start () {
  6. LOADXML ();
  7. if (_tips ! = null && _tips. Length > 0) {
  8. int idx = random.range (0, _tips.  LENGTH-1);
  9. _tipslabel.text = _tips[idx];
  10. }
  11. Startcoroutine (Loadscene ());
  12. }
  13. Update is called once per frame
  14. void Update () {
  15. if (_tips = = Null | | _tips. Length <= 0)
  16. return;
  17. if (time.time-_lasttime >= wait_time) {
  18. _lasttime = Time.time;
  19. int idx = random.range (0, _tips.  LENGTH-1);
  20. _tipslabel.text = _tips[idx];
  21. }
  22. }


So the 2-second interface shows tips, and our loading interface's collaborative asynchrony finally makes sense. Last last picture.

Unity3d reading plist or XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.