Open zhmt opened 4 years ago
How about initializing dll when game started, and reset befor DllManipulatorScript's destructor is called?
If My NativeApi object hold an refercence of DllManipulatorScript , it will stop DllManipulatorScript being GCed before all my GameObjects are destoryed.
Something like this:
class DllManipulatorScript {
void Awake() { Reinitialize(); }
~DllManipulatorScript () { Reset(); }
}
class MyNative {
object dllScriptRef;
Awake() { dllScriptRef = GameObject.Find("DllManipulatorScript"); }
[DllImport]
static extern void test();
}
The problem is, at least when I last tested it, destructor is not called in editor when you just stop the game (or maybe only when it's the selected object in the scene, something of that nature). But if it turns out I'm wrong about that and it's gonna work fine, then yeah, I can change that.
I tested, you are right. It is pretty tough to clean up at right time.
Luckly, mannally dll unloading works fine.
The order of OnDestroy is not guaranteed, because the order of script excution doesn't work when calling OnDestroy.
The code above will raise IndexOutofBoundException randomly when stopping game in editor.
I dont know how to solve this problem.
I am using a stupid solution:
Is there better way?