You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/content/docs/types/scriptable-dictionary.mdx
+103-1Lines changed: 103 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,4 +5,106 @@ sidebar:
5
5
order: 35
6
6
---
7
7
8
-
TODO: Scriptable Dictionary
8
+
`ScriptableDictionary<TKey, TValue>` is a type of scriptable object that holds a dictionary of values. It acts just like a regular [`Dictionary<TKey, TValue>`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2), but it is a scriptable object that can be used in the inspector and be passed around to other objects.
9
+
10
+
## Usage
11
+
12
+
Using a Scriptable Dictionary in your code is really straight forward. You use the `Add`, `Remove`, and `Clear` methods to manage the dictionary.
13
+
:::note
14
+
To actually asign the dictionary to a field you need to create an instance of the dictionary in the editor. These instances need to be created in code first. See [Creating a Dictionary](#creating-a-dictionary) for more information.
15
+
:::
16
+
17
+
`Add` will add an object to the dictionary. The key must be unique, otherwise it will throw an exception.
18
+
19
+
```csharp title="Add Example"
20
+
usingUnityEngine;
21
+
usingHertzole.ScriptableValues;
22
+
23
+
publicclassInventory : MonoBehaviour
24
+
{
25
+
[SerializeField]
26
+
privateScriptableDictionary<string, Item> items;
27
+
28
+
publicvoidAddItem(stringkey, Itemitem)
29
+
{
30
+
items.Add(key, item);
31
+
}
32
+
}
33
+
```
34
+
35
+
`Remove` will remove an object from the dictionary.
36
+
37
+
```csharp title="Remove Example"
38
+
usingUnityEngine;
39
+
usingHertzole.ScriptableValues;
40
+
41
+
publicclassInventory : MonoBehaviour
42
+
{
43
+
[SerializeField]
44
+
privateScriptableDictionary<string, Item> items;
45
+
46
+
publicvoidRemoveItem(stringkey)
47
+
{
48
+
items.Remove(key);
49
+
}
50
+
}
51
+
```
52
+
53
+
`Clear` will remove all objects from the dictionary. This is useful for cleaning up the dictionary when it is no longer needed, like when quitting or changing scenes.
54
+
55
+
```csharp title="Clear Example"
56
+
usingUnityEngine;
57
+
usingHertzole.ScriptableValues;
58
+
59
+
publicclassInventory : MonoBehaviour
60
+
{
61
+
[SerializeField]
62
+
privateScriptableDictionary<string, Item> items;
63
+
64
+
publicvoidClear()
65
+
{
66
+
items.Clear();
67
+
}
68
+
}
69
+
```
70
+
71
+
You can also access the dictionary directly using the indexer. This is useful for getting or setting values in the dictionary.
72
+
73
+
```csharp title="Indexer Example"
74
+
usingUnityEngine;
75
+
usingHertzole.ScriptableValues;
76
+
77
+
publicclassInventory : MonoBehaviour
78
+
{
79
+
[SerializeField]
80
+
privateScriptableDictionary<string, Item> items;
81
+
82
+
publicvoidSetItem(stringkey, Itemitem)
83
+
{
84
+
items[key] =item;
85
+
}
86
+
87
+
publicItemGetItem(stringkey)
88
+
{
89
+
returnitems[key];
90
+
}
91
+
}
92
+
```
93
+
94
+
There are many more methods available in the dictionary, such as `ContainsKey`, `TryGetValue`, and `Count`. You can find the full list of methods in the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2).
95
+
96
+
## Creating a Dictionary
97
+
98
+
You need to create a dictionary before you can use it in the editor. Fortunately, this is really easy to do! All you need to do is inherit from `ScriptableDictionary<TKey, TValue>`.
Copy file name to clipboardExpand all lines: docs/src/content/docs/types/scriptable-list.mdx
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,27 @@ public class Inventory : MonoBehaviour
70
70
}
71
71
```
72
72
73
+
You can also access the list directly using the `Value` property. This will return the list of values, which you can use just like a regular list.
74
+
75
+
```csharp title="Accessing the List"
76
+
usingUnityEngine;
77
+
usingHertzole.ScriptableValues;
78
+
79
+
publicclassInventory : MonoBehaviour
80
+
{
81
+
[SerializeField]
82
+
privateScriptableList<Item> items;
83
+
84
+
publicvoidPrintItems()
85
+
{
86
+
for (inti=0; i<items.Value.Count; i++)
87
+
{
88
+
Debug.Log(items.Value[i]);
89
+
}
90
+
}
91
+
}
92
+
```
93
+
73
94
There are many more methods available on the list, such as `Contains`, `IndexOf`, and `Sort`. It aims to have full parity with the regular `List<T>` class. See the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1) for more information.
0 commit comments