Skip to content

Commit d5a621d

Browse files
committed
fix: utility can now use in build event.
1 parent a4add5e commit d5a621d

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

Editor/USGEngine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public static bool ProcessFile(string assetsRelPath)
195195
if (!isSaveFile || sb == null || string.IsNullOrWhiteSpace(context.OutputPath))
196196
return false;
197197

198+
// NOTE: overwrite check must be done after Emit() due to allowing output path modification.
199+
// TODO: code generation happens but file is not written. any way to skip code generation?
198200
if (File.Exists(context.OutputPath) &&
199201
(!info.Attribute.OverwriteIfFileExists && !IgnoreOverwriteSettingByAttribute)
200202
)

Editor/USGUtility.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,29 @@ static void ForceGenerateSelectedScripts()
2626
}
2727

2828

29-
///<summary>UNSAFE on use in build event due to this method calls fancy UI methods and fire import event. Use `GetAssetPathByName()` instead.</summary>
30-
public static void ForceGenerateInEditor(string clsName, bool showInProjectPanel = true)
29+
///<summary>Force perform source code generation by class name.</summary>
30+
///<param name="showInProjectPanel">works only when Unity is not building app.</param>
31+
public static void ForceGenerateByName(string clsName, bool showInProjectPanel = true)
3132
{
3233
var path = GetAssetPathByName(clsName);
3334
if (path == null) return;
3435

36+
37+
USGEngine.IgnoreOverwriteSettingByAttribute = true; // always disabled after import event.
38+
39+
// NOTE: Invoking unity editor event while building app causes fatal error.
40+
// just generate code and not need to import it.
41+
if (BuildPipeline.isBuildingPlayer)
42+
{
43+
USGEngine.ProcessFile(path);
44+
return;
45+
}
46+
47+
// NOTE: When working in Unity Editor, Do NOT perform ProcessFile(...).
48+
// It will generate code correctly but generated source code is not imported.
49+
// To see changes, you need to import script and as you imagine, code generation happens again.
3550
if (showInProjectPanel)
3651
EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<Object>(path));
37-
USGEngine.IgnoreOverwriteSettingByAttribute = true; // always disabled after import event.
3852
AssetDatabase.ImportAsset(path);
3953
}
4054

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ There are utility functions to perform source code generation on build event.
208208

209209

210210
```csharp
211-
// search by class name if you don't know where it is.
212-
var assetPath = USGUtility.GetAssetPathByName(nameof(MinimalGenerator));
211+
// perform code generation by class name if you don't know where it is.
212+
USGUtility.ForceGenerateByName(nameof(MinimalGenerator));
213213

214-
// perform code generation.
214+
// perform code generation by known path.
215215
USGEngine.IgnoreOverwriteSettingByAttribute = true; // force overwrite
216216
USGEngine.ProcessFile(assetPath);
217217
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.sator-imaging.alt-source-generator",
33
"displayName": "Alternative Source Generator for Unity",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"unity": "2021.3",
66
"description": "Ease-of-Use Source Generator Alternative for Unity.",
77
"author": {

0 commit comments

Comments
 (0)