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 CarlLawl · Jun 29, 2011 at 01:13 AM · stringeval

c# Eval()?

Is there anyway of taking a string and parsing it as code?

like

 public string str = " if (1 == 1) { a = b; } ";

ive had a look on google apparently there are ways of doing it but its confusing! so i wondered if anyone could explain it better or give me an example?

Thanks


EDIT:

in a Custom Editor Textarea(string) the user could write

 varX = 5;

or

 if (1 == 1) { //do this }

if that is written in the textarea, it will be saved as a string

what im trying to do is take that string, and run whats inside it (in laments terms from "varX = 5;" to just varX = 5; so that it will actually parse the code as code not a string

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

3 Replies

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

Answer by Bunny83 · Jun 29, 2011 at 02:34 AM

Why don't you say that you write an editor script? At runtime the compiler isn't available because it isn't part of the engine, but in the editor you can just save your code in a .cs file and let it compile by Unity like any other script.

The question is why do want to do this yourself? In a proper editor like VisualStudio you can write you scripts much faster and have intelliSense. You can of course create some kind of template script and insert the code into a special method and save the script as seperate file.

Take a look at the MonoScript class. It represents the text part of a script asset. It also has a function (MonoScript.GetClass) that returns the Type-Object for the containing class which can be used with AddComponent.

The compiling takes some time so you have to find a way to do all that smoothly. EditorApplication.isCompiling might be helpful.

Comment
Add comment · Show 4 · 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 Eric5h5 · Jun 29, 2011 at 03:35 AM 1
Share

The compiler is available at runtime and is part of the engine, otherwise eval wouldn't work in JS. (Not in the webplayer though, only standalones.)

avatar image CarlLawl · Jun 29, 2011 at 03:49 AM 0
Share

I think I wrote it incorrectly an example of what im trying to do is: in a Custom Editor Textarea(string) the user could write

varX = 5;

or

if (1 == 1) { //do this }

if that is written in the textarea, it will be saved as a string

what im trying to do is take that string, and run whats inside it (in laments terms from "varX = 5;" to just varX = 5; so that it will actually parse the code as code not a string

avatar image Bunny83 · Jun 30, 2011 at 04:06 PM 0
Share

@Eric5h5:
If you mean the runtime-compiler of .Net, yes it is available but as you said only in standalone ($$anonymous$$icrosoft.CSharp and System.CodeDom). I think that's a bit over the edge for "simple" tasks since you can use the created assemblies only through reflection (or you implement some kind of interface).

avatar image Eric5h5 · Jun 30, 2011 at 04:19 PM 0
Share

@Bunny83: I mean that you can write any arbitrary code strings in JS and have them compiled and executed at runtime using eval().

avatar image
0

Answer by Suyuanhan · Jun 29, 2011 at 01:45 AM

I found it may need other Class like Jscript.NET, but I don't know it is suitable for U3D or not?

Comment
Add comment · 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
0

Answer by testure · Jun 29, 2011 at 01:47 AM

You can't do this in C#, the closest you can come is to use reflection to get a property, and then envoke it. but that's not going to help you evaluate straight up code.

What are you trying to do, anyway? There may be a better way of doing it that you might not have thought about.

If you absolutely need to do something like your example, and you have a predictable set of information, you could just parse the string and evaluate it manually.

edit: here's a guy on codeproject who has done just that (written a manual parser/evaluator), could help you get started:

http://www.codeproject.com/KB/dotnet/Expr.aspx

Comment
Add comment · Show 1 · 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 CarlLawl · Jun 29, 2011 at 01:59 AM 0
Share

Im writting a dialog system which is all done in the inspector, if a user want to run a piece of code before/after the dialog is shown they can write it in a textarea in the inspector im just having trouble getting it from the to executing it!

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

7 People are following this question.

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

Related Questions

Insert string behind consecutive numbers in string? 0 Answers

turn string into function with arguments? 1 Answer

Extract number from string? 3 Answers

How do I use a Space between variables in a string? 1 Answer

How would I cast "someString" in the following code in order to get it to work? 1 Answer


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