SerializableCubemap.cs 1.6 KB

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