I have a custom inspector script for a spline, it stores points in a list.
It fully works, but when unity recompiles any script in the project, all the data seems to get lost and the script doesn’t work anymore (the contents in the inspector are gone) untill I reset the script in unity.
What can cause this problem? And how do I solve it? Because its very useless now, with each compile of a script I have to reset the spline script and redo all the work.
Update:
When saved in a prefab the data is there, even after a recompile.
When I reload the scene or press play, the data is back visible in the inspector.
Is there maybe a way to catch a recompile event and refresh the inspector or something like that, or a manual deserialize?
You should stay away from saving permanent data into custom inspector classes. Save them in the inspected class instead. I’m not 100% sure on the life cycle of instances of Editor-derived scripts, but it is entirely possible that they all get deleted and recreated on recompile, together with anything that was statically initialized (all static classes’ constructors are re-called on recompile).
For your second question - I don’t know for a documented way of intercepting recompile events, but you could use the fact that static constructors are called on recompile. The catch is that most of the Unity functionality apparently doesn’t work when called from static class constructors. It probably has something to do with the order of initialization of various codes on program-load. You can circumvent that by just setting a flag in static constructor.
public static class RecompileListener
{
private static bool loaded;
// This should ban setting Loaded to false outside the class.
public static bool Loaded
{
get { return loaded; }
set { if (value != false) loaded = value; }
}
static RecompileListener()
{
// This is called on code recompile
loaded = false;
}
}
//some Unity class method
if (!RecompileListener.Loaded)
{
RefereshStuff;
RecompileListener.Loaded = true;
}