-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonoScriptCache.cs
38 lines (35 loc) · 1.01 KB
/
MonoScriptCache.cs
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
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Minerva.Module
{
/// <summary>
/// A cache allowing getting mono script of an class (if exist)
/// </summary>
public class MonoScriptCache
{
static Dictionary<Type, MonoScript> scripts;
static Dictionary<Type, MonoScript> Init()
{
scripts = new Dictionary<Type, UnityEditor.MonoScript>();
foreach (var item in Resources.FindObjectsOfTypeAll<UnityEditor.MonoScript>())
{
Type type = item.GetClass();
if (type != null)
{
scripts[type] = item;
}
}
return scripts;
}
public static MonoScript Get<T>() => Get(typeof(T));
public static MonoScript Get(Type type)
{
scripts ??= Init();
return scripts.TryGetValue(type, out var script) ? script : null;
}
}
}
#endif