[C#] [SerializeField] Shortcut
Prologue
This Tutorial only works for Windows users since the software AutoHotkey is only built for it.
Why Do We Use [SerializeField]?
Exposing your fields with the public keyword is known to be a bad practice since other scripts are allowed to access variables we don't want to be changed externally.
public Transform transformField;
Using the attribute [SerializeField] for fields still shows them in the inspector, without them being accessible from other scripts.
[SerializeField] Transform transformField;
What is my Problem with [SerializeField]?
Pressing "alt gr" + "8", "ser" (german keyboard layout) and typing [ser] with it for my IDE (Visual Studio) to prompt me for autocompletion takes way too much time for how often I want to use it.
What is my Solution?
I first searched for a text-pasting program and found AutoHotkey, which is useful for script and shortcut creation/automatization.
AutoHotkey uses little .ahk scripts to run in the background and I found 3 ways to script get our shortcut to work:
- Utilizing the autocorrect feature of AutoHotkey like this:
::sf::[SerializeField]
::Text:: is the input which should be corrected to the text to the right :, which is [SerializeField] here.
Just type sf + Space or any other "EndChar" like tab or enter Types out [SerializeField] Letter by letter.
It's important to note that this may look fancy but is not an instant paste, which is a problem for larger text strings.
I've been using that one for months now and am happy with it.
- Pasting it preconfigured from your clipboard with this:
s & f::
Clipboard := "[SerializeField]"
Send ^v
return
s::s
s & f means that the command below gets executed if you hold s and press f,
Clipboard:= "[SerializeField]" means that it pasts [SerializeField] in your clipboard
and "presses" ctr + v with "Send ^v".
The last s::s part means that your s input doesn't get consumed if f isn't pressed.
It's different from the prior version in three ways: You need to hold s and press f like the combo shortcut it is, it pasts the text instantly and it occupies your latest ClipBoard spot like this:
Printing/Sending it on shortcut pressed
s & f:: Send, [SerializeField] return s::s
It uses the hold and press Shortcut from 2., prints out [SerializeField] letter-by-letter, and doesn't consume your unused "a" inputs
(you couldn't type "a" otherwise)
The letter-by-letter typing can be interrupted by your next input which again steals us a fraction of time.Last Setup
To use a covered form of a [SerializeField] shortcut you'll have to download AutoHotkey and the desired script of the 3 above here.
Double-click the script/.ahk file to get it running
If you want the shortcut to always be active you can set it up as an autostart application, here's how:
Head to C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Username = your Windows user
Rightclick to open this in the file and select New>Shortcut
There you can paste the location where you saved your .ahk file
or paste it directly.Summary
You are now in possession of a quicker way to use the [SerializeField] attribute in your preferred form and got a quick intro to AutoHotkey.
The World is technically yours, take care.
Subscribe to my newsletter
Read articles from Tom Grzembke directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by