Skip to content

Commit f6e765c

Browse files
authored
Merge pull request #2 from myunitytools/feature/iso-date
[Feature] Add ISO date format by default
2 parents 770de76 + 43b70bf commit f6e765c

File tree

6 files changed

+58
-46
lines changed

6 files changed

+58
-46
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Change Log
33

44
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [2.2.0] - 2022-09-13
7+
- Added: Support for ISO date format by default.
8+
69
## [2.1.0] - 2022-09-13
710
- Changed: Moved repository to My Unity Tools organization
811
- Changed: Updated package name and author.
@@ -39,6 +42,7 @@ All notable changes to this project will be documented in this file. This projec
3942
## [1.0.0] - 2019-07-27
4043
- Initial Asset Store release.
4144

45+
[2.2.0]: https://github.com/myunitytools/script-template/compare/2.1.0...2.2.0
4246
[2.1.0]: https://github.com/myunitytools/script-template/compare/2.0.1...2.1.0
4347
[2.0.1]: https://github.com/myunitytools/script-template/compare/2.0.0...2.0.1
4448
[2.0.0]: https://github.com/myunitytools/script-template/compare/1.3.0...2.0.0

Editor/ScriptKeywordProcessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* ScriptKeywordProcessor.cs
33
* Created by: João Borks [[email protected]]
4-
* Created on: 2/19/2019 (en-US)
4+
* Created on: 2019-02-19
55
* Tips from https://forum.unity3d.com/threads/c-script-template-how-to-make-custom-changes.273191/
66
*/
77

@@ -12,10 +12,10 @@
1212
namespace MyUnityTools.ScriptTemplates
1313
{
1414
/// <summary>
15-
/// This class listens to <see cref="UnityEditor.AssetModificationProcessor"/>'s <see cref="OnWillCreateAsset(string)"/> event that executes whenever a new asset is created
15+
/// This class listens to <see cref="AssetModificationProcessor"/>'s <see cref="OnWillCreateAsset(string)"/> event that executes whenever a new asset is created
1616
/// and replaces the keywords on scripts to what we defined on our <see cref="ScriptTemplatesEditor"/>
1717
/// </summary>
18-
public class ScriptKeywordProcessor : UnityEditor.AssetModificationProcessor
18+
public class ScriptKeywordProcessor : AssetModificationProcessor
1919
{
2020
public static void OnWillCreateAsset(string path)
2121
{
@@ -34,8 +34,8 @@ public static void OnWillCreateAsset(string path)
3434
return;
3535

3636
string fileContent = System.IO.File.ReadAllText(path);
37-
fileContent = fileContent.Replace("#AUTHOR#", $"{EditorPrefs.GetString(ScriptTemplatesEditor.AuthorNameField)}{(EditorPrefs.HasKey(ScriptTemplatesEditor.AuthorEmailField) ? $" [{EditorPrefs.GetString(ScriptTemplatesEditor.AuthorEmailField)}]" : string.Empty)}");
38-
fileContent = fileContent.Replace("#CREATIONDATE#", $"{System.DateTime.Now.ToString("d", CultureInfo.CurrentCulture)} ({CultureInfo.CurrentCulture.Name})");
37+
fileContent = fileContent.Replace("#AUTHOR#", $"{EditorPrefs.GetString(ScriptTemplatesEditor.AuthorNameKey)}{(EditorPrefs.HasKey(ScriptTemplatesEditor.AuthorEmailKey) ? $" [{EditorPrefs.GetString(ScriptTemplatesEditor.AuthorEmailKey)}]" : string.Empty)}");
38+
fileContent = fileContent.Replace("#CREATIONDATE#", EditorPrefs.GetBool(ScriptTemplatesEditor.UseLocalDateKey, false) ? $"{System.DateTime.Now.ToString("d", CultureInfo.CurrentCulture)} ({CultureInfo.CurrentCulture.Name})" : System.DateTime.Now.ToString("yyyy-MM-dd"));
3939

4040
System.IO.File.WriteAllText(path, fileContent);
4141
AssetDatabase.Refresh();

Editor/ScriptTemplatesEditor.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* CustomScriptTemplateEditor.cs
33
* Created by: João Borks [[email protected]]
4-
* Created on: 7/13/2019 (en-US)
4+
* Created on: 2019-07-13
55
*/
66

77
using System.IO;
@@ -19,29 +19,34 @@ public class ScriptTemplatesEditor : EditorWindow
1919
/// <summary>
2020
/// The name of the <see cref="authorName"/> string field saved on <see cref="EditorPrefs"/>
2121
/// </summary>
22-
public const string AuthorNameField = "com.myunitytools.cst.authorName";
22+
public const string AuthorNameKey = "com.myunitytools.cst.authorName";
2323
/// <summary>
2424
/// The name of the <see cref="authorEmail"/> string field saved on <see cref="EditorPrefs"/>
2525
/// </summary>
26-
public const string AuthorEmailField = "com.myunitytools.cst.authorEmail";
26+
public const string AuthorEmailKey = "com.myunitytools.cst.authorEmail";
27+
/// <summary>
28+
/// Should the date be formatted in the current locale or in the default ISO format?
29+
/// </summary>
30+
public const string UseLocalDateKey = "com.myunitytools.cst.localdate";
2731
/// <summary>
2832
/// The name of the <see cref="localGUID"/> string field saved on <see cref="EditorPrefs"/>
2933
/// </summary>
30-
public const string LocalGUIDField = "com.myunitytools.cst.localguid";
34+
public const string LocalGUIDKey = "com.myunitytools.cst.localguid";
3135
/// <summary>
3236
/// Package path for accessing script templates and visual elements
3337
/// </summary>
34-
public const string PackageRootPath = "Packages/com.myunitytools.scripttemplate/";
38+
public const string PackageRootPath = "Packages/com.myunitytools.script-template/";
3539
/// <summary>
3640
/// Project path for storing script templates
3741
/// </summary>
3842
public const string LocalTemplatesPath = "Assets/ScriptTemplates/";
43+
3944
/// <summary>
4045
/// Editor path for storing script templates
4146
/// </summary>
4247
public static readonly string EditorTemplatesPath = EditorApplication.applicationPath.Replace("Unity.exe", "") + "/Data/Resources/ScriptTemplates/";
4348

44-
const string EditorRestartMesssage = "Copying Script Templates will requre Editor restart in order to apply the changes. Do you want to restart now?";
49+
const string _editorRestartMesssage = "Copying Script Templates will requre Editor restart in order to apply the changes. Do you want to restart now?";
4550

4651
[MenuItem("Assets/Script Templates Editor", false, 800)]
4752
public static void ShowWindow() => GetWindow<ScriptTemplatesEditor>("Script Templates").minSize = new Vector2(300, 250);
@@ -62,35 +67,43 @@ void DrawInfoView()
6267
var saveButton = rootVisualElement.Q<Button>("save-button");
6368

6469
var authorField = rootVisualElement.Q<TextField>("author");
65-
authorField.SetValueWithoutNotify(EditorPrefs.GetString(AuthorNameField, string.Empty));
70+
authorField.SetValueWithoutNotify(EditorPrefs.GetString(AuthorNameKey, string.Empty));
6671
authorField.RegisterValueChangedCallback(e => saveButton.SetEnabled(!isAuthorUpdated(e.newValue) && !string.IsNullOrEmpty(authorField.value)));
6772

6873
var emailField = rootVisualElement.Q<TextField>("email");
69-
emailField.SetValueWithoutNotify(EditorPrefs.GetString(AuthorEmailField, string.Empty));
74+
emailField.SetValueWithoutNotify(EditorPrefs.GetString(AuthorEmailKey, string.Empty));
7075
emailField.RegisterValueChangedCallback(e => saveButton.SetEnabled(!isEmailUpdated(e.newValue) && !string.IsNullOrEmpty(authorField.value)));
7176

77+
var localDateToggle = rootVisualElement.Q<Toggle>("localdate");
78+
localDateToggle.SetValueWithoutNotify(EditorPrefs.GetBool(UseLocalDateKey, false));
79+
localDateToggle.RegisterValueChangedCallback(e => saveButton.SetEnabled(!isLocalDateUpdated(e.newValue)));
80+
7281
var clearButton = rootVisualElement.Q<Button>("clear-button");
73-
clearButton.SetEnabled(EditorPrefs.HasKey(AuthorNameField) || EditorPrefs.HasKey(AuthorEmailField));
82+
clearButton.SetEnabled(EditorPrefs.HasKey(AuthorNameKey) || EditorPrefs.HasKey(AuthorEmailKey));
7483
clearButton.clicked += () =>
7584
{
76-
EditorPrefs.DeleteKey(AuthorNameField);
77-
EditorPrefs.DeleteKey(AuthorEmailField);
85+
EditorPrefs.DeleteKey(AuthorNameKey);
86+
EditorPrefs.DeleteKey(AuthorEmailKey);
87+
EditorPrefs.DeleteKey(UseLocalDateKey);
7888
saveButton.SetEnabled(!string.IsNullOrEmpty(authorField.value));
7989
clearButton.SetEnabled(false);
8090
};
8191

8292
saveButton.SetEnabled(false);
8393
saveButton.clicked += () =>
8494
{
85-
EditorPrefs.SetString(AuthorNameField, authorField.value);
86-
EditorPrefs.SetString(AuthorEmailField, emailField.value);
95+
EditorPrefs.SetString(AuthorNameKey, authorField.value);
96+
EditorPrefs.SetString(AuthorEmailKey, emailField.value);
97+
EditorPrefs.SetBool(UseLocalDateKey, localDateToggle.value);
8798
saveButton.SetEnabled(false);
8899
clearButton.SetEnabled(true);
89100
};
90101

91-
bool isAuthorUpdated(string author) => author == EditorPrefs.GetString(AuthorNameField, string.Empty);
102+
bool isAuthorUpdated(string author) => author == EditorPrefs.GetString(AuthorNameKey, string.Empty);
103+
104+
bool isEmailUpdated(string email) => email == EditorPrefs.GetString(AuthorEmailKey, string.Empty);
92105

93-
bool isEmailUpdated(string email) => email == EditorPrefs.GetString(AuthorEmailField, string.Empty);
106+
bool isLocalDateUpdated(bool value) => value == EditorPrefs.GetBool(UseLocalDateKey, false);
94107
}
95108

96109
void DrawTemplateView()
@@ -124,7 +137,7 @@ void DrawTemplateView()
124137
var projectButton = rootVisualElement.Q<Button>("copy-project-button");
125138
projectButton.clicked += () =>
126139
{
127-
var shouldRestart = EditorUtility.DisplayDialog("Editor Restart", EditorRestartMesssage, "Yes", "No");
140+
var shouldRestart = EditorUtility.DisplayDialog("Editor Restart", _editorRestartMesssage, "Yes", "No");
128141
var length = templates.Length;
129142
if (!AssetDatabase.IsValidFolder(LocalTemplatesPath))
130143
AssetDatabase.CreateFolder("Assets", "ScriptTemplates");
@@ -137,7 +150,7 @@ void DrawTemplateView()
137150
var editorButton = rootVisualElement.Q<Button>("copy-editor-button");
138151
editorButton.clicked += () =>
139152
{
140-
var shouldRestart = EditorUtility.DisplayDialog("Editor Restart", EditorRestartMesssage, "Yes", "No");
153+
var shouldRestart = EditorUtility.DisplayDialog("Editor Restart", _editorRestartMesssage, "Yes", "No");
141154
var length = templates.Length;
142155
for (int i = 0; i < length; i++)
143156
File.Copy(AssetDatabase.GetAssetPath(templates[i]), EditorTemplatesPath + templates[i].name + ".txt");

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Usage
2727

2828
From `Assets/Script Templates Editor` you can open the **Script Templates Editor** Window.
2929

30-
![Custom Script Template Editor](https://user-images.githubusercontent.com/9505905/111533536-a70be580-8745-11eb-909c-b9a97ee0e311.png)
30+
![Custom Script Template Editor](https://user-images.githubusercontent.com/9505905/190041726-dcd85558-00fa-49d3-b0cc-03a20da335bd.png)
3131

32-
Here you can set your Author Name and Email to add to your script's signature header. You need to click save to store this data into
32+
Here you can set your Author Name and Email to add to your script's signature header. Also, you can choose to use the **local date format**: _9/13/2022 (en-US)_, for example, or the standard ISO format: _2022-09-13_. You need to click `Save` to store this data into
3333
the [Editor Prefs](https://docs.unity3d.com/ScriptReference/EditorPrefs.html). You also have the option to clear the saved data.
3434

3535
Immediately below that, we have a list of all included Script Templates and you can select them to locate the file.

UIToolkit/ScriptTemplateWindow.uxml

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<engine:UXML
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xmlns:engine="UnityEngine.UIElements"
5-
xmlns:editor="UnityEditor.UIElements"
6-
xsi:noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd"
7-
>
8-
<engine:Label text="Signature Info" class="bold"/>
9-
<engine:TextField name="author" label="Author"/>
10-
<engine:TextField name="email" label="Email (optional)"/>
11-
<engine:VisualElement class="button-group">
12-
<engine:Button name="clear-button" text="Clear"/>
13-
<engine:Button name="save-button" text="Save"/>
14-
</engine:VisualElement>
15-
<engine:Label text="Templates" class="bold"/>
16-
<engine:VisualElement class="button-group">
17-
<engine:Button name="copy-project-button" text="Copy to Project"/>
18-
<engine:Button name="copy-editor-button" text="Copy to Editor"/>
19-
</engine:VisualElement>
20-
<engine:ListView name="template-list"/>
21-
</engine:UXML>
1+
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
2+
<ui:Label text="Signature Info" class="bold" />
3+
<ui:TextField name="author" label="Author" />
4+
<ui:TextField name="email" label="Email (optional)" />
5+
<ui:Toggle label="Use Local Date" name="localdate" />
6+
<ui:VisualElement class="button-group">
7+
<ui:Button name="clear-button" text="Clear" />
8+
<ui:Button name="save-button" text="Save" />
9+
</ui:VisualElement>
10+
<ui:Label text="Templates" class="bold" />
11+
<ui:VisualElement class="button-group">
12+
<ui:Button name="copy-project-button" text="Copy to Project" />
13+
<ui:Button name="copy-editor-button" text="Copy to Editor" />
14+
</ui:VisualElement>
15+
<ui:ListView name="template-list" />
16+
</ui:UXML>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.myunitytools.script-template",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"displayName": "Script Templates",
55
"description": "This tool adds custom Script Templates to Unity that will serve as alternatives for the default \"C#Script\" that I personally always modify before writing my scripts.\nIn essence, it adds a signature header on every script with your name, email and creation date. \nAdditionally, it adds templates for others types of scripts rather than just Mono Behaviours.",
66
"unity": "2019.4",

0 commit comments

Comments
 (0)