I’ve been sorely missing macros and templates from c++, and would like to build my own precompiler to do some text substitution in my code.
It would be a script in c# that picks up my c# files from the unity project, does my search replace, and writes the modified files to a temporary location, then has Unity compile the temporary code as it normally does.
Does anyone know if this is possible, and where I might start?
In C# you can get similar features with generics and a limited set of macros… you’d have to replace anything aside from the basic #define X with a method though.
#define DEBUG
using UnityEngine;
using System.Collections;
public class GenericsTest : MonoBehaviour
{
// Use this for initialization
void Start () {
MyGenericMethod<int>();
MyGenericMethod<float>();
}
// Update is called once per frame
void Update () {
}
public void MyGenericMethod<T>()
{
T x;
#if DEBUG
Debug.Log("Type of x = " + typeof(T));
#endif
}
}
This is an example of both, though the whole ‘#if DEBUG’ is a little silly in Unity of course. Generics can take generic parameters just like templates in C++.
I have no idea how to go about writing a pre-processor in Unity though.