Skip to content

Commit fe888de

Browse files
author
András Kurai
committed
Add generic class, test materials
1 parent b49cfc2 commit fe888de

File tree

11 files changed

+223
-104
lines changed

11 files changed

+223
-104
lines changed

UnityResourceGenerator/Assets/UnityResourceGenerator.Sample/ResourcePaths.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ namespace UnityResourceGenerator.Sample
33
// ReSharper disable PartialTypeWithSinglePart
44
public static partial class ResourcePaths
55
{
6-
public static partial class Prefabs
7-
{
8-
public const string Cube = "Cube";
9-
}
106
public static partial class Scenes
117
{
128
public const string CreatePrefab = "UnityResourceGenerator.Sample/Scenes/CreatePrefab";
139
public const string LoadSceneInitial = "UnityResourceGenerator.Sample/Scenes/LoadSceneInitial";
1410
public const string LoadSceneNext = "UnityResourceGenerator.Sample/Scenes/LoadSceneNext";
1511
}
12+
public static partial class Prefabs
13+
{
14+
public const string Cube = "Cube";
15+
}
16+
public static partial class Materials
17+
{
18+
public const string Cube = "Cube";
19+
public const string CubeAlt = "CubeAlt";
20+
}
1621
}
1722
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!21 &2100000
4+
Material:
5+
serializedVersion: 6
6+
m_ObjectHideFlags: 0
7+
m_CorrespondingSourceObject: {fileID: 0}
8+
m_PrefabInstance: {fileID: 0}
9+
m_PrefabAsset: {fileID: 0}
10+
m_Name: Cube
11+
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
12+
m_ShaderKeywords:
13+
m_LightmapFlags: 4
14+
m_EnableInstancingVariants: 0
15+
m_DoubleSidedGI: 0
16+
m_CustomRenderQueue: -1
17+
stringTagMap: {}
18+
disabledShaderPasses: []
19+
m_SavedProperties:
20+
serializedVersion: 3
21+
m_TexEnvs:
22+
- _BumpMap:
23+
m_Texture: {fileID: 0}
24+
m_Scale: {x: 1, y: 1}
25+
m_Offset: {x: 0, y: 0}
26+
- _DetailAlbedoMap:
27+
m_Texture: {fileID: 0}
28+
m_Scale: {x: 1, y: 1}
29+
m_Offset: {x: 0, y: 0}
30+
- _DetailMask:
31+
m_Texture: {fileID: 0}
32+
m_Scale: {x: 1, y: 1}
33+
m_Offset: {x: 0, y: 0}
34+
- _DetailNormalMap:
35+
m_Texture: {fileID: 0}
36+
m_Scale: {x: 1, y: 1}
37+
m_Offset: {x: 0, y: 0}
38+
- _EmissionMap:
39+
m_Texture: {fileID: 0}
40+
m_Scale: {x: 1, y: 1}
41+
m_Offset: {x: 0, y: 0}
42+
- _MainTex:
43+
m_Texture: {fileID: 0}
44+
m_Scale: {x: 1, y: 1}
45+
m_Offset: {x: 0, y: 0}
46+
- _MetallicGlossMap:
47+
m_Texture: {fileID: 0}
48+
m_Scale: {x: 1, y: 1}
49+
m_Offset: {x: 0, y: 0}
50+
- _OcclusionMap:
51+
m_Texture: {fileID: 0}
52+
m_Scale: {x: 1, y: 1}
53+
m_Offset: {x: 0, y: 0}
54+
- _ParallaxMap:
55+
m_Texture: {fileID: 0}
56+
m_Scale: {x: 1, y: 1}
57+
m_Offset: {x: 0, y: 0}
58+
m_Floats:
59+
- _BumpScale: 1
60+
- _Cutoff: 0.5
61+
- _DetailNormalMapScale: 1
62+
- _DstBlend: 0
63+
- _GlossMapScale: 1
64+
- _Glossiness: 0.5
65+
- _GlossyReflections: 1
66+
- _Metallic: 0
67+
- _Mode: 0
68+
- _OcclusionStrength: 1
69+
- _Parallax: 0.02
70+
- _SmoothnessTextureChannel: 0
71+
- _SpecularHighlights: 1
72+
- _SrcBlend: 1
73+
- _UVSec: 0
74+
- _ZWrite: 1
75+
m_Colors:
76+
- _Color: {r: 1, g: 1, b: 1, a: 1}
77+
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

UnityResourceGenerator/Assets/UnityResourceGenerator.Sample/Resources/Cube.mat.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!21 &2100000
4+
Material:
5+
serializedVersion: 6
6+
m_ObjectHideFlags: 0
7+
m_CorrespondingSourceObject: {fileID: 0}
8+
m_PrefabInstance: {fileID: 0}
9+
m_PrefabAsset: {fileID: 0}
10+
m_Name: CubeAlt
11+
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
12+
m_ShaderKeywords:
13+
m_LightmapFlags: 4
14+
m_EnableInstancingVariants: 0
15+
m_DoubleSidedGI: 0
16+
m_CustomRenderQueue: -1
17+
stringTagMap: {}
18+
disabledShaderPasses: []
19+
m_SavedProperties:
20+
serializedVersion: 3
21+
m_TexEnvs:
22+
- _BumpMap:
23+
m_Texture: {fileID: 0}
24+
m_Scale: {x: 1, y: 1}
25+
m_Offset: {x: 0, y: 0}
26+
- _DetailAlbedoMap:
27+
m_Texture: {fileID: 0}
28+
m_Scale: {x: 1, y: 1}
29+
m_Offset: {x: 0, y: 0}
30+
- _DetailMask:
31+
m_Texture: {fileID: 0}
32+
m_Scale: {x: 1, y: 1}
33+
m_Offset: {x: 0, y: 0}
34+
- _DetailNormalMap:
35+
m_Texture: {fileID: 0}
36+
m_Scale: {x: 1, y: 1}
37+
m_Offset: {x: 0, y: 0}
38+
- _EmissionMap:
39+
m_Texture: {fileID: 0}
40+
m_Scale: {x: 1, y: 1}
41+
m_Offset: {x: 0, y: 0}
42+
- _MainTex:
43+
m_Texture: {fileID: 0}
44+
m_Scale: {x: 1, y: 1}
45+
m_Offset: {x: 0, y: 0}
46+
- _MetallicGlossMap:
47+
m_Texture: {fileID: 0}
48+
m_Scale: {x: 1, y: 1}
49+
m_Offset: {x: 0, y: 0}
50+
- _OcclusionMap:
51+
m_Texture: {fileID: 0}
52+
m_Scale: {x: 1, y: 1}
53+
m_Offset: {x: 0, y: 0}
54+
- _ParallaxMap:
55+
m_Texture: {fileID: 0}
56+
m_Scale: {x: 1, y: 1}
57+
m_Offset: {x: 0, y: 0}
58+
m_Floats:
59+
- _BumpScale: 1
60+
- _Cutoff: 0.5
61+
- _DetailNormalMapScale: 1
62+
- _DstBlend: 0
63+
- _GlossMapScale: 1
64+
- _Glossiness: 0.5
65+
- _GlossyReflections: 1
66+
- _Metallic: 0
67+
- _Mode: 0
68+
- _OcclusionStrength: 1
69+
- _Parallax: 0.02
70+
- _SmoothnessTextureChannel: 0
71+
- _SpecularHighlights: 1
72+
- _SrcBlend: 1
73+
- _UVSec: 0
74+
- _ZWrite: 1
75+
m_Colors:
76+
- _Color: {r: 1, g: 1, b: 1, a: 1}
77+
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

UnityResourceGenerator/Assets/UnityResourceGenerator.Sample/Resources/CubeAlt.mat.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityResourceGenerator/Assets/UnityResourceGenerator.Tests/ResourcePathTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,15 @@ public void PrefabsWork()
2020
var prefab = Resources.Load<GameObject>(ResourcePaths.Prefabs.Cube);
2121
Assert.NotNull(prefab);
2222
}
23+
24+
[Test]
25+
public void MaterialsWork()
26+
{
27+
var cubeMaterial = Resources.Load<Material>(ResourcePaths.Materials.Cube);
28+
Assert.NotNull(cubeMaterial);
29+
30+
var cubeAltMaterial = Resources.Load<Material>(ResourcePaths.Materials.CubeAlt);
31+
Assert.NotNull(cubeAltMaterial);
32+
}
2333
}
2434
}

UnityResourceGenerator/Assets/UnityResourceGenerator/Editor/Generation/Modules/Prefabs.cs renamed to UnityResourceGenerator/Assets/UnityResourceGenerator/Editor/Generation/Modules/AllResources.cs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@
66

77
namespace UnityResourceGenerator.Editor.Generation.Modules
88
{
9-
public sealed class Prefabs : IModuleGenerator
9+
public sealed class AllResources : IModuleGenerator
1010
{
11-
public string Generate(ResourceContext context)
11+
public string Generate(ResourceContext context) =>
12+
new StringBuilder()
13+
.AppendLine(Generate(context, "Scenes", "*.unity", false))
14+
.AppendLine(Generate(context, "Prefabs", "*.prefab", true))
15+
.AppendLine(Generate(context, "Materials", "*.mat", true))
16+
.ToString();
17+
18+
private string Generate(ResourceContext context, string className, string fileExtension, bool isResource)
1219
{
13-
context.Info("Started generating prefabs");
20+
context.Info($"Started generating {className}");
1421

1522
// ReSharper disable once MissingIndent
16-
const string classBegin =
17-
@"
18-
public static partial class Prefabs
19-
{
23+
var classBegin =
24+
$@"
25+
public static partial class {className}
26+
{{
2027
";
2128
// ReSharper disable once MissingIndent
2229
const string classEnd = " }";
2330

2431
var values = Directory
25-
.EnumerateFiles(context.AssetsFolder, "*.prefab", SearchOption.AllDirectories)
32+
.EnumerateFiles(context.AssetsFolder, fileExtension, SearchOption.AllDirectories)
2633
.Select(filePath =>
2734
{
28-
var parents = GetAllParentDirectories(filePath);
29-
var resourcesFolder = parents.LastOrDefault(p => p.Name == "Resources");
30-
if (resourcesFolder is null) return (null, null);
31-
32-
var baseFolder = resourcesFolder.FullName;
35+
var (canLoad, baseFolder) = GetBaseFolder(filePath, isResource, context);
36+
if (!canLoad) return (null, null);
3337

3438
var resourcePath = filePath
3539
.Replace(baseFolder, string.Empty)
@@ -71,12 +75,24 @@ public static partial class Prefabs
7175
.AppendLine(classEnd)
7276
.ToString();
7377

74-
context.Info("Finished generating prefabs");
78+
context.Info($"Finished generating {className}");
7579

7680
return output;
7781
}
7882

79-
private static List<DirectoryInfo> GetAllParentDirectories(string directoryToScan)
83+
private static (bool canLoad, string baseFolder) GetBaseFolder(string filePath, bool isResource, ResourceContext context)
84+
{
85+
if (!isResource) return (true, context.AssetsFolder);
86+
87+
var parents = GetAllParentDirectories(filePath);
88+
var resourcesFolder = parents.LastOrDefault(p => p.Name == "Resources");
89+
90+
return resourcesFolder is null
91+
? (false, null)
92+
: (true, resourcesFolder.FullName);
93+
}
94+
95+
private static IEnumerable<DirectoryInfo> GetAllParentDirectories(string directoryToScan)
8096
{
8197
var ret = new Stack<DirectoryInfo>();
8298
GetAllParentDirectories(new DirectoryInfo(directoryToScan), ref ret);

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

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityResourceGenerator/Assets/UnityResourceGenerator/Editor/Generation/Modules/Prefabs.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

UnityResourceGenerator/Assets/UnityResourceGenerator/Editor/Generation/Modules/Scenes.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)