Table of contents
Intro
There's a good chance that you've stumbled across the term serialization.
It allows us to see variables in the inspector and it's easier to parse data into a JSON when building a saving system, but there's of course more depth to it.
The Problem
I wanted a solution for a one-line serialized property that possesses a public getter.
You can serialize most fields by using either the public keyword or the [SerializeField] attribute.
public GameObject qinu;
[SerializeField] GameObject pamo;
I wrote more about when to use which in my recent [SerializeField] Shortcut Post.
The getters and setters that the field declarations above by default have are not written out as of now.
Here's what that would look like:
public GameObject qinu
{
get { return qinu; }
set { qinu = value; }
}
What if we need to retrieve a private serialized property from another script?
We could write a getter method like this:
[SerializeField] GameObject qinu;
public GameObject GetQinu()
{
return qinu;
}
It works but doing this for every field gets way too much effort and fills up too much space.
There's also the possibility of this syntax, basically creating a second public field that gets our target field when asked.
[SerializeField] GameObject qinu;
public GameObject Qinu => qinu;
This works but is a pain to write as well, you want a different name for each time and a different type as well, so you can't simply boil that down to a shortcut as I did before.
That's why I tried a third syntax approach, here it is:
[field: SerializeField] public GameObject Qinu { get; private set; }
If I saw this for the first time I would probably say "iu" out loud,
but wait give it a chance.
It's a one-liner now and we only need to change the type and name.
Why do we need to type [field: SerializeField] you may ask?
It doesn't show up in the inspector without it.
I only found that "It's needed to serialize an auto-property's backing field."
What now?
Now that we have the desired syntax, we can convert it into a shortcut.
I'll be using the software AutoHotkey again like in my previous [SerializeField] and Unity Library Clearing posts, so sorry my fellow Mac users.
I want to type "pg" which stands for public getter and paste the syntax.
The script for AutoHotkey:
::pg::[field: SerializeField] public float Field {{} get; private set; {}}
return
::Text:: is the input which should be corrected to the text to the right.
We need to encapsulate each { with {} to type it out.
Last Setup
To use the shortcut download AutoHotkey and the script 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 aware of serialization and some different syntax of it and you have a shortcut for speeding up your workflow if necessary.
Take care!