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