Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Lohaz · May 28, 2015 at 02:32 PM · entry

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

Comment
Add comment · Show 2
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 virgiliu · May 28, 2015 at 02:38 PM 0
Share

Posting more of the code and the exact compiler error might help shed some light on this

avatar image virgiliu · May 28, 2015 at 03:22 PM 0
Share

Try object.AddComponent("testjs");

This might offer some help http://forum.unity3d.com/threads/attaching-script-objects-to-other-objects-by-script.15679/

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

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 virgiliu · May 28, 2015 at 02:51 PM 0
Share

Couldn't eval() be used to run user provided scripts, even though it's not a really nice workaround?

avatar image Lohaz · May 28, 2015 at 03:00 PM 0
Share

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.

avatar image Baste · May 28, 2015 at 03:16 PM 1
Share

See updated answer. Thanks, @vpld!

avatar image Lohaz · May 28, 2015 at 03:21 PM 0
Share

Ok, I will try! Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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