简体   繁体   中英

Eval() replacement in either UnityScript or C# with Unity

As far as I can see my question hasn't been answered before. I am using Unity to create an app for Windows Phone 8 , and I want to get the user's input and execute it as code. Now usually with JavaScript one could just use eval("code here") but Unity doesn't like that, and says that eval() is not allowed with my target (WP8).

So my question is - how could I execute a string as code in EITHER C# or UnityScript without using eval(), and without relying on window or attaching a script to the document - basically so it works with Unity.

For example, if I could use eval I would do : eval("createCuboid(0, 1, 2)");

Thanks for the help.

Edit: So I am making an app that allows the user to input one line of code in GUI.TextField. When the user click a button, it will then get their code and run it. In my example createCuboid(0, 1, 2) I have my own function that creates a cuboid to the user's specification (as seen in parameters).

The reason I want this is because I want the user to be able to freely manipulate the 3D environment without having to do something a bit more constricted - for instance, having to tap a button with createCuboid on it, then enter parameters in multiple fields. I have multiple functions the user can call to manipulate the environment.

Does that help?

Why you need to let the user write the code? That is very dangerous. If you want to allow to user to create objects (like cube) in run-time, let the user choose what they want to create, use fields to get function params (check if correct here too.) and create the object.

Maybe something like:

List<Cubes> cubes = new List<Cubes>();
int create = /* read */;
switch (create)
{
    case 0: {
        int x = /* ask for x */
        int y = /* ask for y */

        cubes.Add(createCuboid(x, y, 0));
        break;
    }

    /* etc */
}

If you want to let the user delete the cube, get from the list and delete it. And.. yes, this code is just to clarify what i want to say.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM