Skip to content

Commit c7d7514

Browse files
committed
docs: Added documentation about inheriting from plain ScriptableObject
1 parent 0c4ac2d commit c7d7514

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This package allows to create and use generic ScriptableObjects and MonoBehaviou
1919

2020
- Unity 2020.2 or higher
2121

22-
- .NET 4.x :heavy_exclamation_mark:
22+
- .NET 4.x :heavy_exclamation_mark: (when using Unity 2021.1 or lower)
2323

2424

2525

@@ -52,20 +52,19 @@ Or if you don't have it, add the scoped registry to manifest.json with the desir
5252
}
5353
],
5454
"dependencies": {
55-
"com.solidalloy.generic-unity-objects": "1.1.5"
55+
"com.solidalloy.generic-unity-objects": "2.16.0"
5656
},
5757
```
5858

59-
60-
6159
### Git URL
6260

6361
Project supports Unity Package Manager. To install it as a Git package do the following:
6462

6563
1. In Unity, open **Window** -> **Package Manager**.
6664
2. Press the **+** button, choose "**Add package from git URL...**"
6765
3. Enter "https://github.com/SolidAlloy/SolidUtilities.git" and press **Add**.
68-
4. Do the same with two more packages:
66+
4. Do the same with three more packages:
67+
- https://github.com/SolidAlloy/UnityDropdown.git
6968
- https://github.com/SolidAlloy/ClassTypeReference-for-Unity.git
7069
- https://github.com/SolidAlloy/GenericUnityObjects.git
7170

@@ -93,6 +92,8 @@ public class WarriorStats<TClass> : GenericScriptableObject
9392
}
9493
```
9594

95+
(Actually, you can inherit from plain ScriptableObject but it's not recommended. Read more here.)
96+
9697
If you use Unity 2020, you need to specify the `Serializable` attribute explicitly. Otherwise, fields of type `WarriorStats<TClass>` will not be serialized. If you use Unity 2021, this bug is fixed and Unity automatically marks generic UnityEngine.Objects as serializable.
9798

9899
In this example, there is only one generic argument, but you can use as many as you want.
@@ -298,6 +299,23 @@ In theory, it can be implemented, but it will add more complexity to the system
298299

299300
Otherwise, when a concrete class is generated, it cannot access the constructor of the internal generic class. [IgnoreAccessCheckTo](https://www.strathweb.com/2018/10/no-internalvisibleto-no-problem-bypassing-c-visibility-rules-with-roslyn/) should work but in Unity it doesn't for some reason. You will be able to create assets and add components of internal generic types, and see their fields in the inspector just fine, but every time you instantiate a generic UnityEngine.Object, an error will show up in the Console.
300301

302+
## Inheriting from plain ScriptableObject
303+
304+
Although it is recommended to inherit from GenericScriptableObject, you can derive your generic class just from ScriptableObject. There may be cases when inheriting from GenericScriptableObject is not possible, for example, when you also need to inherit from [SerializedScriptableObject](https://odininspector.com/documentation/sirenix.odininspector.serializedscriptableobject) or other class which inheritance you can't change.
305+
306+
The inheritance from GenericScriptableObject is recommended due to the fact that `CreateInstance` is implemented in both ScriptableObject and GenericScriptableObject. When inheriting from ScriptableObject, you may forget which version is used for creating instances:
307+
```csharp
308+
public class GenericSO<T> : ScriptableObject
309+
{
310+
public static GenericSO<T> Create()
311+
{
312+
return CreateInstance<GenericSO<T>>(); // this will trigger an error because the default CreateInstance method does not accept generic types. You need to use GenericScriptableObject.CreateInstance() instead.
313+
}
314+
}
315+
```
316+
317+
When inheriting from GenericScriptableObject, you protect yourself from such problems because it ensures that when you use `CreateInstance()`, it will use the GenericScriptableObject version of the method.
318+
301319
## Custom Editors
302320

303321
### Generic class in the inspector header

0 commit comments

Comments
 (0)