Skip to content

Commit 4d447d9

Browse files
committed
Change: Change: automatically generate Awake method if Awake is not found
1 parent 6e6adba commit 4d447d9

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/ComponentCacheGenerator.SourceGenerator/ComponentCacheGenerator.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ static void Emit(SourceProductionContext context, GeneratorAttributeSyntaxContex
7979
? null
8080
: typeSymbol.ContainingNamespace;
8181

82+
var hasAwake = typeSymbol.GetMembers("Awake").Any(x => x is IMethodSymbol methodSymbol && methodSymbol.Parameters.Length == 0);
83+
var awakeCode = hasAwake ? "" :
84+
"""
85+
private void Awake()
86+
{
87+
this.CacheComponents();
88+
}
89+
""";
90+
8291
var fullType = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
8392
.Replace("global::", "")
8493
.Replace("<", "_")
@@ -135,6 +144,8 @@ private void CacheComponents()
135144
{
136145
{{cacheMethodCode}}
137146
}
147+
148+
{{awakeCode}}
138149
}
139150
140151
{{(ns == null ? "" : "}")}}

src/ComponentCacheGenerator/Assets/Sandbox/Sandbox.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
[GenerateComponentCache(typeof(SampleComponent), PropertyName = "sample", SearchScope = ComponentSearchScope.Children)]
66
public partial class Sandbox : MonoBehaviour
77
{
8-
void Start()
9-
{
10-
CacheComponents();
11-
}
12-
138
void FixedUpdate()
149
{
1510
rb.velocity = Vector2.one;

0 commit comments

Comments
 (0)