TeletubProd

xml reader

Aug 8th, 2022
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. using System.IO;
  7.  
  8. public class Lang
  9. {
  10.     [XmlAttribute("id")]
  11.     public int id;
  12.     [XmlAttribute("txt")]
  13.     public string txt;
  14. }
  15.  
  16. public class Quip
  17. {
  18.     [XmlAttribute("id")]
  19.     public int id;
  20.     [XmlAttribute("txt")]
  21.     public string txt;
  22.  
  23.     [XmlAttribute("death")]
  24.     public int death = 0;
  25.     [XmlAttribute("wpn")]
  26.     public int wpn = -1;
  27.     [XmlAttribute("acc")]
  28.     public int acc = -1;
  29. }
  30.  
  31. [XmlRoot("langues")]
  32. public class LANG
  33. {
  34.     [XmlElement("Language")]
  35.     public Language items;
  36. }
  37.  
  38. public class Language
  39. {
  40.     [XmlElement("lang")] public List<Lang> langs = new List<Lang>();
  41.    
  42.     [XmlArray("Quips"), XmlArrayItem("quip")]
  43.     public List<Quip> quips = new List<Quip>();
  44.  
  45.     public static Language Load(string path)
  46.     {
  47.         TextAsset _xml = Resources.Load<TextAsset>(path);
  48.  
  49.         XmlSerializer serializer = new XmlSerializer(typeof(LANG));
  50.         StringReader reader = new StringReader(_xml.text);
  51.  
  52.         LANG lan = serializer.Deserialize(reader) as LANG;
  53.  
  54.         Debug.Log(lan.items.langs.Count);
  55.         reader.Close();
  56.  
  57.         return lan.items;
  58.     }
  59.  
  60.     public static string TXT(int ID)
  61.     {
  62.         string path = "language";
  63.         Language lang = Language.Load(path);
  64.  
  65.         int count = lang.langs.Count;
  66.  
  67.         if(count == 0 || ID > count-1) return null;
  68.         return lang.langs[ID].txt;
  69.     }
  70.  
  71.     public static Quip[] QUIP()
  72.     {
  73.         string path = "language";
  74.         Language lang = Language.Load(path);
  75.  
  76.         return lang.quips.ToArray();
  77.     }
  78. }
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment