暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializableTexture.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.ShaderGraph.Internal
  4. {
  5. [Serializable]
  6. public sealed class SerializableTexture : ISerializationCallbackReceiver
  7. {
  8. [SerializeField]
  9. string m_SerializedTexture;
  10. [SerializeField]
  11. string m_Guid;
  12. [NonSerialized]
  13. Texture m_Texture;
  14. [Serializable]
  15. class TextureHelper
  16. {
  17. #pragma warning disable 649
  18. public Texture texture;
  19. #pragma warning restore 649
  20. }
  21. // used to get a Texture ref guid without loading the texture asset itself into memory
  22. [Serializable]
  23. class MinimalTextureHelper
  24. {
  25. // these variables are only ever populated by serialization, disable the C# warning that checks if they are ever assigned
  26. #pragma warning disable 0649
  27. [Serializable]
  28. public struct MinimalTextureRef
  29. {
  30. public string guid;
  31. }
  32. public MinimalTextureRef texture;
  33. #pragma warning restore 0649
  34. }
  35. internal string guid
  36. {
  37. get
  38. {
  39. if (!string.IsNullOrEmpty(m_SerializedTexture))
  40. {
  41. var textureHelper = new MinimalTextureHelper();
  42. EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, textureHelper);
  43. if (!string.IsNullOrEmpty(textureHelper.texture.guid))
  44. return textureHelper.texture.guid;
  45. }
  46. if (!string.IsNullOrEmpty(m_Guid))
  47. {
  48. return m_Guid;
  49. }
  50. if (m_Texture != null)
  51. {
  52. if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(m_Texture, out string guid, out long localId))
  53. return guid;
  54. }
  55. return null;
  56. }
  57. }
  58. public Texture texture
  59. {
  60. get
  61. {
  62. if (!string.IsNullOrEmpty(m_SerializedTexture))
  63. {
  64. var textureHelper = new TextureHelper();
  65. EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, textureHelper);
  66. m_SerializedTexture = null;
  67. m_Guid = null;
  68. m_Texture = textureHelper.texture;
  69. }
  70. else if (!string.IsNullOrEmpty(m_Guid) && m_Texture == null)
  71. {
  72. m_Texture = AssetDatabase.LoadAssetAtPath<Texture>(AssetDatabase.GUIDToAssetPath(m_Guid));
  73. m_Guid = null;
  74. }
  75. return m_Texture;
  76. }
  77. set
  78. {
  79. m_Texture = value;
  80. m_Guid = null;
  81. m_SerializedTexture = null;
  82. }
  83. }
  84. public void OnBeforeSerialize()
  85. {
  86. m_SerializedTexture = EditorJsonUtility.ToJson(new TextureHelper { texture = texture }, false);
  87. }
  88. public void OnAfterDeserialize()
  89. {
  90. }
  91. }
  92. }