Hi there,
I’d like to use jobs in the editor (i.e. at edit time, will also be used at runtime), and it seems like it should be totally supported as mentioned here . I’m creating and disposing of a NativeArray
(for use in said jobs) like so:
[PreferBinarySerialization]
public class MyTree : ScriptableObject
{
public uint[] Data = null;
private NativeArray<uint> _jobData;
private void OnEnable()
{
if (Data != null)
_jobData = new NativeArray<uint>(Data, Allocator.Persistent);
}
private void OnDisable()
{
if (_jobData.IsCreated)
_jobData.Dispose();
}
}
This data is intented to be immutable (will not change once imported unless re-imported).
MyTree
is created (and its data array filled out) via custom ScriptedImporter
. On the first import, and in regular use, OnEnable/OnDisable
are called exactly as expected. However, there are a few cases where OnDisable
is never called (and we therefore have memory leaks):
- Upon Deleting the Asset
- Upon Applying new Import Settings
- Upon Right-Click>Reimport
- Upon Changing
ScriptedImporter
version (untested)
In the first two cases, I can catch them before they happen and dispose manually (i.e. OnWillDeleteAssets
and overriding Apply
in the ScriptedImporterEditor
). However, I’m stumped at what to do for Reimports. I’ve tried hooking into OnPreProcessAsset
but its too late - the old scriptable is already destroyed at this point (AssetDatabase.LoadMainAssetAtPath(assetPath)
returns null
).
Does anybody have any ideas how I can get a callback or hook into right before a reimport happens on an asset so I can dispose of the NativeArray
data properly? Or perhaps I need a different way of thinking about how to allocate my NativeArray
for use in jobs in both editor and runtime?
Thanks,
Ant