No Description
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.

SerializableCubemap.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // used to get a Cubemap ref guid without loading the cubemap asset itself into memory
  22. [Serializable]
  23. class MinimalCubemapHelper
  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 cubemap;
  33. #pragma warning restore 0649
  34. }
  35. internal string guid
  36. {
  37. get
  38. {
  39. if (!string.IsNullOrEmpty(m_SerializedCubemap))
  40. {
  41. var textureHelper = new MinimalCubemapHelper();
  42. EditorJsonUtility.FromJsonOverwrite(m_SerializedCubemap, textureHelper);
  43. if (!string.IsNullOrEmpty(textureHelper.cubemap.guid))
  44. return textureHelper.cubemap.guid;
  45. }
  46. if (!string.IsNullOrEmpty(m_Guid))
  47. {
  48. return m_Guid;
  49. }
  50. if (m_Cubemap != null)
  51. {
  52. if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(m_Cubemap, out string guid, out long localId))
  53. return guid;
  54. }
  55. return null;
  56. }
  57. }
  58. public Cubemap cubemap
  59. {
  60. get
  61. {
  62. if (!string.IsNullOrEmpty(m_SerializedCubemap))
  63. {
  64. var textureHelper = new CubemapHelper();
  65. EditorJsonUtility.FromJsonOverwrite(m_SerializedCubemap, textureHelper);
  66. m_SerializedCubemap = null;
  67. m_Guid = null;
  68. m_Cubemap = textureHelper.cubemap;
  69. }
  70. else if (!string.IsNullOrEmpty(m_Guid) && m_Cubemap == null)
  71. {
  72. m_Cubemap = AssetDatabase.LoadAssetAtPath<Cubemap>(AssetDatabase.GUIDToAssetPath(m_Guid));
  73. m_Guid = null;
  74. }
  75. return m_Cubemap;
  76. }
  77. set
  78. {
  79. m_Cubemap = value;
  80. m_Guid = null;
  81. m_SerializedCubemap = null;
  82. }
  83. }
  84. public void OnBeforeSerialize()
  85. {
  86. m_SerializedCubemap = EditorJsonUtility.ToJson(new CubemapHelper { cubemap = cubemap }, false);
  87. }
  88. public void OnAfterDeserialize()
  89. {
  90. }
  91. }
  92. }