SkinningSerializerXML.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. internal class SkinningXMLSerializer : ISkinningSerializer
  9. {
  10. public bool CanDeserialize(string data)
  11. {
  12. bool result = false;
  13. try
  14. {
  15. using (TextReader textReader = new StringReader(data))
  16. {
  17. using (XmlTextReader xmlReader = new XmlTextReader(textReader))
  18. {
  19. var xmlSerializer = new XmlSerializer(typeof(List<UnityEngine.U2D.SpriteBone>));
  20. result = xmlSerializer.CanDeserialize(xmlReader);
  21. }
  22. }
  23. }
  24. catch
  25. {
  26. }
  27. return result;
  28. }
  29. public SkinningCopyData Deserialize(string data)
  30. {
  31. SkinningCopyData skinningCopyData;
  32. using (TextReader textReader = new StringReader(data))
  33. {
  34. using (XmlTextReader xmlReader = new XmlTextReader(textReader))
  35. {
  36. var xmlSerializer = new XmlSerializer(typeof(SkinningCopyData));
  37. if (!xmlSerializer.CanDeserialize(xmlReader))
  38. {
  39. throw new InvalidOperationException(
  40. "Unable to deserialize system copy buffer to Skinning Copy Data");
  41. }
  42. skinningCopyData = (SkinningCopyData)xmlSerializer.Deserialize(xmlReader);
  43. }
  44. }
  45. return skinningCopyData;
  46. }
  47. public string Serialize(SkinningCopyData skinningData)
  48. {
  49. XmlSerializer xmlSerializer = new XmlSerializer(skinningData.GetType());
  50. using (StringWriter textWriter = new StringWriter())
  51. {
  52. xmlSerializer.Serialize(textWriter, skinningData);
  53. return textWriter.ToString();
  54. }
  55. }
  56. }
  57. }