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