PandemoniumSyndicate
PandemoniumSyndicate

Reputation: 2955

Unity3d: Execute arbitrary javascript from C# object

I would like to

string expression = "2+2";

public string evaluateExpresion(expression)
{
   return executeJavascript(expression); // Magic javascript executor
}

Debug.Log(evaluateExpression(expression); 

How can I accomplish this?

Upvotes: 2

Views: 1046

Answers (1)

kgd
kgd

Reputation: 1665

You can create a javascript file with your desired functions and you can call them in your c# files if I don't misunderstand what you mean. In your example you can create:

// JavaScriptExpressions.js

function executeExpression( x : string ) {
    // Some code here..
}

JavaScript file and you can access the function by dragging js to the same transform with your c# file or you can drag it to a different transform. Or you can set it static if it is not connected to mono library. Here is the sample code for c#:

// YourCsharp.cs
string expression = "2+2";

public string evaluateExpresion(string expression) { 
    return transform.GetComponent<JavaScriptExpressions>().executeExpression(expression);
}

Debug.Log(evaluateExpression(expression));

Upvotes: 1

Related Questions