Skip to content

Commit 93e3830

Browse files
authored
Merge pull request #1 from AnnulusGames/change-property-name-arg
Change: PropertyName to constructor argument
2 parents 1ea656f + 2346085 commit 93e3830

6 files changed

Lines changed: 56 additions & 9 deletions

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ void Awake()
9797
}
9898
```
9999

100+
It is also possible to specify the property name of the generated cache. If not specified, the class name of the target component converted to lower camel case will be used.
101+
102+
```cs
103+
using UnityEngine;
104+
using ComponentCacheGenerator;
105+
106+
[GenerateComponentCache(typeof(FooComponent), "foo")]
107+
[GenerateComponentCache(typeof(BarComponent), "bar")]
108+
[GenerateComponentCache(typeof(BazComponent), "baz")]
109+
public partial class SomeBehaviour : MonoBehaviour
110+
{
111+
void Update()
112+
{
113+
foo.Foo();
114+
bar.Bar();
115+
bazCBaz();
116+
}
117+
}
118+
```
119+
100120
## Generation Options
101121

102122
You can specify generation code settings by specifying values for properties in the `[GenerateComponentCache]` attribute.
@@ -105,7 +125,6 @@ You can specify generation code settings by specifying values for properties in
105125
| - | - |
106126
| SearchScope | Specifies the scope to search for components. If multiple are specified, the search will be performed in the order of Self > Children > Parent. (Default is Self) |
107127
| IsRequired | If IsRequired is true, an exception will be thrown if the component is not found when `CacheComponents()` is called. If the search scope is Self, `[RequireComponent]` attribute will be automatically generated. (Default is true) |
108-
| PropertyName | Specifies the name of the generated property. If not specified, the lower camel case version of the target component's class name will be used. |
109128

110129
## License
111130

README_JA.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ void Awake()
9797
}
9898
```
9999

100+
また、生成されるキャッシュのプロパティ名を指定することも可能です。指定がない場合には対象のコンポーネントのクラス名をlower camel caseに変換した名前が使用されます。
101+
102+
```cs
103+
using UnityEngine;
104+
using ComponentCacheGenerator;
105+
106+
[GenerateComponentCache(typeof(FooComponent), "foo")]
107+
[GenerateComponentCache(typeof(BarComponent), "bar")]
108+
[GenerateComponentCache(typeof(BazComponent), "baz")]
109+
public partial class SomeBehaviour : MonoBehaviour
110+
{
111+
void Update()
112+
{
113+
foo.Foo();
114+
bar.Bar();
115+
bazCBaz();
116+
}
117+
}
118+
```
119+
100120
## 生成オプション
101121

102122
`[GenerateComponentCache]`属性のプロパティの値を指定することで、生成コードの設定を行うことが可能です。
@@ -105,7 +125,6 @@ void Awake()
105125
| - | - |
106126
| SearchScope | コンポーネントを探索する範囲を指定します。複数指定された場合はSelf > Children > Parentの順番で探索を行います。(デフォルトはSelf) |
107127
| IsRequired | IsRequiredがtrueの場合、`CacheComponents()`でコンポーネントが見つからなかったときに例外をスローします。また、探索範囲がSelfの場合は`[RequireComponent]`属性を自動で生成します。(デフォルトはtrue) |
108-
| PropertyName | 生成するプロパティの名前を指定します。指定がない場合には対象のコンポーネントのクラス名をlower camel caseに変換した名前が使用されます。 |
109128

110129
## ライセンス
111130

src/ComponentCacheGenerator.SourceGenerator/ComponentCacheGenerator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2828
context.RegisterSourceOutput(source, Emit);
2929
}
3030

31-
static void GetAttributeParameters(AttributeData attr, out INamedTypeSymbol targetType, out ComponentSearchScope searchScope, out bool isRequired, out string? propertyName)
31+
static void GetAttributeParameters(AttributeData attr, out INamedTypeSymbol targetType, out string? propertyName, out ComponentSearchScope searchScope, out bool isRequired)
3232
{
3333
targetType = (INamedTypeSymbol)attr.ConstructorArguments[0].Value!;
34+
propertyName = attr.ConstructorArguments.Length == 2
35+
? (string)attr.ConstructorArguments[1].Value!
36+
: null;
3437

3538
var properties = attr.NamedArguments.ToDictionary(kv => kv.Key, kv => kv.Value);
3639
searchScope = properties.TryGetValue("SearchScope", out var searchScopeConstant) ? (ComponentSearchScope)searchScopeConstant.Value! : ComponentSearchScope.Self;
3740
isRequired = properties.TryGetValue("IsRequired", out var isRequiredConstant) ? (bool)isRequiredConstant.Value! : true;
38-
propertyName = properties.TryGetValue("PropertyName", out var propertyNameConstant) ? (string)propertyNameConstant.Value! : null;
3941
}
4042

4143
static bool Verify(SourceProductionContext context, TypeDeclarationSyntax typeSyntax, INamedTypeSymbol targetType)
@@ -99,7 +101,7 @@ private void Awake()
99101

100102
foreach (var attribute in source.Attributes)
101103
{
102-
GetAttributeParameters(attribute, out var targetType, out var searchScope, out var isRequired, out var propertyName);
104+
GetAttributeParameters(attribute, out var targetType, out var propertyName, out var searchScope, out var isRequired);
103105
var targetTypeFullName = targetType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
104106
var cachePropertyName = propertyName ?? MemberNames.ClassNameToFieldName(targetType.Name);
105107
cachePropertyCode.AppendLine($"private {targetTypeFullName} {cachePropertyName} {{ get; set; }}");

src/ComponentCacheGenerator/Assets/ComponentCacheGenerator/Runtime/GenerateComponentCacheAttribute.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ public GenerateComponentCacheAttribute(Type componentType)
1818
ComponentType = componentType;
1919
}
2020

21-
public Type ComponentType { get; }
21+
public GenerateComponentCacheAttribute(Type componentType, string propertyName)
22+
{
23+
ComponentType = componentType;
24+
PropertyName = propertyName;
25+
}
26+
27+
public Type ComponentType { get; } = null;
28+
public string PropertyName { get; } = null;
29+
2230
public ComponentSearchScope SearchScope { get; set; } = ComponentSearchScope.Self;
2331
public bool IsRequired { get; set; } = true;
24-
public string PropertyName { get; set; } = null;
2532
}
2633
}

src/ComponentCacheGenerator/Assets/Sandbox/Sandbox.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using UnityEngine;
22
using ComponentCacheGenerator;
33

4-
[GenerateComponentCache(typeof(Rigidbody), PropertyName = "rb")]
5-
[GenerateComponentCache(typeof(SampleComponent), PropertyName = "sample", SearchScope = ComponentSearchScope.Children)]
4+
[GenerateComponentCache(typeof(Rigidbody), "rb")]
5+
[GenerateComponentCache(typeof(SampleComponent), "sample", SearchScope = ComponentSearchScope.Children)]
66
public partial class Sandbox : MonoBehaviour
77
{
88
void FixedUpdate()

0 commit comments

Comments
 (0)