This library "CSharpScriptGenerator" is a library for generating C# scripts.
Since it supports UPM, you can utilize it directly for script generation in Unity, or you can use it as a supplement to SourceGenerator.
| Environment | Version |
|---|---|
| Unity | 2021.3.38f1, 2022.3.20f1 |
| .Net | 4.x, Standard 2.0, Standard 2.1 |
- Open [Window > Package Manager].
- click [+ > Add package from git url...].
- Type
https://github.com/Katsuya100/CSharpScriptGenerator.git?path=packagesand click [Add].
The above method may not work well in environments where git is not installed.
Download the appropriate version of com.katuusagi.csharpscriptgenerator.tgz from Releases, and then [Package Manager > + > Add package from tarball...] Use [Package Manager > + > Add package from tarball...] to install the package.
Download the appropriate version of Katuusagi.CSharpScriptGenerator.unitypackage from Releases and Import it into your project from [Assets > Import Package > Custom Package].
The following notation can be used to output C# Script.
var root = new RootGenerator();
root.Generate(rg =>
{
rg.Using.Generate("UnityEngine");
rg.Namespace.Generate("Example.NameSpace", ng =>
{
ng.Type.Generate(ModifierType.Public | ModifierType.Class, "HelloWorld", tg =>
{
tg.Method.Generate(ModifierType.Public, "void", "Dump", mg =>
{
mg.Param.Generate("bool", "check");
mg.Statement.Generate("if (check)", () =>
{
mg.Statement.Generate("Debug.Log(\"HelloWorld\");");
});
});
});
});
});
var builder = new CSharpScriptBuilder();
builder.BuildAndNewLine(root.Result);
var script = builder.ToString();
File.WriteAllText("HelloWorld.cs", script);using UnityEngine;
namespace Example.NameSpace
{
public class HelloWorld
{
public void Dump(bool check)
{
if (check)
{
Debug.Log("HelloWorld");
}
}
}
}