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
@@ -93,6 +92,8 @@ public class WarriorStats<TClass> : GenericScriptableObject
93
92
}
94
93
```
95
94
95
+
(Actually, you can inherit from plain ScriptableObject but it's not recommended. Read more here.)
96
+
96
97
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.
97
98
98
99
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
298
299
299
300
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.
300
301
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
+
publicclassGenericSO<T> : ScriptableObject
309
+
{
310
+
publicstaticGenericSO<T> Create()
311
+
{
312
+
returnCreateInstance<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.
0 commit comments