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
0
Question by davidefeng · May 13, 2013 at 01:44 PM · editoreditorutility

use of EditorUtility.CompileCSharp

Hello,

I am working on an Editor script to

  1. create an new C# script with a new class name which is inherited from MonoBehavior, and save it on disk. The script should be copied from an existing script, MyScriptTemplate.cs, but the class name and script file name should be renamed as MyNewScript.cs

  2. create an empty gameobject, MyNewGameObject

  3. add (attach) the newly created c# script, MyNewScript.cs, to MyNewGameObject as a component

Here is the code:

 [MenuItem("GameObject/Create Other/MyGameObject")]
 static void CreateNewMyGameObject()
 {
     string source = "Assets/MyScripts/MyScriptTemplate.cs";
     string target = Application.dataPath + "/MyNewScript.cs";
     
     MonoScript s = AssetDatabase.LoadAssetAtPath(source, typeof(MonoScript)) as MonoScript;
     string scriptContent = Regex.Replace(s.text, "MyScriptTemplate", "MyNewScript");

     StreamWriter sw;
     using (sw = File.CreateText(target))
     {
         sw.Write(scriptContent);
     }

     AssetDatabase.ImportAsset(target);
     AssetDatabase.Refresh();

     //EditorUtility.CompileCSharp(string[] sources, string[] references, string[] defines, string outputFile);//how to specify the parameters?

     GameObject aNewGameObject = new GameObject("MyNewGameObject");
     
     //aNewGameObject.AddComponent<MyNewScript>();//this is not working because template parameters should be determined at compile time
 
     aNewGameObject.AddComponent("MyNewScript");

 }

With this sample code, both step 1 and step 2 are working correctly. However, MyNewScript cannot be attached to MyNewGameObject as a component, and there is a warning message: "Script MyNewScript has not finished compilation yet. Please wait until compilation of the script has finished and try again."

I am not sure if EditorUtility.CompileCSharp() is the correct method to solve this problem. If it is, how can I specify those parameters -- string[] sources, string[] references, string[] defines, and string outputFile -- of EditorUtility.CompileCSharp()?

Thanks very much for any help.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · May 13, 2013 at 01:56 PM

Everything I can find seems to point to that being a bad idea - so what I did was store the name of the script and wait for the editor script to be re-initialized after a successful compile and then add it.

The answer here: http://answers.unity3d.com/questions/377425/add-component-button-doesnt-work.html includes a download for my version of that.

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

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

15 People are following this question.

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

Related Questions

The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers

Can't open scripts with monodevelop? 1 Answer

AssetPreview.GetMiniTypeThumbnail not working for custom scripts 0 Answers

UniSciTE in version 3.x Script Edtior 1 Answer

Can I auto run a script when editor launches or a project finishes loading? 4 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