Create a method attribute
Take this simple example.
[AttributeUsage(AttributeTargets.Method)]
public sealed class ExampleAttribute : InheritPrebuildAttribute {
public override string GetContent(System.Reflection.MethodInfo methodInfo, ScriptInfos scriptInfo, uint methID, uint elementUID) {
return PrebuildAttribute.BaseMethod(methodInfo, "Hello" + methodInfo.Name, CreateMyMethodContent());
}
string CreateMyMethodContent() {
return PrebuildAttribute.Template(MyTemplate,
"var1", "Hello Prebuild",
"var2", "test"
);
}
string MyTemplate {
get {
return @"
Debug.Log(""${var1}"");
Debug.Log(""${var2}"");
";
}
}
}
GetContent
is the entry point of method generation.
- methodInfo is the standard Reflection.MethodInfo (name, arguments...)
- scriptInfo contains a lot of informations on the current script
- methID is a unique method id for this script
- elementID is a unique id for this project, it can be changed between each prebuild.
PrebuildAttribute.BaseMethod
is a helper to generate a string method based on another method.
PrebuildAttribute.Template
is a helper to generate a string and replace variables in.
Here we created a method with the same return but changed the name of 'mymethod' to Hellomymethod.
This script generates this code:
[ExamplePrebuild] // see packer section
public class MyExampleBehaviour : MonoBehaviour {
[Example]
void WorldTest() { }
#region Prebuild
private void HelloWorldTest() {
Debug.Log("Hello Prebuild");
Debug.Log("test");
}
#endregion
}