SerializableTextureArray.cs 1.7 KB

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