Search Unity

ScriptedImporter with lengthy processing time

Discussion in 'Scripting' started by DryerLint, Aug 8, 2019.

  1. DryerLint

    DryerLint

    Joined:
    Feb 7, 2016
    Posts:
    68
    Hello,

    I want to write a ScriptedImporter to load a voxel format I created (it's an old format that I have lots of assets already made with). I would also like the ScriptedImporter to perform some calculations on the data that are fairly CPU-heavy (I want to raymarch the voxel objects to bake a custom ambient occlusion effect into them). However, I fear that these calculations won't be preserved, and that the calculations will happen every time Unity is loaded (The ScriptedImporter is called every time a file is loaded, as far as I know). Is there any way I can ensure this data be stored in some way, perhaps by serialization in the generated .meta file that accompanies my custom file?

    Thanks!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I don't have any experience with the new ScriptedImporters, but after a cursory glance I don't see what could stop you from storing associated data in your project and saving/loading that.

    Something like this:
    Code (csharp):
    1.  
    2. string path = Path.Combine(Application.dataPath, "Resources", "VoxelData");
    3. string file = path + "unique identifer for your asset goes here";
    4.  
    5. if(File.Exists(file) == false)
    6. {
    7.    // data doesn't exist yet
    8.    string data = GenerateVoxelData(object);
    9.  
    10.    File.WriteAllText(file, data);
    11.    AssetDatabase.SaveAssets();
    12.    AssetDatabase.Refresh();
    13. }
    14. else
    15. {
    16.    // data does already exist
    17.    string data = File.ReadAllText(file);
    18.    
    19.    // do whatever
    20. }
    21.