UnityEssentials
Small but useful tools and features for Unity
TypeSerializable.cs
Go to the documentation of this file.
1 // Written by Bryan Keiren (http://www.bryankeiren.com)
2 // Found in: https://forum.unity.com/threads/serializable-system-type-get-it-while-its-hot.187557/
3 
4 
5 namespace UnityEngine
6 {
10  [System.Serializable]
11  public class TypeSerializable
12  {
16  [SerializeField]
17  private string m_Name;
21  public string Name => m_Name;
22 
26  [SerializeField]
27  private string m_AssemblyQualifiedName;
32 
36  [SerializeField]
37  private string m_AssemblyName;
41  public string AssemblyName => m_AssemblyName;
42 
46  private System.Type m_SystemType;
50  public System.Type SystemType
51  {
52  get
53  {
54  if (m_SystemType == null)
55  {
56  GetSystemType();
57  }
58  return m_SystemType;
59  }
60  }
61 
62  private void GetSystemType()
63  {
64  m_SystemType = System.Type.GetType(m_AssemblyQualifiedName);
65  }
66 
67  public TypeSerializable( System.Type _SystemType )
68  {
69  m_SystemType = _SystemType;
70  m_Name = _SystemType.Name;
71  m_AssemblyQualifiedName = _SystemType.AssemblyQualifiedName;
72  m_AssemblyName = _SystemType.Assembly.FullName;
73  }
74 
75  public override bool Equals( System.Object obj )
76  {
77  TypeSerializable temp = obj as TypeSerializable;
78  if ((object)temp == null)
79  {
80  return false;
81  }
82  return this.Equals(temp);
83  }
84 
85  public bool Equals( TypeSerializable _Object )
86  {
87  //return m_AssemblyQualifiedName.Equals(_Object.m_AssemblyQualifiedName);
88  return _Object.SystemType.Equals(SystemType);
89  }
90 
91  public static bool operator ==( TypeSerializable a, TypeSerializable b )
92  {
93  // If both are null, or both are same instance, return true.
94  if (System.Object.ReferenceEquals(a, b))
95  {
96  return true;
97  }
98 
99  // If one is null, but not both, return false.
100  if (((object)a == null) || ((object)b == null))
101  {
102  return false;
103  }
104 
105  return a.Equals(b);
106  }
107 
108  public static bool operator !=( TypeSerializable a, TypeSerializable b )
109  {
110  return !(a == b);
111  }
112 
113  public override int GetHashCode()
114  {
115  return SystemType != null ? SystemType.GetHashCode() : 0;
116  }
117 
118  }
119 }
Simple helper class that allows you to serialize System.Type objects
string m_AssemblyQualifiedName
The assembly-qualified name of the type, which includes the name of the assembly from which the Type ...
string m_AssemblyName
The display name of the assembly of the type.
TypeSerializable(System.Type _SystemType)
string Name
The name of the type.
override bool Equals(System.Object obj)
static bool operator!=(TypeSerializable a, TypeSerializable b)
string AssemblyQualifiedName
The assembly-qualified name of the type, which includes the name of the assembly from which the Type ...
bool Equals(TypeSerializable _Object)
System.Type SystemType
The type.
string m_Name
The name of the type.
System.Type m_SystemType
The type.
string AssemblyName
The display name of the assembly of the type.
static bool operator==(TypeSerializable a, TypeSerializable b)