-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnity_Load-ScriptableObject-from-assets2
More file actions
54 lines (42 loc) · 1.52 KB
/
Unity_Load-ScriptableObject-from-assets2
File metadata and controls
54 lines (42 loc) · 1.52 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
//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 a scriptableobject is modified, only in the unity editor
public class Collectable : ScriptableObject
{
#if UNITY_EDITOR
//OnValidate is called whenever the file is modified
private void OnValidate()
{
Collectables.Instance.AddCollectable(this);
}
#endif
}
public class Collectables : MonoBehaviour
{
[SerializeField] private List<Collectable> collectables;
//singleton
private static Collectables _instance;
public static Collectables Instance
{
get
{
if (_instance == null)
_instance = FindObjectOfType<Collectables>();
return _instance;
}
}
#if UNITY_EDITOR
public void AddCollectable(Collectable collectable)
{
//Here all nulls are removed and the new scriptableobject is added //////////////////////////////////
//It is important to remember that every time a scriptableobject is removed, it is necessary to modify another scriptableobject to remove the null
//it is possible to handle this using try cat. Or removing all nulls from the list on startup
//An interesting way to solve it is using the script that is run when compiling the project. Calling a function to remove nulls, see PreBuild script.
collectables.RemoveAll(x => x == null);
if (!collectables.Contains(collectable))
{
collectables.Add(collectable);
Debug.Log("<color=Lime>" + collectable + " Added to list</color>");
}
}
#endif
}