-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptReference.cs
More file actions
120 lines (99 loc) · 3 KB
/
Copy pathScriptReference.cs
File metadata and controls
120 lines (99 loc) · 3 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
using System;
using System.Linq;
using System.Reflection;
namespace Minerva.Module
{
/// <summary>
/// class that refer to an custom script, usable outside editor
/// </summary>
[Serializable]
public class ScriptReference
{
#if UNITY_EDITOR
public UnityEditor.MonoScript script;
#endif
public string fullName;
public string assemblyName;
public virtual Type Type => typeof(UnityEngine.Object);
public ScriptReference() { }
public ScriptReference(Type type)
{
SetClass(type);
}
public Type GetClass()
{
return TryResolve(out var type) ? type : null;
}
public void SetClass(Type type)
{
if (type == null)
{
fullName = string.Empty;
assemblyName = string.Empty;
}
else
{
fullName = type.FullName;
assemblyName = type.Assembly.GetName().Name;
}
}
public bool TryResolve(out Type type)
{
type = null;
var asm = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName, StringComparison.Ordinal));
if (asm != null)
type = asm.GetType(fullName, throwOnError: false);
if (type == null)
type = Type.GetType($"{fullName}, {assemblyName}", throwOnError: false);
if (type == null)
{
try
{
var loaded = Assembly.Load(new AssemblyName(assemblyName));
type = loaded.GetType(fullName, throwOnError: false);
}
catch { }
}
return type != null;
}
public static implicit operator Type(ScriptReference cr)
{
return cr.GetClass();
}
#if UNITY_EDITOR
public ScriptReference(UnityEditor.MonoScript monoScript)
{
if (!monoScript)
{
return;
}
script = monoScript;
SetClass(monoScript.GetClass());
}
public static implicit operator ScriptReference(UnityEditor.MonoScript cr)
{
return !cr ? new ScriptReference() : new ScriptReference(cr);
}
#endif
}
/// <summary>
/// class that refer to an custom script
/// </summary>
[Serializable]
public class ScriptReference<T> : ScriptReference
{
public override Type Type => typeof(T);
public ScriptReference() : base() { }
public ScriptReference(Type type) : base(type) { }
#if UNITY_EDITOR
public ScriptReference(UnityEditor.MonoScript type) : base(type)
{
}
public static implicit operator ScriptReference<T>(UnityEditor.MonoScript cr)
{
return !cr ? new ScriptReference<T>() : new ScriptReference<T>(cr);
}
#endif
}
}