Skip to content

Code generation for Unity3D

Pravda project is able to generate auxiliary code for Unity that provides convenient way to call program's methods.
It uses meta information from the given bytecode to detect and analyse methods of the program.

Dependecies

Important note: The generated code uses Json .NET library for hadling Json. You can download Unity version of it from AssetStore.

How to generate code

pravda gen unity --input input.pravda

This command will create Assets folder (if it doesn't exist) and will place several files in it. You should include these files to your Unity project.

How to use generated code

Generated code uses coroutinues to handle DApp API requests.

For example: if you have Increment method that takes integer and increment it in your program generated code will include IncrementRequest class that handles DApp API request for calling Increment method.

var req = new IncrementRequest(address); // address of program in the blockchain as byte array
yield return req.Increment(42);
ProcessResult(req);

Internally IncrementRequest class will look like this:

public class IncrementRequest: ProgramRequest<int> // int is the type of result
{
    public IncrementRequest(byte[] programAddress) : base(programAddress) { } // address of deployed program in the blockchain

    protected override int ParseResult(string elem) // method that parses Pravda specific format to result value
    {
        return ExploadTypeConverters.ParseInt32(elem); // parse int32 see (data specification)[ref/vm/data.md]
    }

    public IEnumerator Increment(int arg0)
    {
        yield return SendRequest( // send http request to DApp API
            "Increment",
            new string[] { ExploadTypeConverters.PrintInt32(arg0) } // print int32 see (data specification)[ref/vm/data.md]
        );
    }
}

Expload.Unity.Codegen

Internally generated code uses auxiliary functionality from namespace called Expload.Unity.Codegen. Source file of this namespace situated here.