Skip to content

Commit 1516593

Browse files
author
András Kurai
committed
add button for resetting default file mappings
1 parent 61193b1 commit 1516593

File tree

8 files changed

+179
-51
lines changed

8 files changed

+179
-51
lines changed

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator.Sample/CustomTool.cs

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

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator.Sample/CustomTool.cs.meta

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

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator.Sample/ResourcePaths.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ public static partial class Materials
2020
}
2121
public static partial class AudioClips
2222
{
23-
public const string Coin = "Coin";
2423
public const string CoinSpin = "CoinSpin";
24+
public const string Coin = "Coin";
25+
}
26+
public static partial class Sprites
27+
{
28+
}
29+
public static partial class TextAssets
30+
{
31+
}
32+
public static partial class Fonts
33+
{
2534
}
2635
}
2736
}

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Extensions.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.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System.Collections;
2+
using System.Reflection;
3+
using System.Text.RegularExpressions;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
// Made from https://gist.github.com/aholkner/214628a05b15f0bb169660945ac7923b
8+
9+
namespace AutSoft.UnityResourceGenerator.Editor.Extensions
10+
{
11+
internal static class SerializedPropertyExtensions
12+
{
13+
/// (Extension) Set the value of the serialized property.
14+
public static void SetValue(this SerializedProperty property, object value)
15+
{
16+
Undo.RecordObject(property.serializedObject.targetObject, $"Set {property.name}");
17+
18+
SetValueNoRecord(property, value);
19+
20+
EditorUtility.SetDirty(property.serializedObject.targetObject);
21+
property.serializedObject.ApplyModifiedProperties();
22+
}
23+
24+
/// (Extension) Set the value of the serialized property, but do not record the change.
25+
/// The change will not be persisted unless you call SetDirty and ApplyModifiedProperties.
26+
private static void SetValueNoRecord(this SerializedProperty property, object value)
27+
{
28+
var propertyPath = property.propertyPath;
29+
object container = property.serializedObject.targetObject;
30+
31+
var i = 0;
32+
NextPathComponent(propertyPath, ref i, out var deferredToken);
33+
while (NextPathComponent(propertyPath, ref i, out var token))
34+
{
35+
container = GetPathComponentValue(container, deferredToken);
36+
deferredToken = token;
37+
}
38+
39+
Debug.Assert(!container.GetType().IsValueType, $"Cannot use SerializedObject.SetValue on a struct object, as the result will be set on a temporary. Either change {container.GetType().Name} to a class, or use SetValue with a parent member.");
40+
SetPathComponentValue(container, deferredToken, value);
41+
}
42+
43+
// Union type representing either a property name or array element index. The element
44+
// index is valid only if propertyName is null.
45+
private struct PropertyPathComponent
46+
{
47+
public string PropertyName;
48+
public int ElementIndex;
49+
}
50+
51+
private static readonly Regex ArrayElementRegex = new Regex(@"\GArray\.data\[(\d+)\]", RegexOptions.Compiled);
52+
53+
private static bool NextPathComponent(string propertyPath, ref int index, out PropertyPathComponent component)
54+
{
55+
component = new PropertyPathComponent();
56+
57+
if (index >= propertyPath.Length)
58+
return false;
59+
60+
var arrayElementMatch = ArrayElementRegex.Match(propertyPath, index);
61+
if (arrayElementMatch.Success)
62+
{
63+
index += arrayElementMatch.Length + 1; // Skip past next '.'
64+
component.ElementIndex = int.Parse(arrayElementMatch.Groups[1].Value);
65+
return true;
66+
}
67+
68+
var dot = propertyPath.IndexOf('.', index);
69+
if (dot == -1)
70+
{
71+
component.PropertyName = propertyPath.Substring(index);
72+
index = propertyPath.Length;
73+
}
74+
else
75+
{
76+
component.PropertyName = propertyPath.Substring(index, dot - index);
77+
index = dot + 1; // Skip past next '.'
78+
}
79+
80+
return true;
81+
}
82+
83+
private static object GetPathComponentValue(object container, PropertyPathComponent component) =>
84+
component.PropertyName == null
85+
? ((IList)container)[component.ElementIndex]
86+
: GetMemberValue(container, component.PropertyName);
87+
88+
private static void SetPathComponentValue(object container, PropertyPathComponent component, object value)
89+
{
90+
if (component.PropertyName == null)
91+
{
92+
((IList)container)[component.ElementIndex] = value;
93+
}
94+
else
95+
{
96+
SetMemberValue(container, component.PropertyName, value);
97+
}
98+
}
99+
100+
private static object GetMemberValue(object container, string name)
101+
{
102+
if (container == null)
103+
return null;
104+
var type = container.GetType();
105+
var members = type.GetMember(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
106+
foreach (var member in members)
107+
{
108+
switch (member)
109+
{
110+
case FieldInfo field:
111+
return field.GetValue(container);
112+
case PropertyInfo property:
113+
return property.GetValue(container);
114+
}
115+
}
116+
117+
return null;
118+
}
119+
120+
private static void SetMemberValue(object container, string name, object value)
121+
{
122+
var type = container.GetType();
123+
var members = type.GetMember(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
124+
foreach (var member in members)
125+
{
126+
switch (member)
127+
{
128+
case FieldInfo field:
129+
field.SetValue(container, value);
130+
return;
131+
case PropertyInfo property:
132+
property.SetValue(container, value);
133+
return;
134+
}
135+
}
136+
137+
Debug.Assert(false, $"Failed to set member {container}.{name} via reflection");
138+
}
139+
}
140+
}

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Extensions/SerializedPropertyExtensions.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/AutSoft.UnityResourceGenerator/Editor/ResourceGeneratorSettings.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using AutSoft.UnityResourceGenerator.Editor.Generation;
1+
using AutSoft.UnityResourceGenerator.Editor.Extensions;
2+
using AutSoft.UnityResourceGenerator.Editor.Generation;
23
using System;
34
using System.Collections.Generic;
45
using UnityEditor;
@@ -51,44 +52,40 @@ public ResourceData(string className, string[] fileExtensions, bool isResource)
5152
public bool LogError => _logError;
5253
public IReadOnlyList<ResourceData> Data => _data;
5354

54-
public static ResourceGeneratorSettings GetOrCreateSettings
55-
(
56-
string folderPath = null,
57-
string baseNamespace = null,
58-
string className = null,
59-
bool? logInfo = null,
60-
bool? logError = null
61-
)
55+
public static ResourceGeneratorSettings GetOrCreateSettings()
6256
{
6357
var settings = AssetDatabase.LoadAssetAtPath<ResourceGeneratorSettings>(SettingsPath);
6458
if (settings != null) return settings;
6559

6660
settings = CreateInstance<ResourceGeneratorSettings>();
6761

68-
settings._folderPath = folderPath ?? string.Empty;
69-
settings._baseNamespace = baseNamespace ?? "Resources";
70-
settings._className = className ?? "ResourcePaths";
71-
settings._logInfo = logInfo ?? false;
72-
settings._logError = logError ?? true;
62+
settings._folderPath = string.Empty;
63+
settings._baseNamespace = "Resources";
64+
settings._className = "ResourcePaths";
65+
settings._logInfo = false;
66+
settings._logError = true;
7367

68+
settings._data = CreateDefaultFileMappings();
69+
70+
AssetDatabase.CreateAsset(settings, SettingsPath);
71+
AssetDatabase.SaveAssets();
72+
73+
return settings;
74+
}
75+
76+
private static List<ResourceData> CreateDefaultFileMappings() =>
7477
// https://docs.unity3d.com/Manual/BuiltInImporters.html
75-
settings._data = new List<ResourceData>
78+
new List<ResourceData>
7679
{
7780
new ResourceData("Scenes", new[]{"*.unity"}, false),
7881
new ResourceData("Prefabs", new[]{"*.prefab"}, true),
7982
new ResourceData("Materials", new[]{"*.mat"}, true),
80-
new ResourceData("AudioClips", new[]{"*.ogg", "*.aif", "*.aiff", "*.flac", "*.mp3", "*.mod", "*.it", "*.s3m", "*.xm"}, true),
83+
new ResourceData("AudioClips", new[]{"*.ogg", "*.aif", "*.aiff", "*.flac", "*.mp3", "*.mod", "*.it", "*.s3m", "*.xm", "*.wav"}, true),
8184
new ResourceData("Sprites", new[]{"*.jpg", "*.jpeg", "*.tif", "*.tiff", "*.tga", "*.gif", "*.png", "*.psd", "*.bmp", "*.iff", "*.pict", "*.pic", "*.pct", "*.exr", "*.hdr"}, true),
8285
new ResourceData("TextAssets", new[]{"*.txt", "*.html", "*.htm", "*.xml", "*.bytes", "*.json", "*.csv", "*.yaml", "*.fnt"}, true),
8386
new ResourceData("Fonts", new[]{"*.ttf", "*.dfont", "*.otf", "*.ttc"}, true)
8487
};
8588

86-
AssetDatabase.CreateAsset(settings, SettingsPath);
87-
AssetDatabase.SaveAssets();
88-
89-
return settings;
90-
}
91-
9289
[SettingsProvider]
9390
public static SettingsProvider CreateSettingsProvider() =>
9491
new SettingsProvider("Project/ResourceGenerator", SettingsScope.Project)
@@ -103,6 +100,9 @@ public static SettingsProvider CreateSettingsProvider() =>
103100
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_className)), new GUIContent("Class name"));
104101
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_logInfo)), new GUIContent("Log Infos"));
105102
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_logError)), new GUIContent("Log Errors"));
103+
104+
if (GUILayout.Button("Reset file mappings")) settings.FindProperty(nameof(_data)).SetValue(CreateDefaultFileMappings());
105+
106106
EditorGUILayout.PropertyField(settings.FindProperty(nameof(_data)), new GUIContent("Data"));
107107

108108
settings.ApplyModifiedProperties();

UnityResourceGenerator/Assets/ResourceGenerator.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ MonoBehaviour:
4141
- '*.it'
4242
- '*.s3m'
4343
- '*.xm'
44+
- '*.wav'
4445
_isResource: 1
4546
- _className: Sprites
4647
_fileExtensions:

0 commit comments

Comments
 (0)