A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace UnityEditor.ShaderGraph.Internal
5{
6 [Serializable]
7 public sealed class SerializableTexture : ISerializationCallbackReceiver
8 {
9 [SerializeField]
10 string m_SerializedTexture;
11
12 [SerializeField]
13 string m_Guid;
14
15 [NonSerialized]
16 Texture m_Texture;
17
18 [Serializable]
19 class TextureHelper
20 {
21#pragma warning disable 649
22 public Texture texture;
23#pragma warning restore 649
24 }
25
26 // used to get a Texture ref guid without loading the texture 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 texture;
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.texture.guid))
50 return textureHelper.texture.guid;
51 }
52 if (!string.IsNullOrEmpty(m_Guid))
53 {
54 return m_Guid;
55 }
56 if (m_Texture != null)
57 {
58 if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(m_Texture, out string guid, out long localId))
59 return guid;
60 }
61 return null;
62 }
63 }
64
65 public Texture texture
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_Texture = textureHelper.texture;
76 }
77 else if (!string.IsNullOrEmpty(m_Guid) && m_Texture == null)
78 {
79 m_Texture = AssetDatabase.LoadAssetAtPath<Texture>(AssetDatabase.GUIDToAssetPath(m_Guid));
80 m_Guid = null;
81 }
82
83 return m_Texture;
84 }
85 set
86 {
87 m_Texture = value;
88 m_Guid = null;
89 m_SerializedTexture = null;
90 }
91 }
92
93 public void OnBeforeSerialize()
94 {
95 var textureHelper = texture ? new TextureHelper { texture = texture } : null;
96 m_SerializedTexture = EditorJsonUtility.ToJson(textureHelper, false);
97 }
98
99 public void OnAfterDeserialize()
100 {
101 }
102 }
103}