#Unity code generation

1 messages · Page 1 of 1 (latest)

craggy pollen
#

Is there any library that wors for unity code generations, I would like to translate c# code into hlsl code so that I can have a shared codebase between my c# and shaders
if not I need help with translating a tokenized function body into further tokenTypes
current TokenTypes are 'N' for Name, 'r' for return, 'n' for new, and for operators and symboles just the symbol '=', '(', ...
I'm kinda stuggeling with finding an algorithm that covers all cases in a tokenstring like this

#
//These are the token types
public enum WordType
                {
                    AnyName,
                    TypeName,
                    VariableName,
                    NewKeyWord,
                    FunctionName,
                    ConstructorName,
                    Assign,
                    Space,
                    Paranthies,
                    ParanthiesClose,
                    Comma,
                    Number,
                    Other,
                    Return,
                }
                char[] serialize = new char[] { 
                    'N',
                    'N',
                    'N',
                    'n',
                    'N',
                    'N',
                    '=',
                    ' ',
                    '(',
                    ')',
                    ',',
                    '0',
                    'x',
                    'r'

                };
#

the tokens kinda look like the stiring in the body rn, would be really neat if someone had an idea on how to aproach that(it s best to not focus on the big ones yet)

//FunctionName Dependency TypeOf(floor)
float GradientNoise2DNoise(float x, float y)
{
    N N = (N)N(N) x 0;    N N = (N)N(N) x 0;    N x= (N)N(N);    N x= (N)N(N);    N N = N(N);    N N = N(N);    N N = NxNx x;    N N = NxN x 0x x;    r N(N(N(NxNx, N, N), N(NxNx, N x 0, N), N), N(N(NxN x 0x, N, N x 0), N(NxN x 0x, N x 0, N x 0), N), N);
}=>
int X = (int)math.floor(x) & 255; int Y = (int)math.floor(y) & 255; x -= (int)math.floor(x); y -= (int)math.floor(y); float u = Fade(x); float v = Fade(y); int A = perm[X] + Y; int B = perm[X + 1] + Y; return Lerp(Lerp(Grad(perm[A], x, y), Grad(perm[B], x - 1, y), u), Lerp(Grad(perm[A + 1], x, y - 1), Grad(perm[B + 1], x - 1, y - 1), u), v);

float GradientNoise2DLerp(float a, float b, float t)
{
    r (0 x N) x N x N x;
}=>
return (1 - t) * a + t * b;

UnknownType TypeOf(Void) GradientNoise2DNumberTester(UnknownType TypeOf(Void) a, float b, float c)
{
    N = n N(0, 0);
}=>
a = new float2(1.0f, 1f);


hardy blade
#

C# has code generation but this is fairly new (.NET 7, .NET 8). It's being added because code generation is pretty hot, but Unity will not have this yet

#

You're better off making a seperate project that does this for you

craggy pollen
#

how should I communicate with this project, and is there a way I can have it run in the backgorund?

hardy blade
#

Yes, C# has the ability to create background tasks, with or without libraries

#

And I guess you spoecify a path to your Unity project and have it put the code in there

craggy pollen
#

hm thanks for the advice I already thought about that but my only idea was to have a websocket connection

hardy blade
#

That's overkill

#

It's part of your development pipeline, no point in having a websocket unless you generate the code in production lol

#

I personally have a tool that I can call using a script, and this script compiles my project, and then it calls that tool

#

You can do something similar

craggy pollen
#

yes I know, that s why I was just implementing the parsing my self but my algoriths until now kinda sucksed, is your tool just a normal .net application?

hardy blade
#

Yes

#

Console app

#

But it is not for Unity

craggy pollen
#

hm so do you have a script that starts the application in background and then invokes functions form that somehow?

#

@hardy blade Is this what you mean?

 Assembly assembly = Assembly.LoadFile("PathToYourNewerProject.dll"); // Provide the actual path

        // Get the type of YourClass from the assembly
        Type yourClassType = assembly.GetType("YourNewerNamespace.YourClass"); // Adjust the namespace

        // Create an instance of YourClass
        object yourClassInstance = Activator.CreateInstance(yourClassType);

        // Get the MethodInfo for YourFunction
        MethodInfo yourFunctionMethod = yourClassType.GetMethod("YourFunction");

        // Invoke YourFunction on the instance
        yourFunctionMethod.Invoke(yourClassInstance, null);
    }
hardy blade
#

No

#

You wanted code generation

#

So you generate your code using the tool, and then either put that in your Unity project, or compile the result and make whatever you generated a plugin