-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnity_Load-ScriptableObject-from-assets
More file actions
68 lines (49 loc) · 1.54 KB
/
Unity_Load-ScriptableObject-from-assets
File metadata and controls
68 lines (49 loc) · 1.54 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
//doesn't work well on mobile for a solution check Unity_Load-ScriptableObject-from-assets2
//You can load a ScriptableObject directly from the folders and save it to a variable
//In this example the ScriptableObject is loaded and stored in a variable when another script tries get a script using the Singleton method
public class Collectables
{
private static Collectable[] collectables;
//Singleton
private static Collectables _instance = null;
public static Collectables Instance {
get{
if (_instance == null)
{
_instance = new Collectables();
//Here the load is done
collectables = Resources.FindObjectsOfTypeAll(typeof(Collectable)) as Collectable[];
}
return _instance;
}
}
void Start()
{
}
}
//We can simplify using a constructor method that will be called when executing the "new Collectables()".
public class Collectables
{
private static Collectable[] collectables;
private static Collectables _instance = null;
public static Collectables Instance {
get{
if (_instance == null)
{
//here the constructor is called
_instance = new Collectables();
}
return _instance;
}
}
//constructor method
private Collectables()
{
//Resources.FindObjectsOfTypeAll only works on loader objects, so we load them all before
Resources.LoadAll("Scripts/Collectables/ScriptableObject", typeof(Collectable));
collectables = Resources.FindObjectsOfTypeAll(typeof(Collectable)) as Collectable[];
}
void Start()
{
}
}