-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSerializableMonoScript.cs
More file actions
165 lines (161 loc) · 6.56 KB
/
SerializableMonoScript.cs
File metadata and controls
165 lines (161 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#region License and Information
/*****
* Provides a serializable wrapper for Unity's MonoScript class which is an
* Editor-only class. It allows you to drag-and-drop actual script assets
* onto a field. The generic version allows for filtering for certain types
* It does support MonoBehaviour derived as well as ScriptableObject MonoScript
* types. The generic version also provides two methods to either add the
* represented type as component to a given gameobject (AddToGameObject) or
* to create a scriptable object instance of that class (CreateSOInstance)
*
* The generic argument does support filtering for interfaces as well.
* Note: Assigned types are NOT refactoring safe. The actual type is stored as
* and Assembly qualified type name. So be careful when refactoring.
*
* Copyright (c) 2023 Bunny83
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*****/
#endregion License and Information
using UnityEngine;
namespace B83
{
[System.Serializable]
public class SerializableMonoScript
{
System.Type m_Type = null;
[SerializeField]
string m_TypeName = null;
public System.Type Type
{
get
{
if (m_Type == null)
{
if (string.IsNullOrEmpty(m_TypeName))
return null;
m_Type = System.Type.GetType(m_TypeName);
}
return m_Type;
}
set
{
m_Type = value;
if (m_Type == null)
m_TypeName = "";
else
m_TypeName = m_Type.AssemblyQualifiedName;
}
}
}
[System.Serializable]
public class SerializableMonoScript<T> : SerializableMonoScript where T : class
{
public T CreateSOInstance()
{
var type = Type;
if (type != null)
return (T)(object)ScriptableObject.CreateInstance(type);
return default(T);
}
public T AddToGameObject(GameObject aGO)
{
var type = Type;
if (type != null)
return (T)(object)aGO.AddComponent(type);
return default(T);
}
}
#if UNITY_EDITOR
namespace PropertyDrawers
{
using System.Collections.Generic;
using UnityEditor;
[CustomPropertyDrawer(typeof(SerializableMonoScript),true)]
public class SerializableTypePropertyDrawer : PropertyDrawer
{
private System.Type m_FilterType = null;
private static Dictionary<System.Type, MonoScript> m_MonoScriptCache = new Dictionary<System.Type, MonoScript>();
private static MonoScript GetMonoScript(System.Type aType)
{
if (aType == null)
return null;
if (m_MonoScriptCache.TryGetValue(aType, out MonoScript script) && script != null)
{
return script;
}
var scripts = Resources.FindObjectsOfTypeAll<MonoScript>();
foreach(var s in scripts)
{
var type = s.GetClass();
if (type != null)
m_MonoScriptCache[type] = s;
if (type == aType)
script = s;
}
return script;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (m_FilterType == null)
{
System.Type fieldType = fieldInfo.FieldType;
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
// when used in a List<>, grab the actual type from the generic argument of the List
fieldType = fieldInfo.FieldType.GetGenericArguments()[0];
}
else if (fieldType.IsArray)
{
// when used in an array, grab the actual type from the element type.
fieldType = fieldType.GetElementType();
}
if (fieldType.IsGenericType)
{
var types = fieldType.GetGenericArguments();
if (types != null && types.Length == 1)
m_FilterType = types[0];
}
else
m_FilterType = typeof(UnityEngine.Object);
}
var typeName = property.FindPropertyRelative("m_TypeName");
System.Type type = System.Type.GetType(typeName.stringValue);
MonoScript monoScript = GetMonoScript(type);
EditorGUI.BeginChangeCheck();
monoScript = (MonoScript)EditorGUI.ObjectField(position, label, monoScript, typeof(MonoScript), true);
if (EditorGUI.EndChangeCheck())
{
if (monoScript == null)
typeName.stringValue = "";
else
{
var newType = monoScript.GetClass();
if (newType != null && m_FilterType.IsAssignableFrom(newType))
typeName.stringValue = newType.AssemblyQualifiedName;
else
Debug.LogWarning("Dropped type does not derive or implement " + m_FilterType.Name);
}
}
}
}
}
#endif
}