MaterialFile.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. namespace UnitEditor
  8. {
  9. public class MaterialFile
  10. {
  11. private JObject m_root;
  12. private List<string> m_materials;
  13. public MaterialFile (string mat_file_name)
  14. {
  15. string json_string = string.Empty;
  16. using (StreamReader streamReader = new StreamReader(mat_file_name))
  17. {
  18. json_string = streamReader.ReadToEnd();
  19. m_root = JObject.Parse (json_string);
  20. }
  21. deserialize ();
  22. }
  23. public string[] materials()
  24. {
  25. return m_materials.ToArray ();
  26. }
  27. public void deserialize()
  28. {
  29. JToken renderables_token = m_root ["texture_layers"];
  30. m_materials = JsonConvert.DeserializeObject<List<string>>(renderables_token.ToString());
  31. }
  32. public void serialize()
  33. {
  34. string json_string = "{\n";
  35. json_string += "\"texture_layers\": [\n";
  36. string last = m_materials.Last();
  37. foreach (string m in m_materials)
  38. {
  39. json_string += "\"" + m + "\"\n";
  40. if (m != last) json_string += ",";
  41. }
  42. json_string += "]\n";
  43. json_string += "}";
  44. Console.Write (json_string);
  45. }
  46. }
  47. }