Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
4
Question by Adam-Mechtley · Apr 03, 2010 at 08:08 PM · editorcompiling

How can I wait for Unity to recompile during the execution of an editor script?

I have an editor script that creates other scripts when it is run. Immediately after creating these new scripts, the editor script needs to access them (e.g., by adding their components to objects, setting references, etc.). Thus, I need a way to make my editor script wait for Unity to recompile before it tries to access these newly-created scripts.

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

6 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Adam-Mechtley · Apr 14, 2010 at 07:06 PM

I think I found a workaround solution for this problem:

  1. Immediately after I create my script using StreamWriter, I call AssetDatabase.ImportAsset using ImportAssetOptions.ForceSynchronousImport.
  2. I then try to add the component that my procedurally-generated script has created.
  3. If the result of the AddComponent call is null, then I call AssetDatabase.ImportAsset on the asset I am postprocessing. The second time it imports, the class exists.
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 Adam-Mechtley · Apr 16, 2010 at 02:09 PM 0
Share

To clarify, the process basically works like this:

  1. If the script already exists and its contents are identical, then just proceed to link references.

  2. If the script already exists and its contents are different, but its fields are identical to the inco$$anonymous$$g replacement script, then call ImportAsset forcing synchronous and then proceed to link references.

  3. If the script does not already exist, or if it exists but the fields have changed, then call ImportAsset forcing synchronous and immediately reimport the current asset. The next time it runs, it will do path 1.

avatar image Adam-Mechtley · Apr 16, 2010 at 02:09 PM 0
Share

If I use reflection on the class after the initial import of the new script, the assembly claims it exists though I cannot add the component, so the second importation may be giving the assemblies time to rebuild?

avatar image Aubrey Hesselgren · Jan 18, 2011 at 04:52 PM 0
Share

Interesting. I wish I could get this to work. Don't suppose you've any code examples you could show?

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

I'm also very confused about this. Can you give me any example code about that?

avatar image
1

Answer by qJake · Apr 03, 2010 at 08:29 PM

I don't think that's possible. There is no "OnCompiled" event or anything of that nature.

I think your best bet would be to tell your script to wait for a few seconds (enough time to where you would be sure the scripts have compiled) and then try to access them. You can use a coroutine (and yield return WaitForSeconds(x)), or you can start a new thread and use Thread.Sleep(<milliseconds>).

Comment
Add comment · Show 1 · 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 Bunny83 · Oct 27, 2011 at 08:30 AM 0
Share

Unfortunately you can't use Untiy's coroutines in the editor, but you can use threads or implement your own coroutine handler.

http://www.unifycommunity.com/wiki/index.php?title=CoroutineScheduler

avatar image
1

Answer by Lucas Meijer 1 · Apr 03, 2010 at 09:13 PM

I think you can achieve this by leveraging the fact that when Unity reloads scripts, it completely serializes all editor windows (including yours), shuts down mono, starts mono, and deserializes all editor windows.

If you can detect that this process has happened, you're probably good to go.

I think you can recognize this by stuffing a value into a field that is not serialized, and then keep monitoring that value in OnGUI() (or something else that happens frequently). As soon as your variable is gone, that means your scripts have reloaded.

make sure not to do the check in a tight loop, but instead let unity "breathe", and check from a OnGUI() method or similar.

Comment
Add comment · Show 2 · 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 Adam-Mechtley · Apr 06, 2010 at 09:02 PM 0
Share

Hi Lucas

$$anonymous$$y editor script actually extends AssetPostProcessor, not ScriptableObject, so I don't have an OnGUI() function. I am guessing there is therefore also no way for me to start a coroutine to do a similar thing?

avatar image BarryNorthern · Mar 07, 2012 at 02:48 PM 0
Share

I did exactly as Lucas described to monitor the re-creation of the editor window and it worked perfectly. Thank you.

avatar image
1

Answer by Molix · Apr 12, 2010 at 04:09 PM

Maybe you can yield on EditorApplication.isCompiling?

Comment
Add comment · Show 1 · 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 Adam-Mechtley · Apr 14, 2010 at 04:10 PM 0
Share

That sounds like it would work, but unfortunately I cannot yield inside an AssetPostprocessor script

avatar image
1

Answer by Tommynator · Oct 27, 2011 at 07:47 AM

Another solution to have a sort of OnPostBuildEvent in Unity is hidden in the AssetPostProcessor class. Save this little snippet in your editor folder and you will get an automatic call whenever compilation finished.

class MyAllPostprocessor : AssetPostprocessor
{
    static void OnPostprocessAllAssets(
    string[] importedAssets,
    string[] deletedAssets,
    string[] movedAssets,
    string[] movedFromAssetPaths)
    {
        Debug.Log("Hello OnPostBuildEvent");
    }
}
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
  • 1
  • 2
  • ›

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Who owns NetworkViews placed in the editor? 1 Answer

How can I preserve static object/data between editor and play? 2 Answers

Is the animator graph editor exposed to the API? Unity 4.0 0 Answers

UniSciTE in version 3.x Script Edtior 1 Answer


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