- Home /
ExpressionEvaluator - UnityEditor class
There is not much to say. I read the document but I couldn't figure out how to use this info to evaluate an expression. Can someone put that info into an example?
https://docs.unity3d.com/ScriptReference/ExpressionEvaluator.Evaluate.html
Answer by Bunny83 · Feb 17, 2018 at 03:22 PM
I'm not sure what information you expect here. It just evaluates simple mathematical expressions. It only supports the operators: "+", "-", "*", "/", "%" and "^" as well as brackets "(", ")" . It only supports literals as operands. So no support for variables or any kind of "functions". Since it's an editor class it can only be used in editor scripts.
I don't have the latest Unity version and in my version this class is still internal. I guess it's meant to evaluate the input string in number fields to directly convert an expression into a number. So when you enter 11/32
into the inspector it should directly convert to 0.34375
.
A simple example is this:
float f = ExpressionEvaluator.Evaluate<float>("(3+2)*7 - 5^2");
Here "f" should contain the number 10.0f
.
If you need something more advanced you may want to have a look at my ExpressionParser on the wiki. It actually creates an expression tree which can be evaluated later multiple times. It also supports functions and (input) variables. Of course this class is not limited to editor code. It can be used in a build as well. It will work on all platforms.
Hi @Bunny83 , I tried your ExpressionParser and it seems that it doesn't work with decimal values. For example, if I try to evaluate the expression 10*1,1^2
, it returns 10 ins$$anonymous$$d of 12.1. As a workaround, I can use 10*(11/10)^2
and it will give me the right result. But maybe I am just writing decimal number wrong for the parser to understand? NB: I also tried 10*(1,1)^2
but it also gives 10 ins$$anonymous$$d of 12.1.
It does work with decimal values. However it expects a culture that uses "." as decimal point. The comma is already used to seperate multiple parameter values. Actually I also live in a country that uses the comma as decimal point. However I've changed my culture settings years ago ^^.
Now that i thought about it I should have used the invariant culute to parse the double values since using the local culture doesn't make sense since the comma will never work anyways.
For a quick fix search for all double.TryParse
calls and replace
double.TryParse(text, out value);
with
double.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out value);
Now it should work as long as you use a value like 1.1
and not 1,1
. So an expression like 10*1.1^2
should evaluate to 12.1
.
Note that there is also a slightly more advanced version over here which also allows to evaluate boolean expressions. Though is has the same issue with the non-invariant culture. So in it's current form it wouldn't work with decimal values on machines that have a culture that uses the comma as decimal point. The same fix applies there as well. If i find the time I'll fix it.
The fix did the job! Thank you for your answer! :)