Skip to content

Commit b66a7e5

Browse files
committed
feat: Allowed usage of non-serialized types when creating generic UnityEngine Objects and removed the possibility to choose custom namespace name and scripts path when generating concrete types from generic scriptable objects
1 parent a97f4f0 commit b66a7e5

14 files changed

Lines changed: 14 additions & 75 deletions

Editor/AssetCreation/AssetCreatorHelper.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ internal class AssetCreatorHelper
1515
{
1616
private readonly Type _genericType;
1717
private readonly Type[] _argumentTypes;
18-
private readonly string _scriptsPath;
19-
private readonly string _namespaceName;
2018
private readonly string _fileName;
2119

22-
public AssetCreatorHelper(Type genericType, Type[] argumentTypes, string namespaceName, string scriptsPath, string fileName)
20+
public AssetCreatorHelper(Type genericType, Type[] argumentTypes, string fileName)
2321
{
2422
_genericType = genericType;
2523
_argumentTypes = argumentTypes;
26-
_namespaceName = namespaceName;
27-
_scriptsPath = scriptsPath;
2824
_fileName = fileName;
2925
}
3026

@@ -46,7 +42,7 @@ public void CreateAsset()
4642
return;
4743
}
4844

49-
GenericObjectsPersistentStorage.SaveForAssemblyReload(genericTypeWithArgs, _namespaceName, _scriptsPath, _fileName);
45+
GenericObjectsPersistentStorage.SaveForAssemblyReload(genericTypeWithArgs, _fileName);
5046
string className = GetUniqueClassName();
5147
CreateScript(className);
5248
}
@@ -95,7 +91,7 @@ private void CreateScript(string className)
9591
{
9692
#if UNITY_EDITOR_WIN
9793
const string longPathPrefix = @"\\?\";
98-
string fullAssetPath = $"{longPathPrefix}{Application.dataPath}/{_scriptsPath}/{className}.cs";
94+
string fullAssetPath = $"{longPathPrefix}{Application.dataPath}/{Config.ScriptsPath}/{className}.cs";
9995

10096
if (fullAssetPath.Length > 260 + longPathPrefix.Length)
10197
{
@@ -108,15 +104,15 @@ private void CreateScript(string className)
108104

109105
string scriptContent = GetScriptContent(className);
110106

111-
AssetDatabaseHelper.MakeSureFolderExists(_scriptsPath);
107+
AssetDatabaseHelper.MakeSureFolderExists(Config.ScriptsPath);
112108
File.WriteAllText(fullAssetPath, scriptContent);
113109
AssetDatabase.Refresh();
114110
}
115111

116112
private string GetScriptContent(string className)
117113
{
118114
string genericTypeWithBrackets = CreatorUtil.GetFullNameWithBrackets(_genericType, _argumentTypes);
119-
return $"namespace {_namespaceName} {{ public class {className} : {genericTypeWithBrackets} {{ }} }}";
115+
return $"namespace {Config.NamespaceName} {{ public class {className} : {genericTypeWithBrackets} {{ }} }}";
120116
}
121117

122118
private void CreateAssetInteractively()

Editor/AssetCreation/GenericBehaviourCreator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static string GetUniqueClassName(Type genericTypeWithoutArgs, Type[] gen
105105
public static string GetPathToGeneratedFile(Type genericTypeWithoutArgs, Type[] genericArgs)
106106
{
107107
string fileName = genericTypeWithoutArgs.FullName.MakeClassFriendly();
108-
string fileDir = $"Assets/{Config.DefaultScriptsPath}";
108+
string fileDir = $"Assets/{Config.ScriptsPath}";
109109
return $"{fileDir}/{fileName}.cs";
110110
}
111111

Editor/AssetCreation/GenericSOCreator.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@ public class GenericSOCreator
2323
/// <param name="scriptsPath">Path to a folder where auto-generated non-generic types must be kept.</param>
2424
/// <param name="fileName">Name for an asset.</param>
2525
protected static void CreateAsset(
26-
Type genericType, string namespaceName, string scriptsPath, string fileName)
26+
Type genericType, string fileName)
2727
{
28-
namespaceName = ValidateNamespaceName(namespaceName);
29-
scriptsPath = ValidateScriptsPath(scriptsPath);
30-
3128
genericType = TypeHelper.MakeGenericTypeDefinition(genericType);
3229
var constraints = genericType.GetGenericArguments()
3330
.Select(type => type.GetGenericParameterConstraints())
3431
.ToArray();
3532

3633
TypeSelectionWindow.Create(constraints, paramTypes =>
3734
{
38-
var creator = new AssetCreatorHelper(genericType, paramTypes, namespaceName, scriptsPath, fileName);
35+
var creator = new AssetCreatorHelper(genericType, paramTypes, fileName);
3936
creator.CreateAsset();
4037
});
4138
}
@@ -54,8 +51,6 @@ private static void OnScriptsReload()
5451
var creator = new AssetCreatorHelper(
5552
genericTypeWithoutArgs,
5653
paramTypes,
57-
GenericObjectsPersistentStorage.NamespaceName,
58-
GenericObjectsPersistentStorage.ScriptsPath,
5954
GenericObjectsPersistentStorage.FileName);
6055

6156
creator.CreateAssetFromExistingType();
@@ -65,25 +60,5 @@ private static void OnScriptsReload()
6560
GenericObjectsPersistentStorage.Clear();
6661
}
6762
}
68-
69-
private static string ValidateNamespaceName(string namespaceName)
70-
{
71-
if (namespaceName.IsValidIdentifier())
72-
return namespaceName;
73-
74-
Debug.LogError($"The provided namespace name '{namespaceName}' is not a valid identifier.");
75-
namespaceName = Config.DefaultNamespaceName;
76-
return namespaceName;
77-
}
78-
79-
private static string ValidateScriptsPath(string scriptsPath)
80-
{
81-
if (scriptsPath.IsValidPath())
82-
return scriptsPath;
83-
84-
Debug.LogError($"The provided path '{scriptsPath}' is not a valid Unity path. Restricted characters are /?<>\\:*|\"");
85-
scriptsPath = Config.DefaultScriptsPath;
86-
return scriptsPath;
87-
}
8863
}
8964
}

Editor/BehaviourSelectorEditor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
namespace GenericScriptableObjects.Editor
22
{
3-
using System.Collections.Generic;
43
using System.Linq;
54
using AssetCreation;
65
using GenericScriptableObjects.Util;
76
using SolidUtilities.Editor.Helpers;
87
using SolidUtilities.Extensions;
98
using SolidUtilities.Helpers;
10-
using TypeReferences.Editor.Util;
119
using UnityEditor;
1210
using UnityEngine;
1311

Editor/MenuItemsGeneration/GenericBehaviourChecker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private static void OnScriptsReload()
4242
string niceFullName = CreatorUtil.GetGenericTypeDefinitionName(type);
4343

4444
string fileContent =
45-
$"namespace {Config.DefaultNamespaceName}{NewLine}" +
45+
$"namespace {Config.NamespaceName}{NewLine}" +
4646
$"{{{NewLine}" +
4747
$" [UnityEngine.AddComponentMenu(\"Scripts/{componentName}\")]{NewLine}" +
4848
$" internal class {className} : {BehaviourSelectorFullName}{NewLine}" +

Editor/MenuItemsGeneration/GenericSOTypesChecker.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ private static void OnScriptsReload()
3838
TypeName = type.FullName.MakeClassFriendly(),
3939
FileName = assetMenuAttribute.FileName,
4040
MenuName = assetMenuAttribute.MenuName,
41-
NamespaceName = assetMenuAttribute.NamespaceName,
42-
ScriptsPath = assetMenuAttribute.ScriptsPath,
4341
Order = assetMenuAttribute.Order,
4442
Type = type
4543
});

Editor/MenuItemsGeneration/MenuItemMethod.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ public struct MenuItemMethod
1212
public string TypeName;
1313
public string FileName;
1414
public string MenuName;
15-
public string NamespaceName;
16-
public string ScriptsPath;
1715
public int Order;
1816
public Type Type;
1917

@@ -34,8 +32,7 @@ public class EqualityComparer : EqualityComparer<MenuItemMethod>
3432
{
3533
public override bool Equals(MenuItemMethod x, MenuItemMethod y)
3634
{
37-
return x.TypeName == y.TypeName && x.FileName == y.FileName && x.MenuName == y.MenuName
38-
&& x.NamespaceName == y.NamespaceName && x.ScriptsPath == y.ScriptsPath && x.Order == y.Order;
35+
return x.TypeName == y.TypeName && x.FileName == y.FileName && x.MenuName == y.MenuName && x.Order == y.Order;
3936
}
4037

4138
public override int GetHashCode(MenuItemMethod obj)
@@ -46,8 +43,6 @@ public override int GetHashCode(MenuItemMethod obj)
4643
hash = hash * 23 + obj.TypeName.GetHashCode();
4744
hash = hash * 23 + obj.FileName.GetHashCode();
4845
hash = hash * 23 + obj.MenuName.GetHashCode();
49-
hash = hash * 23 + obj.NamespaceName.GetHashCode();
50-
hash = hash * 23 + obj.ScriptsPath.GetHashCode();
5146
hash = hash * 23 + obj.Order.GetHashCode();
5247
return hash;
5348
}

Editor/MenuItemsGeneration/MenuItemsGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static string CreateMenuItemMethod(MenuItemMethod method)
126126
string typeName = CreatorUtil.GetGenericTypeDefinitionName(method.Type);
127127

128128
string methodLine = $"private static void Create{method.TypeName}() => CreateAsset(typeof({typeName}), " +
129-
$"\"{method.NamespaceName}\", \"{method.ScriptsPath}\", \"{fileName}\");";
129+
$"\"{Config.NamespaceName}\", \"{Config.ScriptsPath}\", \"{fileName}\");";
130130

131131
return $"{attributeLine}{NewLine}{methodLine}{NewLine}{NewLine}";
132132
}

Editor/TypeSelectionWindows/MultipleTypeSelectionWindow.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using TypeReferences;
99
using UnityEditor;
1010
using UnityEngine;
11-
using Util;
1211

1312
/// <summary>
1413
/// A window that has as many TypeReference fields as needed for the asset creation. The user has to choose all

Editor/TypeSelectionWindows/OneTypeSelectionWindow.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ protected override void OnGUI()
3737
: new InheritsAttribute(_genericParamConstraints) { ExpandAllFolders = true };
3838

3939
typeOptionsAttribute.ExcludeNone = true;
40-
typeOptionsAttribute.SerializableOnly = true;
4140

4241
var dropdownDrawer = new CenteredTypeDropdownDrawer(null, typeOptionsAttribute, null);
4342
_dropdownWindow = dropdownDrawer.Draw(type => _onTypeSelected(new[] { type }));

0 commit comments

Comments
 (0)