3
\$\begingroup\$

In the Unity Editor, we can type an expression in a numeric field and press Enter to apply the result of the expression. For example, for a float field, we can type 10 * 5:

Expression 10 * 5 typed into float field

If we press enter, the expression is resolved to 50 and that value is applied to the field.

Resolved to 50

Addition, subtraction, and division are also supported.

We can also type some mathematical functions, e.g.:

floor(131.8)
ceil(22.2)
round(5.5)

What other expression syntax/functions are supported?

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

According to documentation for editing properties, the C# ExpressionEvaluator is used for processing numeric field expressions. The expressions that can be evaluated are:

  • arithmetic operators a+b, a-b, a*b, a/b,
  • power (a^b) and modulo (a%b) operators,
  • parentheses,
  • math functions sqrt(a), floor(a), ceil(a), round(a),
  • trigonometric functions cos(a), sin(a), tan(a) (argument expressed in radians), and a constant pi.

Supported number types are: int, float, long, double.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you, I hadn't found that documentation when I was searching. \$\endgroup\$
    – Kevin
    Feb 1 at 2:21
  • 1
    \$\begingroup\$ You're very welcome! I put the links in mostly for future reference - Unity is generally pretty good about adding versioned docs as the editor evolves & it's possible that in the future the supported options may change. I also thought it's possible that someone might want to use ExpressionEvaluator directly themselves for a plug-in, command line game or something & that seemed like another good reason to link it in. Glad it helped & happy deving! \$\endgroup\$
    – Pikalek
    Feb 1 at 2:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .