Skip to content

Commit d94b3cc

Browse files
author
András Kurai
committed
Rework scene generation, remove IsResource
1 parent d977630 commit d94b3cc

File tree

7 files changed

+19
-25
lines changed

7 files changed

+19
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Reference documentation in `package.json`
44
- Make using namespaces optional
55
- Configure using directives
6+
- Remove `IsResource` property from `IResourceData`. Scene path generation now depends on file extension
67

78
# 0.1.0
89
- Add initial project

Documentation/articles/Customization.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ The data section describes the automatically generated file mappings. The data i
2727

2828
The default mappings are created from the [Unity documentation](https://docs.unity3d.com/Manual/BuiltInImporters.html). If you know of any valid file mapping that the documentation does not state, please fork and create a Pull Request, or create an [Issue](https://github.com/AutSoft/UnityResourceGenerator/issues/new).
2929

30+
> [!WARNING]
31+
> Scenes have special handling during the file generation, because of the way scenes paths are used by the SceneManager. Scenes are identified by the file ending `.unity`
32+
3033
## Extending the generated classes
3134

3235
The generated classes are `partial` by default. This means that if you want to add custom methods, simply create another `partial` class and add your code there.
4.38 KB
Loading

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public interface IResourceData
66
{
77
string ClassName { get; }
88
IReadOnlyList<string> FileExtensions { get; }
9-
bool IsResource { get; }
109
string DataType { get; }
1110
}
1211
}

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Generation/Modules/AllResources.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public static partial class {data.ClassName}
3232
.SelectMany(ext => Directory.EnumerateFiles(context.AssetsFolder, ext, SearchOption.AllDirectories))
3333
.Select(filePath =>
3434
{
35-
var (canLoad, baseFolder) = GetBaseFolder(filePath, data.IsResource, context);
36-
if (!canLoad) return (null, null);
35+
var (canLoad, baseFolder) = GetBaseFolder(filePath, context, data);
36+
if (!canLoad) return (null, null, null);
3737

3838
var resourcePath = filePath
3939
.Replace(baseFolder, string.Empty)
@@ -49,7 +49,8 @@ public static partial class {data.ClassName}
4949
return
5050
(
5151
name: Path.GetFileNameWithoutExtension(filePath),
52-
path: resourcePath
52+
path: resourcePath,
53+
fileExtension: Path.GetExtension(filePath)
5354
);
5455
})
5556
.Where(p => p.name != null)
@@ -77,7 +78,7 @@ public static partial class {data.ClassName}
7778
{
7879
sb.Append(" public const string ").Append(s.name).Append(" = \"").Append(s.path).AppendLine("\";");
7980

80-
if (data.DataType == "Scene")
81+
if (s.fileExtension == ".unity")
8182
{
8283
sb.Append(" public static ").Append("void").Append(" Load").Append(s.name).Append("(LoadSceneMode mode = LoadSceneMode.Single) => SceneManager.LoadScene(").Append(s.name).AppendLine(", mode);");
8384
sb.Append(" public static ").Append("AsyncOperation").Append(" LoadAsync").Append(s.name).Append("(LoadSceneMode mode = LoadSceneMode.Single) => SceneManager.LoadSceneAsync(").Append(s.name).AppendLine(", mode);");
@@ -99,9 +100,9 @@ public static partial class {data.ClassName}
99100
void LogFinished() => context.Info($"Finished generating {data.ClassName}");
100101
}
101102

102-
private static (bool canLoad, string baseFolder) GetBaseFolder(string filePath, bool isResource, ResourceContext context)
103+
private static (bool canLoad, string baseFolder) GetBaseFolder(string filePath, ResourceContext context, IResourceData data)
103104
{
104-
if (!isResource) return (true, context.AssetsFolder);
105+
if (Path.GetExtension(filePath) == ".unity") return (true, context.AssetsFolder);
105106

106107
var parents = GetAllParentDirectories(filePath);
107108
var resourcesFolder = parents.LastOrDefault(p => p.Name == "Resources");

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@ public sealed class ResourceData : IResourceData
1414
{
1515
[SerializeField] private string _className = default;
1616
[SerializeField] private string[] _fileExtensions = default;
17-
[SerializeField] private bool _isResource = default;
1817
[SerializeField] private string _dataType = default;
1918

2019
public ResourceData()
2120
{
2221
}
2322

24-
public ResourceData(string className, string[] fileExtensions, bool isResource, string dataType)
23+
public ResourceData(string className, string[] fileExtensions, string dataType)
2524
{
2625
_className = className;
2726
_fileExtensions = fileExtensions;
28-
_isResource = isResource;
2927
_dataType = dataType;
3028
}
3129

3230
public string ClassName => _className;
3331
public IReadOnlyList<string> FileExtensions => _fileExtensions;
34-
public bool IsResource => _isResource;
3532
public string DataType => _dataType;
3633
}
3734

@@ -86,13 +83,13 @@ private static (List<ResourceData> data, List<string> usings) CreateDefaultFileM
8683
(
8784
new List<ResourceData>
8885
{
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")
86+
new ResourceData("Scenes", new[] { "*.unity" }, "Scene"),
87+
new ResourceData("Prefabs", new[] { "*.prefab" }, "GameObject"),
88+
new ResourceData("Materials", new[] { "*.mat" }, "Material"),
89+
new ResourceData("AudioClips", new[] { "*.ogg", "*.aif", "*.aiff", "*.flac", "*.mp3", "*.mod", "*.it", "*.s3m", "*.xm", "*.wav" }, "AudioClip"),
90+
new ResourceData("Sprites", new[] { "*.jpg", "*.jpeg", "*.tif", "*.tiff", "*.tga", "*.gif", "*.png", "*.psd", "*.bmp", "*.iff", "*.pict", "*.pic", "*.pct", "*.exr", "*.hdr" }, "Sprite"),
91+
new ResourceData("TextAssets", new[] { "*.txt", "*.html", "*.htm", "*.xml", "*.bytes", "*.json", "*.csv", "*.yaml", "*.fnt" }, "TextAsset"),
92+
new ResourceData("Fonts", new[] { "*.ttf", "*.dfont", "*.otf", "*.ttc" }, "Font")
9693
},
9794
new List<string>
9895
{

UnityResourceGenerator/Assets/ResourceGenerator.asset

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ MonoBehaviour:
2424
- _className: Scenes
2525
_fileExtensions:
2626
- '*.unity'
27-
_isResource: 0
2827
_dataType: Scene
2928
- _className: Prefabs
3029
_fileExtensions:
3130
- '*.prefab'
32-
_isResource: 1
3331
_dataType: GameObject
3432
- _className: Materials
3533
_fileExtensions:
3634
- '*.mat'
37-
_isResource: 1
3835
_dataType: Material
3936
- _className: AudioClips
4037
_fileExtensions:
@@ -48,7 +45,6 @@ MonoBehaviour:
4845
- '*.s3m'
4946
- '*.xm'
5047
- '*.wav'
51-
_isResource: 1
5248
_dataType: AudioClip
5349
- _className: Sprites
5450
_fileExtensions:
@@ -67,7 +63,6 @@ MonoBehaviour:
6763
- '*.pct'
6864
- '*.exr'
6965
- '*.hdr'
70-
_isResource: 1
7166
_dataType: Sprite
7267
- _className: TextAssets
7368
_fileExtensions:
@@ -80,13 +75,11 @@ MonoBehaviour:
8075
- '*.csv'
8176
- '*.yaml'
8277
- '*.fnt'
83-
_isResource: 1
8478
_dataType: TextAsset
8579
- _className: Fonts
8680
_fileExtensions:
8781
- '*.ttf'
8882
- '*.dfont'
8983
- '*.otf'
9084
- '*.ttc'
91-
_isResource: 1
9285
_dataType: Font

0 commit comments

Comments
 (0)