Create a field attribute
Take this simple example.
[AttributeUsage(AttributeTargets.Field)]
public sealed class ExampleAttribute : InheritPrebuildAttribute {
public override string GetContent(System.Reflection.FieldInfo fieldInfo, ScriptInfos scriptInfo, uint fieldID, uint elementUID) {
// create name _name => Name
var name = fieldInfo.Name.Replace("_", "");
name = name.Substring(0, 1).ToUpperInvariant() + name.Substring(1);
// get type string
var type = UnityBitConverter.GetStringOfType(fieldInfo.FieldType);
return PrebuildAttribute.Template(MyTemplate,
"baseName", fieldInfo.Name,
"name", name,
"type", type
);
}
string MyTemplate {
get {
return @"
public ${type} ${name} {
get {
return ${baseName};
}
}
";
}
}
}
GetContent
is the entry point of method generation.
- fieldInfo is the standard Reflection.FieldInfo (name, type...)
- scriptInfo contains a lot of informations on the current script
- fieldID is a unique field id for this script
- elementID is a unique id for this project, it can be changed between each prebuild.
PrebuildAttribute.Template
is a helper to generate a string and replace variables in.
Here we created a property MyField from _myField;
This script generates this code:
[ExamplePrebuild] // see packer section
public class MyExampleBehaviour : MonoBehaviour {
[Example]
int _test;
#region Prebuild
public int Test() {
get {
return _test;
}
}
#endregion
}