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
0
Question by Untrade · Jul 06, 2016 at 05:43 PM · c#editoreditor-scriptingnative pluginosx

Using native plugin with callbacks in editor (OS X)

I have a native plugin that has callbacks back to the C# code. This is how the callbacks are setup:

C# Side (Unity):

 public delegate void Callback(int value);
 
 [DllImport("NativePlugin")]
 public static extern void setup_callback(Callback callback);
 
 [MonoPInvokeCallback(typeof(Callback))]
 private static void OnCallback(int value)
 {
     Debug.Log("Callback called with value: " + value);
 }
 
 void Start()
 {
     setup_callback(OnCallback);
 }

The Objective-C side (with C wrapper so you'd be able to call it from C#):

 typedef void(*Callback)(int);
 
 Callback _callback = NULL;
 
 void setup_callback(Callback callback)
 {
     _callback = callback;
 }
 
 void on_something_happened(int value)
 {
     if(_callback)
         _callback(value);
 }

Now, most of the time the above code works ok, and I receive the correct values in Unity. But sometimes when I make changes to any one of my C# scripts, Unity crashes with this:

 Exception Type: EXC_BAD_ACCESS (SIGABRT)
 Exception Codes: KERN_INVALID_ADDRESS at 0x00000000ffffffa9
 Exception Note: EXC_CORPSE_NOTIFY

Looking at Unity's Editor logs I found out that when I make changes to a C# script it recompiles Assembly-CSharp.dll and reloads it togheter with some other C# assemblies. So that made me think that this might cause the pointer saved in the native plugin to now point to an invalid address (because Assembly-CSharp.dll is now probably in a different address space). Note that it does not happen every time the dll gets reloaded, is it possible that it gets reloaded to the exact same address space sometimes?

Can I get an event from Unity when the editor reloads the DLL and then re-register the callbacks (assuming that this is the real problem)? Is this even the correct way to setup a callback from native code?

Comment
Add comment · Show 3
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 ETLang · Mar 01, 2017 at 06:06 PM 0
Share

Hey @Untrade, did you ever resolve this? I'm dealing with the same issue. AppDomain.DomainUnload is not getting called, and object destructors are not called, so I don't know how to tell when Unity is recompiling the script and my callback is no longer valid...

avatar image Untrade · Mar 02, 2017 at 03:26 PM 0
Share

@ETLang I've added an answer

avatar image ETLang · Mar 04, 2017 at 02:46 AM 0
Share

I don't see it. I suppose it's pending approval?

2 Replies

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

Answer by Untrade · Mar 04, 2017 at 03:12 AM

I ended up using LockReloadAssemblies https://docs.unity3d.com/ScriptReference/EditorApplication.LockReloadAssemblies.html

It let's you prevent Unity from reloading until you call UnlockReloadAssemblies. I just created a new script:

     void Awake()
     {
         //Prevent editor from reloading dlls files during the gameplay
         #if UNITY_EDITOR
         UnityEditor.EditorApplication.LockReloadAssemblies();
         #endif
     }

     void OnApplicationQuit()
     {
         //Prevent editor from reloading dlls files during the gameplay
          #if UNITY_EDITOR
         UnityEditor.EditorApplication.UnlockReloadAssemblies();
         #endif
     }

And put it on some object in the scene.

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

Answer by ETLang · Mar 05, 2017 at 03:39 PM

For anyone else with a similar issue, another solution that might work for you is documented here:

http://answers.unity3d.com/questions/704066/callback-before-unity-reloads-editor-assemblies.html

In the code below, Unity will call OnDisable immediately before unloading the assembly and performing a recompile.

  [InitializeOnLoad]
  internal sealed class MyScriptableFriend : ScriptableObject {
      static MyScriptableFriend() { s_Instance = CreateInstance<MyScriptableFriend>(); }
      private static MyScriptableFriend s_Instance;

      private void OnDisable() {
          // Incoming recompile... save yourself!
      }
  }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to access one class instance in editor script? 1 Answer

Unity API for checking if an object was modified 3 Answers

How to : Editor.OnPreviewGUI Implementation 0 Answers

Custom Editor script to show non-native class in inspector 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