Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vGuMuSs · Feb 17, 2018 at 01:54 PM · expression

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yanka33 · May 29, 2019 at 10:05 AM 0
Share

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)^2and 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)^2but it also gives 10 ins$$anonymous$$d of 12.1.

avatar image Bunny83 Yanka33 · May 29, 2019 at 11:44 AM 1
Share

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.

avatar image Yanka33 Bunny83 · Jun 04, 2019 at 08:32 AM 0
Share

The fix did the job! Thank you for your answer! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Predict Where ScrollRect Will Land Based on Velocity 1 Answer

It is not possible to invoke an expression of type 'SpriteManager[]'. 1 Answer

B83 Expression parser problem 1 Answer

Something wrong with this script? 3 Answers

Messaging System ,return value 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges