Skip to content

Commit 0c4ac2d

Browse files
committed
feat: Made possible to inherit from simple ScriptableObject instead of GenericScriptableObject to create generic assets
1 parent 47877e7 commit 0c4ac2d

20 files changed

+38
-34
lines changed

.GenericUnityInternals/GenericUnityEditorInternals/GenericTypeHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public static Type GetConcreteType(Type genericType)
3434
: genericType;
3535
}
3636

37-
if (typeof(GenericScriptableObject).IsAssignableFrom(genericType))
37+
if (typeof(ScriptableObject).IsAssignableFrom(genericType))
3838
{
3939
return ScriptableObjectsDatabase.TryGetConcreteType(genericType, out Type concreteType)
4040
? concreteType
4141
: genericType;
4242
}
4343

4444
throw new ArgumentException(
45-
$"Expected a type derived from MonoBehaviour or GenericScriptableObject. Got {genericType} instead.");
45+
$"Expected a type derived from MonoBehaviour or ScriptableObject. Got {genericType} instead.");
4646
}
4747
}
4848
}

.GenericUnityInternals/GenericUnityEditorInternals/GenericUnityEventDrawer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public static string GetTypeName(Object unityObject, bool onlyShort)
296296
{
297297
shortName = unityObject is MonoBehaviour
298298
? GetBehaviourShortName(unityObjectType)
299-
: GetScriptableObjectShortName(unityObject, unityObjectType);
299+
: GetScriptableObjectShortName(unityObjectType);
300300

301301
_typeNameCache.Add(unityObjectType, shortName);
302302
}
@@ -320,9 +320,9 @@ private static string GetBehaviourShortName(Type unityObjectType)
320320
componentAttribute.componentMenu.Split('/').Last();
321321
}
322322

323-
private static string GetScriptableObjectShortName(Object unityObject, Type unityObjectType)
323+
private static string GetScriptableObjectShortName(Type unityObjectType)
324324
{
325-
return unityObject is GenericScriptableObject ? TypeUtility.GetNiceNameOfGenericType(unityObjectType.BaseType) : unityObjectType.Name;
325+
return ((unityObjectType is GenericScriptableObject) || unityObjectType.BaseType.IsGenericType) ? TypeUtility.GetNiceNameOfGenericType(unityObjectType.BaseType) : unityObjectType.Name;
326326
}
327327
}
328328
}

Editor/Drawers/CreatableObjectDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.IO;
55
using System.Reflection;
66
using ScriptableObjects;
7-
using SolidUtilities.Editor;
87
using SolidUtilities;
8+
using SolidUtilities.Editor;
99
using SolidUtilities.UnityEditorInternals;
1010
using UnityEditor;
1111
using UnityEngine;
@@ -66,7 +66,7 @@ private ScriptableObject CreateAsset(SerializedProperty property, Type type)
6666
{
6767
var folderPath = ProjectWindowUtilProxy.GetActiveFolderPath();
6868

69-
bool isGeneric = type.InheritsFrom(typeof(GenericScriptableObject));
69+
bool isGeneric = type.InheritsFrom(typeof(ScriptableObject)) && type.IsGenericType && !type.IsAbstract;
7070

7171
string fileName = isGeneric
7272
? type.GetCustomAttribute<CreateGenericAssetMenuAttribute>()?.FileName

Editor/Drawers/GenericHeaderUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Object = UnityEngine.Object;
1010

1111
/// <summary>
12-
/// An extension of Editor that changes name of <see cref="GenericScriptableObject"/> assets in the Inspector header.
12+
/// An extension of Editor that changes name of generic <see cref="ScriptableObject"/> assets in the Inspector header.
1313
/// For all other assets, it draws header like before.
1414
/// </summary>
1515
public static class GenericHeaderUtility
@@ -91,7 +91,7 @@ private static string GetMixedTitle(Type genericType, Object[] targets)
9191
{
9292
if (genericType?.IsGenericType != true)
9393
return targets.Length + " " + ObjectNames.NicifyVariableName(GetTypeName(targets[0])) + "s";
94-
94+
9595
return $"{targets.Length} objects of type {GetGenericTypeName(genericType)}";
9696
}
9797

Editor/Drawers/GenericUnityObjectDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
1414
GenericObjectDrawer.ObjectField(position, property, label);
1515
}
1616
}
17-
18-
[CustomPropertyDrawer(typeof(GenericScriptableObject), true)]
17+
18+
[CustomPropertyDrawer(typeof(ScriptableObject), true)]
1919
#if ODIN_INSPECTOR
2020
[DrawerPriority(0, 0, 2)]
2121
#endif

Editor/GenerationDatabase/GenerationDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// </summary>
1717
/// <typeparam name="TUnityObject">
1818
/// Type derived from <see cref="UnityEngine.Object"/>. Currently supports only
19-
/// <see cref="GenericScriptableObject"/> and <see cref="MonoBehaviour"/>.
19+
/// <see cref="ScriptableObject"/> and <see cref="MonoBehaviour"/>.
2020
/// </typeparam>
2121
/// <remarks>
2222
/// The database utilizes two dictionaries: [Argument, List{GenericType}] and [GenericType, List{Argument[]}].

Editor/GenerationDatabase/SOGenerationDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// <summary>
88
/// All the work is done in the parent class. This is implemented just to create a ScriptableObject asset.
99
/// </summary>
10-
internal class SOGenerationDatabase : GenerationDatabase<GenericScriptableObject>
10+
internal class SOGenerationDatabase : GenerationDatabase<ScriptableObject>
1111
{
1212
[SerializeField] private Collection<GenericTypeInfo>[] _genericTypeValues;
1313
[SerializeField] private GenericTypeInfo[] _genericTypeKeys;

Editor/GenericTypesAnalyzer/GenericTypesAnalyzer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public static void AnalyzeGenericTypes()
4848
UpdateGeneratedAssemblies();
4949
// We don't check missing selector assemblies because a new one would be created in
5050
// UpdateGeneratedAssemblies anyway if a generic type exists in the project and is missing from the database.
51-
AddMissingConcreteClassesToDatabase<GenericScriptableObject>();
51+
AddMissingConcreteClassesToDatabase<ScriptableObject>();
5252
AddMissingConcreteClassesToDatabase<MonoBehaviour>();
5353
}
5454

5555
if (FailedAssembliesChecker.FailedAssemblyPaths.Count == 0)
5656
{
5757
DictInitializer<MonoBehaviour>.Initialize();
58-
DictInitializer<GenericScriptableObject>.Initialize();
58+
DictInitializer<ScriptableObject>.Initialize();
5959
}
6060

6161
CompilationHelper.CompilationNotNeeded();
@@ -163,7 +163,7 @@ private static void UpdateGeneratedAssemblies()
163163
| behavioursChecker.Check();
164164

165165
scriptableObjectsNeedDatabaseRefresh =
166-
ArgumentsChecker<GenericScriptableObject>.Check(scriptableObjectsChecker)
166+
ArgumentsChecker<ScriptableObject>.Check(scriptableObjectsChecker)
167167
| scriptableObjectsChecker.Check()
168168
| MenuItemsChecker.Check();
169169
}
@@ -177,9 +177,9 @@ private static void FlushConfigChangesToDisk()
177177
// AssetDatabase.SaveAssetIfDirty was added in Unity 2020.3.16 and Unity 2021.1.17
178178
#if UNITY_2020_3_16_OR_NEWER || UNITY_2021_1_17_OR_NEWER || UNITY_2021_2_OR_NEWER
179179
AssetDatabase.SaveAssetIfDirty(GenerationDatabase<MonoBehaviour>.Instance);
180-
AssetDatabase.SaveAssetIfDirty(GenerationDatabase<GenericScriptableObject>.Instance);
180+
AssetDatabase.SaveAssetIfDirty(GenerationDatabase<ScriptableObject>.Instance);
181181
AssetDatabase.SaveAssetIfDirty(GenericTypesDatabase<MonoBehaviour>.Instance);
182-
AssetDatabase.SaveAssetIfDirty(GenericTypesDatabase<GenericScriptableObject>.Instance);
182+
AssetDatabase.SaveAssetIfDirty(GenericTypesDatabase<ScriptableObject>.Instance);
183183
#else
184184
AssetDatabase.SaveAssets();
185185
#endif

Editor/GenericTypesAnalyzer/MenuItemsChecker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
using SolidUtilities;
99
using SolidUtilities.Editor;
1010
using UnityEditor;
11+
using UnityEngine;
1112
using Util;
1213

1314
/// <summary>
14-
/// Gathers types that inherit from <see cref="GenericScriptableObject"/> and have
15+
/// Gathers types that inherit from <see cref="ScriptableObject"/> and have
1516
/// <see cref="CreateGenericAssetMenuAttribute"/>, then generates a class with methods marked as
1617
/// <see cref="MenuItemMethod"/> so that they appear in the Assets/Create menu.
1718
/// </summary>
@@ -21,7 +22,7 @@ public static class MenuItemsChecker
2122

2223
public static bool Check()
2324
{
24-
var newScriptableObjects = TypeCache.GetTypesDerivedFrom<GenericScriptableObject>()
25+
var newScriptableObjects = TypeCache.GetTypesDerivedFrom<ScriptableObject>()
2526
.Where(type => type.IsGenericType && ! type.IsAbstract)
2627
.ToArray();
2728

Editor/GenericTypesAnalyzer/ScriptableObjectsChecker.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
{
33
using System;
44
using GeneratedTypesDatabase;
5+
using UnityEngine;
56

67
/// <summary>
7-
/// Checks if any generic <see cref="GenericScriptableObject"/> types were changed, removed, or updated, and
8+
/// Checks if any generic <see cref="ScriptableObject"/> types were changed, removed, or updated, and
89
/// regenerates DLLs if needed. Most of the work is done in the parent type. This class contains only methods
9-
/// where a task needs to be done differently for GenericScriptableObject compared to MonoBehaviour.
10+
/// where a task needs to be done differently for ScriptableObject compared to MonoBehaviour.
1011
/// </summary>
11-
internal class ScriptableObjectsChecker : GenericTypesChecker<GenericScriptableObject>
12+
internal class ScriptableObjectsChecker : GenericTypesChecker<ScriptableObject>
1213
{
1314
protected override bool AddNewGenericTypes(GenericTypeInfo[] genericTypes)
1415
{

0 commit comments

Comments
 (0)