SerializableMesh.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.ShaderGraph
  4. {
  5. [Serializable]
  6. class SerializableMesh : ISerializationCallbackReceiver
  7. {
  8. [SerializeField]
  9. string m_SerializedMesh;
  10. [SerializeField]
  11. string m_Guid;
  12. [NonSerialized]
  13. Mesh m_Mesh;
  14. [Serializable]
  15. class MeshHelper
  16. {
  17. #pragma warning disable 649
  18. public Mesh mesh;
  19. #pragma warning restore 649
  20. }
  21. public Mesh mesh
  22. {
  23. get
  24. {
  25. if (!string.IsNullOrEmpty(m_SerializedMesh))
  26. {
  27. var textureHelper = new MeshHelper();
  28. EditorJsonUtility.FromJsonOverwrite(m_SerializedMesh, textureHelper);
  29. m_SerializedMesh = null;
  30. m_Guid = null;
  31. m_Mesh = textureHelper.mesh;
  32. }
  33. else if (!string.IsNullOrEmpty(m_Guid) && m_Mesh == null)
  34. {
  35. m_Mesh = AssetDatabase.LoadAssetAtPath<Mesh>(AssetDatabase.GUIDToAssetPath(m_Guid));
  36. m_Guid = null;
  37. }
  38. return m_Mesh;
  39. }
  40. set
  41. {
  42. m_Mesh = value;
  43. m_Guid = null;
  44. m_SerializedMesh = null;
  45. }
  46. }
  47. public void OnBeforeSerialize()
  48. {
  49. m_SerializedMesh = EditorJsonUtility.ToJson(new MeshHelper { mesh = mesh }, false);
  50. }
  51. public void OnAfterDeserialize()
  52. {
  53. }
  54. }
  55. }