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
4
Question by numberkruncher · May 09, 2014 at 03:13 AM · editorassemblycallback

Callback before Unity reloads editor assemblies?

Is there a callback or event that I can subscribe to allowing me to detect when Unity plans to reload its editor assemblies?

For instance, before recompiling, before switching to/from play mode, etc:

 ??? += () {
     // i.e. Stash some static variables before they are lost!
 };

I am aware of EditorApplication.playmodeStateChanged, but I was hoping for a more reliable solution which will also work for the various other situations where reload occurs.

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

3 Replies

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

Answer by numberkruncher · May 09, 2014 at 08:41 PM

With thanks to IRC it seems that this problem can be solved quite easily using a ScriptableObject:

 using UnityEngine;
 using UnityEditor;

 [InitializeOnLoad]
 internal sealed class MyScriptableFriend : ScriptableObject {
     static MyScriptableFriend() {
         // Create instance as soon as Unity starts.
         s_Instance = CreateInstance<MyScriptableFriend>();

         // For some reason the following breaks this upon
         // exiting from Unity (Case 598897).
         //s_Instance.hideFlags = HideFlags.DontSave;
     }

     private static MyScriptableFriend s_Instance;

     private void OnDisable() {
         // This was what I needed!
     }
 }
Comment
Add comment · Show 3 · 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 vexe · May 27, 2014 at 11:54 PM 0
Share

Unity 4.5 is here and ISerializationCallbackReceiver has been introduced.

avatar image numberkruncher · Jun 01, 2014 at 02:52 PM 0
Share

@vexe ISerializationCallbackReceiver occurs on a separate "serialization" thread and so cannot interact with the Unity API: http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html

Unity's serializer usually runs on the non main thread, while most of the Unity API can only be called from the main thread.

avatar image vexe · Jun 04, 2014 at 01:50 PM 0
Share

Crap. Never payed attention to that. Thanks for the hint. What do they actually mean by Unity API? Like, basically anything Unity-related?

avatar image
8

Answer by vexe · May 09, 2014 at 03:45 AM

If you want an editor solution, have a look at the UnityEditor.Callbacks namespace. DidReloadScripts and PostProcessBuild/Scene look interesting. (*1)

EDIT: I think I found something more interesting. EditorUtility.AssemblyReloadEvents it's internal so you have to use reflection. (*2) Unfortunately there's no delegates, but methods. And analyzing them (finding out who uses/consumes them) didn't lead me anywhere. I'll see if I can inject something into them.

alt text

EDIT: I have indeed managed to inject custom code inside these assembly reload methods using the idea I mentioned here.

alt text

Here's an example of OnBeforeAssemblyReload:

1- Somewhere in your code, create a twin OnBeforeAssemblyReload with your injected code, and whatever code that is actually called (in this case, Security.ClearVerifiedAssemblies), so:

 public static void OnBeforeAssemblyReload()
 {
     typeof(Security).GetMethod("ClearVerifiedAssemblies", BindingFlags.NonPublic | BindingFlags.Static)
                     .Invoke(null, null);
     Debug.Log("Injected OnBeforeAssemblyReload!");
 }

2- Follow the injection steps in the previous link, and redirect the method call Security.ClearVerifiedAssemblies to your OnBeforeAssemblyReload so now it executes your injected code, and calls the same method it used to call (`ClearVerifiedAssemblies`)

3- Save the patched dll, and paste over your Unity/Editor/Managed folder.

4- Enjoy! :)

Of course, instead of the debug log, you could invoke a delegate from which you could subscribe to, use your imagination...

Edit: You might get some type/file not found errors. Just use peverify.exe on your patched assembly.

Now here's what you can do about OnAfterAssemblyReload:

0- Create an injection method:

 public static Action onAfterAssemblyReload;
 public static void OnAfterAssemblyReload()
 {
     Debug.Log("Injected OnAfterAssemblyReload");
     if (onAfterAssemblyReload != null)
         onAfterAssemblyReload();
 }

1- Navigate to the method using your reflector

2- Add a new call opcode to call your new method, right before the first call to GetAllProjectBrowsers.

3- Save, and you're done (don't forget to verify your assembly using peverify)

Obviously, we could have done the same with OnBeforeAssemblyReload, would have been cleaner, but just to show a variety of methods here... I'm still getting the hang of reflexil.

DISCLAIMER: Do this shit on you own responsibility, I'm not responsible for the laws/stuff/etc that might be broken due to this hack. I do this for pure educational reasons.


inject.png (133.3 kB)
asm.png (10.7 kB)
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 numberkruncher · May 09, 2014 at 08:38 PM 0
Share

Wow that is complicated! but fascinating since I haven't seen this kind of injection before. Didn't even know it was possible with .NET! Sadly this approach is not workable for me since this is for a commercial product and I wouldn't want to use this kind of hack. But thanks anyhow!

avatar image
7

Answer by bluksPL · Aug 22, 2017 at 07:54 AM

Since version 2017.1 we (finally) have:


AssemblyReloadEvents

class in UnityEditor

Events:

  • afterAssemblyReload

  • beforeAssemblyReload

Delegates:

  • AssemblyReloadCallback


Link: https://docs.unity3d.com/2017.1/Documentation/ScriptReference/AssemblyReloadEvents.html


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 Minchuilla · Apr 25, 2020 at 08:24 AM 0
Share

It's worth noting that beforeAssemblyReload is called before OnDisable and afterAssemblyReload is called after OnEnable for $$anonymous$$onoBehaviours

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityEventBase editor changed callback 0 Answers

Locking assembly reloading in editor 0 Answers

Editor class for Animation window 1 Answer

Can Editor scripts be in external DLL's or assemblies? 1 Answer

Event or Callback for when Files are moved. 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