- Home /
User entry for a class file
Hello there.
I am a Taiwanese and because my poor English that may hard to understand my question so I'm sorry about that first. I want to build a game for teaching JavaScript, because I am a student that's something like my assignment. This game provide a blank block that user can type their code in it. And the game objects will act by that.
I want to know if it is possible that get the entry of user, and execute it like a js file. e.g. a whole class file type from user.
I have found some question for that as follow: http://answers.unity3d.com/questions/14367/how-can-i-wait-for-unity-to-recompile-during-the-e.html
And I wonder if that is actually what I want.
Thanks you guys for reading my question. I'm waiting for your answers.
update 5/28/2015
if (GUI.Button(Rect(600,10,50,50),btnTexture))
{
if (!System.IO.File.Exists ("../tryFile.js"))
{
stringToEdit = "You don't have such file like that";
}
else
{
AssetDatabase.ImportAsset("../tryFile.js",ImportAssetOptions.ForceSynchronousImport);
}
try
{
object.AddComponent.<tryFile>();
}
catch(err)
{
stringToEdit = err.Message;
}
}
I make a textArea and after I finished my class code in it, I click an button and then it will write a js file by StreamWriter.
And then I click this texture button. It checks if the file is exist. Then use AssetDatabase.ImportAsset to load theFile.
Then I try to add that js file as a component of a gameObject which name is "object".
And it cause a compile time error (of course).
I just want to know is there has any way to solve it T_T.
----------update 5/28/2015
It's the compile error message:
Assets/TextBoxCreator.js(37,38): BCE0018: The name 'testjs' does not denote a valid type ('not found').
And it is my whole code:
#pragma strict
var stringToEdit : String = "Hello World";
var btnTexture : Texture;
var object : GameObject;
function Awake()
{
object = GameObject.Find("testObject");
}
function OnGUI ()
{
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextArea (Rect (10, 10, 500, 500), stringToEdit, 1000);
if (!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(Rect(600,10,50,50),btnTexture))
{
if (!System.IO.File.Exists ("../Assets/tring/testjs.js"))
{
stringToEdit = "You don't have such file like that";
}
else
{
AssetDatabase.ImportAsset("../Assets/tring/testjs.js",ImportAssetOptions.ForceSynchronousImport);
stringToEdit = "import sucess?\n";
}
try
{
object.AddComponent.<testjs>();
}
catch(err)
{
//AssetDatabase.ImportAsset
stringToEdit = err.Message;
}
}
if (GUI.Button(Rect(600,70,50,30),"Click"))
{
//create Folder
if (!System.IO.Directory.Exists ("../Assets/tring"))
{
System.IO.Directory.CreateDirectory ("../Assets/tring");
}
System.IO.File.WriteAllText("../Assets/tring/testjs.js",stringToEdit);
stringToEdit = "export sucess?\n";
}
}
Hope there has anyway to do itT_T
Posting more of the code and the exact compiler error might help shed some light on this
Try object.AddComponent("testjs");
This might offer some help http://forum.unity3d.com/threads/attaching-script-objects-to-other-objects-by-script.15679/
Answer by Baste · May 28, 2015 at 02:49 PM
Okay, it seems like I wasn't quite sure what I was talking about.
There is a built-in eval() function, and you might be able to do what you want with it. Check out this answer.
Also note that this will only work in webplayer, and webplayer is about to die a slow death, as browsers start dropping plugin support.
Couldn't eval() be used to run user provided scripts, even though it's not a really nice workaround?
Thanks for your response. It seems like it is not that easy and I has lots of misunderstandings. $$anonymous$$aybe the link I had mentioned in my question give me too many hope to try.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
disable or remove enum entry from inspector 1 Answer
bug producion beta? 0 Answers
high score name entry iphone 1 Answer
entry point of the application 1 Answer
Storing Positions of gameobjects 2 Answers