- Home /
interpret unityscript at runtime?
is it possible for unity to interpret a unityscript at runtime, i. e. could i enter unityscript commands in a gui and execute that at runtime?
Answer by Justin D'Arcangelo · Apr 11, 2010 at 07:23 AM
Actually, this can be done when using UnityScript since it is based off of the Microsoft JScript language in the .NET Framework. It is, however, NOT recommended for performance and security reasons. If you still want to do this for something like an in-game debugging console, you can use the eval() function. For further reference, please see the .NET documentation on JScript's implementation of the eval() function here: http://msdn.microsoft.com/en-us/library/b51a45x6(VS.80).aspx
you can indeed do this using eval(), however our Javascript implementation is not "based off" the microsoft jscript language.
Answer by Eric5h5 · Apr 11, 2010 at 07:44 AM
I'd rather add this as a comment to Justin's answer because it's correct, but I can't do code formatting that way, and I wanted to add a little example:
import UnityEngine.GUILayout; private var exeText = ""; private var run = false;
function OnGUI () { exeText = TextField(exeText, Width(600)); if (Button("Execute", Width(60))) { run = true; } if (run) { eval(exeText); } }
Put some GUI code in the text field, click execute, and see what happens, like this maybe:
for (i = 1; i <= 10; i++) Label(i.ToString());
Of course, it's trivially easy to crash Unity that way by entering junk instead of valid code.
@Eric5h5 Um... won't your OnGUI
never stop running the code?
Answer by Ashkan_gc · Apr 11, 2010 at 11:23 AM
actually there are 3 ways to do this. 1 using Eval as Erric5h5 said. (i did not test it) 2 mono has a class for compiling C# code at runtime. i think lucas meijer made a ingame console debugger with it. 3 when unity releases 3.0 and updates to newer version of mono (they will upgrade to 2.6 just like iphone but even 2.2 is enough) you can use DLR and ironpython or ironRuby or your own scripting language to run scripts at runtime.
it's the best way to make patches easily
Answer by Brian-Kehrer · Nov 18, 2009 at 05:32 AM
Nope. The 'script' in unity is actually compiled to an intermediate language when you build your app, and then compiled to machine code at runtime, using the JIT compiler.
EDIT: Oops. This is wrong. Sorry guys.
Additionally, this is part of the reason the language is so powerful and fast. It is not simply a scripting language, but built on $$anonymous$$ono 's open source implementation of C#. $$anonymous$$ost runtime scripting languages must run through some sort of interpreter or emulator, and end up being much slower.
downvote explanation: It is possible to do this, see other answers
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Interpreted Scripting 1 Answer
Text Mesh not updating during run time? 2 Answers
Using replacement shader at runtime? 1 Answer