Skip to content

Commit d977630

Browse files
author
András Kurai
committed
customizable using directives
1 parent 6a1e6b9 commit d977630

File tree

7 files changed

+47
-21
lines changed

7 files changed

+47
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Add documentation website
33
- Reference documentation in `package.json`
44
- Make using namespaces optional
5+
- Configure using directives
56

67
# 0.1.0
78
- Add initial project

Documentation/articles/Extensibility.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ A module is single string that is placed inside the main generated class. To cre
1111
A post processor is a piece of code that is run after all modules are produced, and the final file text is created. To create a post processor implement the [IResourcePostProcessor](xref:AutSoft.UnityResourceGenerator.Editor.Generation.IResourcePostProcessor) interface. The implementation must provide a parameterless constructor.
1212

1313
The input of the processor is the current state of the generated file and it returns the new state of the generated file. Post processors also provide a Priority property which determines the ordering of them.
14+
15+
## Using directives
16+
17+
The generated using directives are also customizable under the settings window. If your custom generated code will require other using directives, there is no need to generate them, simply add them in the settings

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Generation/ResourceContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public ResourceContext
1313
string className,
1414
Action<string> info,
1515
Action<string> error,
16-
IReadOnlyList<IResourceData> data)
16+
IReadOnlyList<IResourceData> data,
17+
IReadOnlyList<string> usings)
1718
{
1819
AssetsFolder = assetsFolder;
1920
FolderPath = folderPath;
@@ -22,6 +23,7 @@ public ResourceContext
2223
Info = info;
2324
Error = error;
2425
Data = data;
26+
Usings = usings;
2527
}
2628

2729
public string AssetsFolder { get; }
@@ -32,5 +34,6 @@ public ResourceContext
3234
public Action<string> Error { get; }
3335

3436
public IReadOnlyList<IResourceData> Data { get; }
37+
public IReadOnlyList<string> Usings { get; }
3538
}
3639
}

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Generation/ResourceFileGenerator.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ public static string CreateResourceFile(ResourceContext context)
1212
// ReSharper disable once MissingIndent
1313
const string fileBeginHasNamespace =
1414
@"
15-
using UnityEngine;
16-
using UnityEngine.SceneManagement;
17-
1815
namespace {0}
1916
{
2017
// ReSharper disable PartialTypeWithSinglePart
@@ -29,9 +26,6 @@ public static partial class {1}
2926
// ReSharper disable once MissingIndent
3027
const string fileBeginNoNamespace =
3128
@"
32-
using UnityEngine;
33-
using UnityEngine.SceneManagement;
34-
3529
// ReSharper disable PartialTypeWithSinglePart
3630
public static partial class {1}
3731
{";
@@ -47,6 +41,8 @@ public static partial class {1}
4741
.Where(t => !t.IsAbstract && !t.IsGenericType && !t.IsInterface)
4842
.ToArray();
4943

44+
builder.AppendMultipleLines(context.Usings.Select(u => $"using {u};"));
45+
5046
builder.AppendLine(
5147
(string.IsNullOrWhiteSpace(context.BaseNamespace) ? fileBeginNoNamespace : fileBeginHasNamespace)
5248
.Replace("{0}", context.BaseNamespace)

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/ResourceFileMenu.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static void GenerateResources()
2323
settings.ClassName,
2424
settings.LogInfo ? Debug.Log : LogEmpty,
2525
settings.LogError ? Debug.LogError : LogEmpty,
26-
settings.Data
26+
settings.Data,
27+
settings.Usings
2728
);
2829

2930
context.Info("Resource Path generation started");

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/ResourceGeneratorSettings.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ public ResourceData(string className, string[] fileExtensions, bool isResource,
4646

4747
[SerializeField] private bool _logInfo;
4848
[SerializeField] private bool _logError;
49+
[SerializeField] private List<string> _usings;
4950
[SerializeField] private List<ResourceData> _data;
5051

5152
public string FolderPath => _folderPath;
5253
public string BaseNamespace => _baseNamespace;
5354
public string ClassName => _className;
5455
public bool LogInfo => _logInfo;
5556
public bool LogError => _logError;
57+
public IReadOnlyList<string> Usings => _usings;
5658
public IReadOnlyList<ResourceData> Data => _data;
5759

5860
public static ResourceGeneratorSettings GetOrCreateSettings()
@@ -68,26 +70,36 @@ public static ResourceGeneratorSettings GetOrCreateSettings()
6870
settings._logInfo = false;
6971
settings._logError = true;
7072

71-
settings._data = CreateDefaultFileMappings();
73+
var (data, usings) = CreateDefaultFileMappings();
74+
75+
settings._data = data;
76+
settings._usings = usings;
7277

7378
AssetDatabase.CreateAsset(settings, SettingsPath);
7479
AssetDatabase.SaveAssets();
7580

7681
return settings;
7782
}
7883

79-
private static List<ResourceData> CreateDefaultFileMappings() =>
84+
private static (List<ResourceData> data, List<string> usings) CreateDefaultFileMappings() =>
8085
// https://docs.unity3d.com/Manual/BuiltInImporters.html
81-
new List<ResourceData>
82-
{
83-
new ResourceData("Scenes", new[]{"*.unity"}, false, "Scene"),
84-
new ResourceData("Prefabs", new[]{"*.prefab"}, true, "GameObject"),
85-
new ResourceData("Materials", new[]{"*.mat"}, true, "Material"),
86-
new ResourceData("AudioClips", new[]{"*.ogg", "*.aif", "*.aiff", "*.flac", "*.mp3", "*.mod", "*.it", "*.s3m", "*.xm", "*.wav"}, true, "AudioClip"),
87-
new ResourceData("Sprites", new[]{"*.jpg", "*.jpeg", "*.tif", "*.tiff", "*.tga", "*.gif", "*.png", "*.psd", "*.bmp", "*.iff", "*.pict", "*.pic", "*.pct", "*.exr", "*.hdr"}, true, "Sprite"),
88-
new ResourceData("TextAssets", new[]{"*.txt", "*.html", "*.htm", "*.xml", "*.bytes", "*.json", "*.csv", "*.yaml", "*.fnt"}, true, "TextAsset"),
89-
new ResourceData("Fonts", new[]{"*.ttf", "*.dfont", "*.otf", "*.ttc"}, true, "Font")
90-
};
86+
(
87+
new List<ResourceData>
88+
{
89+
new ResourceData("Scenes", new[] { "*.unity" }, false, "Scene"),
90+
new ResourceData("Prefabs", new[] { "*.prefab" }, true, "GameObject"),
91+
new ResourceData("Materials", new[] { "*.mat" }, true, "Material"),
92+
new ResourceData("AudioClips", new[] { "*.ogg", "*.aif", "*.aiff", "*.flac", "*.mp3", "*.mod", "*.it", "*.s3m", "*.xm", "*.wav" }, true, "AudioClip"),
93+
new ResourceData("Sprites", new[] { "*.jpg", "*.jpeg", "*.tif", "*.tiff", "*.tga", "*.gif", "*.png", "*.psd", "*.bmp", "*.iff", "*.pict", "*.pic", "*.pct", "*.exr", "*.hdr" }, true, "Sprite"),
94+
new ResourceData("TextAssets", new[] { "*.txt", "*.html", "*.htm", "*.xml", "*.bytes", "*.json", "*.csv", "*.yaml", "*.fnt" }, true, "TextAsset"),
95+
new ResourceData("Fonts", new[] { "*.ttf", "*.dfont", "*.otf", "*.ttc" }, true, "Font")
96+
},
97+
new List<string>
98+
{
99+
"UnityEngine",
100+
"UnityEngine.SceneManagement",
101+
}
102+
);
91103

92104
[SettingsProvider]
93105
public static SettingsProvider CreateSettingsProvider() =>
@@ -104,8 +116,14 @@ public static SettingsProvider CreateSettingsProvider() =>
104116
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_logInfo)), new GUIContent("Log Infos"));
105117
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_logError)), new GUIContent("Log Errors"));
106118

107-
if (GUILayout.Button("Reset file mappings")) settings.FindProperty(nameof(_data)).SetValue(CreateDefaultFileMappings());
119+
if (GUILayout.Button("Reset file mappings"))
120+
{
121+
var (data, usings) = CreateDefaultFileMappings();
122+
settings.FindProperty(nameof(_data)).SetValue(data);
123+
settings.FindProperty(nameof(_usings)).SetValue(usings);
124+
}
108125

126+
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_usings)), new GUIContent("Using directives"));
109127
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_data)), new GUIContent("Data"));
110128

111129
settings.ApplyModifiedProperties();

UnityResourceGenerator/Assets/ResourceGenerator.asset

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ MonoBehaviour:
1717
_folderPath: AutSoft.UnityResourceGenerator.Sample
1818
_logInfo: 1
1919
_logError: 1
20+
_usings:
21+
- UnityEngine
22+
- UnityEngine.SceneManagement
2023
_data:
2124
- _className: Scenes
2225
_fileExtensions:

0 commit comments

Comments
 (0)