A Unity compatible C# source generator to create getter and setter properties for fields to make specifically readonly scriptable objects.
If you want readonly scriptable objects, you must either write getter properties for serialized fields or add [field: SerializedField]
to property but second options serialized as <foo>k__BackingField
and also has other downsides. If you choose writing getter properties, you must write many boilerplate codes. This source generator writes these boilerplate code for you.
For more info about [field: SerializedField]
Unity Forum
Minimum supported Unity version is Unity 2021.2
because of netstandard2.1
support of the project.
- Download
Serenity.PropertyGenerator.dll
file from Release Page - Move the dll file into the Assets folder, or any folder nested inside of the Assets folder, in your Unity project.
- Click on the .dll file inside the Asset Browser inside Unity to open the
Plugin Inspector
window. - Under the
Select platforms for plugin
heading, disableAny Platform
. - Under the
Include Platforms
heading, disableEditor
andStandalone
. - Under the
Asset Labels
heading in the Plugin Inspector window, click on the blue label icon to open theAsset Labels
sub-menu. - Create and assign a new label called RoslynAnalyzer
For more information on Unity and Source Generator, please refer to the Unity Documentation.
To use the generator, add the [Getter]
or [Setter]
or [Getter, Setter]
attribute to a field in partial class, struct or record. The attributes are also auto generated. It will generate a partial type under same namespace.
using System;
using Serenity.Property;
namespace FooNamespace
{
public partial class Foo : ScriptableObject
{
[SerializedField, Getter]
private int _intGetterTest;
[SerializedField, Setter]
private int[] _intArraySetterTest;
[Getter, Setter]
private int _intGetterSetterTest;
}
}
Source generator generates a class called Foo.g.cs
which contains getter and setter properties.
namespace FooNamespace
{
public partial class Foo : ScriptableObject
{
public int IntGetterTest
{
get => _intGetterTest;
}
public int[] IntArraySetterTest
{
set => _intArraySetterTest = value;
}
public int IntGetterSetterTest
{
get => _intGetterSetterTest;
set => _intGetterSetterTest = value;
}
}
}
Serenity.PropertyGenerator is Copyright © 2023 Serenity Studios under the MIT License.