Skip to content

Commit a1dd782

Browse files
committed
add: script template + chore
1 parent 75c65f9 commit a1dd782

9 files changed

+228
-14
lines changed

Editor/EditorExtension.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#if UNITY_EDITOR
2+
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using UnityEditor;
6+
using UnityEngine;
7+
using UnityEditor.Callbacks;
8+
using System;
9+
using System.Reflection;
10+
11+
namespace SatorImaging.UnitySourceGenerator
12+
{
13+
class EditorExtension
14+
{
15+
const string ROOT_MENU = @"Assets/Unity Source Generator/";
16+
const string TEMPLATE_PATH = @"Packages/com.sator-imaging.alt-source-generator/Template/Template_";
17+
const string CS_EXT = @".cs";
18+
const string TXT_EXT = @".txt";
19+
20+
21+
[MenuItem(ROOT_MENU + "Force Generate while Overwriting Disabled")]
22+
static void ForceGenerateSelectedScripts()
23+
{
24+
USGEngine.IgnoreOverwriteSettingByAttribute = true; // always disabled after import event.
25+
26+
foreach (var GUID in Selection.assetGUIDs)
27+
{
28+
var path = AssetDatabase.GUIDToAssetPath(GUID);
29+
AssetDatabase.ImportAsset(path);
30+
}
31+
}
32+
33+
34+
[MenuItem(ROOT_MENU + "Method Generator Template", priority = 100)]
35+
static void MethodGenerator()
36+
{
37+
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(
38+
TEMPLATE_PATH + nameof(MethodGenerator) + TXT_EXT,
39+
nameof(MethodGenerator) + CS_EXT);
40+
}
41+
42+
43+
[MenuItem(ROOT_MENU + "Generic Generator Template", priority = 100)]
44+
static void GenericGenerator()
45+
{
46+
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(
47+
TEMPLATE_PATH + nameof(GenericGenerator) + TXT_EXT,
48+
nameof(GenericGenerator) + CS_EXT);
49+
}
50+
51+
52+
/* kept for reference. USGEngine update seems to work as expected.
53+
// NOTE: This will prevent Unity to reload updated scripts in background.
54+
// Unity doesn't invoke asset import event correctly when load script in background.
55+
[InitializeOnLoadMethod]
56+
static void InitializeFocusChangedEvent()
57+
{
58+
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/EditorApplication.cs#L275
59+
var focusChanged = typeof(EditorApplication).GetField("focusChanged",
60+
BindingFlags.Static | BindingFlags.NonPublic);
61+
if (focusChanged == null) return;
62+
63+
var focusAction = focusChanged.GetValue(null) as Action<bool>;
64+
focusAction -= OnEditorApplicationFocus;
65+
focusAction += OnEditorApplicationFocus;
66+
focusChanged.SetValue(null, focusAction);
67+
68+
s_restoreAutoRefresh = EditorPrefs.GetInt(PREF_AUTO_REFRESH, EditorPrefs.GetInt(PREF_AUTO_REFRESH_OLD));
69+
s_restoreDirMonitoring = EditorPrefs.GetBool(PREF_DIR_MONITORING);
70+
}
71+
72+
const string PREF_AUTO_REFRESH = "kAutoRefreshMode";
73+
const string PREF_AUTO_REFRESH_OLD = "kAutoRefresh";
74+
const string PREF_DIR_MONITORING = "DirectoryMonitoring";
75+
static bool s_restoreDirMonitoring;
76+
static int s_restoreAutoRefresh;
77+
static void OnEditorApplicationFocus(bool focus)
78+
{
79+
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/PreferencesWindow/AssetPipelinePreferences.cs#L94
80+
if (focus == false)
81+
{
82+
s_restoreAutoRefresh = EditorPrefs.GetInt(PREF_AUTO_REFRESH, EditorPrefs.GetInt(PREF_AUTO_REFRESH_OLD));
83+
s_restoreDirMonitoring = EditorPrefs.GetBool(PREF_DIR_MONITORING);
84+
//AssetDatabase.DisallowAutoRefresh();
85+
EditorApplication.LockReloadAssemblies();
86+
EditorPrefs.GetBool(PREF_DIR_MONITORING, false);
87+
EditorPrefs.SetInt(PREF_AUTO_REFRESH, 0);
88+
EditorPrefs.SetInt(PREF_AUTO_REFRESH_OLD, 0);
89+
}
90+
else
91+
{
92+
//AssetDatabase.AllowAutoRefresh();
93+
EditorApplication.UnlockReloadAssemblies();
94+
EditorPrefs.GetBool(PREF_DIR_MONITORING, s_restoreDirMonitoring);
95+
EditorPrefs.SetInt(PREF_AUTO_REFRESH, s_restoreAutoRefresh);
96+
EditorPrefs.SetInt(PREF_AUTO_REFRESH_OLD, s_restoreAutoRefresh);
97+
98+
AssetDatabase.Refresh();
99+
}
100+
Debug.Log($"[USG] Focus:{focus} / AutoRefresh:{s_restoreAutoRefresh} / DirMonitoring:{s_restoreDirMonitoring}");
101+
}
102+
*/
103+
104+
}
105+
}
106+
107+
#endif

Editor/EditorExtension.cs.meta

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

Editor/USGUtility.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@ namespace SatorImaging.UnitySourceGenerator
1313
{
1414
public class USGUtility
1515
{
16-
[MenuItem("Assets/Unity Source Generator/Force Generate while Overwriting Disabled")]
17-
static void ForceGenerateSelectedScripts()
18-
{
19-
USGEngine.IgnoreOverwriteSettingByAttribute = true; // always disabled after import event.
20-
21-
foreach (var GUID in Selection.assetGUIDs)
22-
{
23-
var path = AssetDatabase.GUIDToAssetPath(GUID);
24-
AssetDatabase.ImportAsset(path);
25-
}
26-
}
27-
28-
2916
///<summary>Force perform source code generation by class name.</summary>
3017
///<param name="showInProjectPanel">works only when Unity is not building app.</param>
3118
public static void ForceGenerateByName(string clsName, bool showInProjectPanel = true)

Sample/PanicMethodGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Sample
55
{
66
// HOW TO USE: Add the following attribute to *target* class.
77
// Note that target class must be defined as partial.
8-
// [UnitySourceGenerator(typeof(Sample.PanicMethodGenerator)]
8+
// [UnitySourceGenerator(typeof(Sample.PanicMethodGenerator))]
99
public class PanicMethodGenerator
1010
{
1111
static string OutputFileName() => "PanicMethod.cs"; // -> PanicMethod.<TargetClass>.<GeneratorClass>.g.cs

Template.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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if UNITY_EDITOR
2+
#pragma warning disable IDE0051
3+
4+
using SatorImaging.UnitySourceGenerator;
5+
using System.Text;
6+
using Debug = UnityEngine.Debug;
7+
8+
#ROOTNAMESPACEBEGIN#
9+
// NOTE: Copy this file to Assets/ folder to enable source generator.
10+
// USG process files in Assets/ folder only.
11+
[UnitySourceGenerator(OverwriteIfFileExists = false)]
12+
class #SCRIPTNAME#
13+
{
14+
static string OutputFileName() => "Test.cs"; // -> Test.<ClassName>.g.cs
15+
16+
static bool Emit(USGContext context, StringBuilder sb)
17+
{
18+
// write content into passed StringBuilder.
19+
sb.AppendLine($"Asset Path: {context.AssetPath}");
20+
sb.AppendLine($"Hello World from {typeof(#SCRIPTNAME#)}");
21+
22+
// you can modify output path. initial file name is that USG updated.
23+
// NOTE: USG doesn't care the modified path is valid or not.
24+
context.OutputPath += "_MyFirstTest.txt";
25+
26+
// return true to tell USG to write content into OutputPath. false to do nothing.
27+
return true;
28+
}
29+
30+
}
31+
#ROOTNAMESPACEEND#
32+
33+
#pragma warning restore IDE0051
34+
#endif

Template/Template_GenericGenerator.txt.meta

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

Template/Template_MethodGenerator.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#if UNITY_EDITOR
2+
#pragma warning disable IDE0051
3+
4+
using SatorImaging.UnitySourceGenerator;
5+
using System.Text;
6+
using Debug = UnityEngine.Debug;
7+
8+
#ROOTNAMESPACEBEGIN#
9+
// HOW TO USE: Add the following attribute to *target* class.
10+
// Note that target class must be defined as partial.
11+
// [UnitySourceGenerator(typeof(#SCRIPTNAME#), OverwriteIfFileExists = false)]
12+
public class #SCRIPTNAME#
13+
{
14+
const string METHOD_NAME = "Panic";
15+
static string OutputFileName() => METHOD_NAME + ".cs"; // -> Panic.<TargetClass>.<GeneratorClass>.g.cs
16+
17+
static bool Emit(USGContext context, StringBuilder sb)
18+
{
19+
if (!context.TargetClass.IsClass || context.TargetClass.IsAbstract)
20+
return false; // return false to tell USG doesn't write file.
21+
22+
// code generation
23+
sb.Append($@"
24+
using System.Collections;
25+
using System.Collections.Generic;
26+
using UnityEngine;
27+
28+
namespace {context.TargetClass.Namespace}
29+
{{
30+
internal partial class {context.TargetClass.Name}
31+
{{
32+
public void {METHOD_NAME}()
33+
{{
34+
");
35+
// Method Implementation
36+
var INDENT = " ";
37+
38+
39+
40+
// End of Method Implementation
41+
sb.Append($@"
42+
}}
43+
}}
44+
}}
45+
");
46+
return true;
47+
}
48+
49+
}
50+
#ROOTNAMESPACEEND#
51+
52+
#pragma warning restore IDE0051
53+
#endif

Template/Template_MethodGenerator.txt.meta

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

0 commit comments

Comments
 (0)