Search Unity

ScriptedImporter - Replace Existing Unity Importer

Discussion in 'Editor Workflows' started by fremen42, Dec 7, 2017.

  1. fremen42

    fremen42

    Joined:
    Oct 19, 2015
    Posts:
    2
    Is it possible to disable, or override an existing Unity importer?

    I'm writing a DXF file importer, however this is already supported by Unity but doesn't seem to be implemented in any way as no geometry is constructed. The dxf file's I'm working with have Face3d objects within, and I know they have geometry (as I'm already loading them during runtime).

    The error message i currently get is:
    A scripted importer attempted to register file type dxf which is handled by a native Unity importer. Registration rejected.

    Ideally I'd like to replace the existing Unity dxf importer and use my own which would support more objects that DXF files contain (polylines, face3ds, ect)
     
    megame_dev likes this.
  2. j_ho_74

    j_ho_74

    Joined:
    Aug 13, 2014
    Posts:
    24
  3. fremen42

    fremen42

    Joined:
    Oct 19, 2015
    Posts:
    2
    Joerg, I've had most success with netDxf: https://github.com/haplokuon/netDxf

    Before the .NET46 support I used some old fork that supported double/decimal precision so I can keep accuracy with large coordinates (easting/northing). I needed that so the geometry/polyines would still be accurate to the centimeter when I bring them down to Unity coordinates (close to 0,0,0). At some point I'll be moving to the latest version on github.

    I still find the occasional file that can't be loaded though. There's no nice solution unless you want to shell out $$$ for Aspose.CAD/3D, then you run into licensing issues.

    Still hoping I can get some help on this, if any Unity developer happens to be here to answer some Q's:
    - Will ScriptedImporter allow me to replace an existing Unity Importer?
    - Why is there a dxf importer in Unity when its functionality is very limited?

    Cheers.
     
  4. markvi

    markvi

    Joined:
    Oct 31, 2016
    Posts:
    118
    I'm sorry you're having problems. The existing DXF import functionality is actually part of the FBX importer (the FBX SDK also supports DXF). Not being able to override a native importer is a known issue. We've also hit the same problem internally, but fixing it requires changes in the Asset Pipeline that we're not ready to make quite yet. Overriding native importers will *probably* land after 2018.2, but I can't promise that.

    In the meantime, the workaround is to use a different extension (e.g. ".dxf2") to bypass the native importer.

    Can you log a bug with a sample project containing the offending DXF file?
     
    CodeSmile likes this.
  5. kenamis

    kenamis

    Joined:
    Feb 5, 2015
    Posts:
    387
    Is there anything new on the ability to override a native importer?
     
  6. corhal

    corhal

    Joined:
    Mar 28, 2015
    Posts:
    1
    Hi, any news on that?

    We hit that problem too...
     
  7. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,278
    I am also interested in being able to override a native importer.
     
  8. JohnAustinPontoco

    JohnAustinPontoco

    Joined:
    Dec 23, 2013
    Posts:
    283
    What I really want is a ScriptableAssetPipeline. There are lots of things I'd like to control about the asset import process (doing more preprocessing work, using the AssetPipeline as an asset build system) but the current hooks are very fragile.
     
    megame_dev and CodeSmile like this.
  9. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    +1 on this
     
  10. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    212
    To me, the ability to run multiple importers for a single file type alongside each other would also be very useful. Unity then figures out how to merge their generated data.
     
  11. GamerXP

    GamerXP

    Joined:
    Mar 22, 2014
    Posts:
    78
    CodeSmile, cofdream and Peter77 like this.
  12. JohnAustinPontoco

    JohnAustinPontoco

    Joined:
    Dec 23, 2013
    Posts:
    283
  13. GamerXP

    GamerXP

    Joined:
    Mar 22, 2014
    Posts:
    78
    [ScriptedImporter(1, "psd", AutoSelect = false)]
    AutoSelect seems to disable automatic selection of that importer. My guess if it's true - it will be used by default? Like I said, I didn't try it yet though.
     
  14. JohnAustinPontoco

    JohnAustinPontoco

    Joined:
    Dec 23, 2013
    Posts:
    283
    Fairly sure I've tried every attribute value available in 2019.3. My understanding of that one is that it just means *this* importer won't be selected automatically. ie. If you had two custom importers for a custom file type, you could disable automatic selection on one.

    See the docs:
    upload_2020-9-2_10-46-27.png
     
  15. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I'm trying to override the PSDImporter with my own version to extend it, however, I can only make this work by calling
    AssetDatabaseExperimental.SetImporterOverride<PSDImporterOverride>(path)
    from a menu item. However, I'd like to do this before importing any PSD/PSB file, so I tried putting the call into OnPreprocessAsset, but it didn't work. Is there any way I can make this override automatic?
     
  16. JohnAustinPontoco

    JohnAustinPontoco

    Joined:
    Dec 23, 2013
    Posts:
    283
    Have you tried a callback that runs on InitializeOnLoad, and calls that function for every file in the Assets folder with a ".psd" extension? InitializeOnLoad should run before asset importing, right after it finished compiling assemblies.

    There might be an issue trying to use SetImporterOverride in the middle of an asset import phase.
     
  17. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Thanks for the idea, this will probably work. I'm just not sure that I want to pay the performance hit of checking every asset in the project since Unity already does this and should be able to provide me with a callback before an asset is imported the first time. I mean, the editor checks for changed assets and has to determine which importer to run anyway, so I should be able to hook right into this call to set my own, everything else seems like a big performance hit.

    Another workaround that comes to mind would be to simply copy the PSDImporter source as an embedded project, then remove the ScriptedImporter attribute from the base class and put it on my derived class. I've tried this and it works, but of course now I have to manually update and merge if a new version is released by Unity.
     
  18. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    268
    Any updates on our ability to override native Unity Importers? It's a shame because we would have had much better Blender integration by now if not for this issue.

    we got a "probably" for 2018.2. 4 years on and still no solution?
     
    megame_dev likes this.
  19. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    I've run into this thread while trying to overide default Blender importer with my own. But I also wanted to keep the ability to use default importer - just in case.

    So my, not so elegant, but efficient solution was to introduce .blend2 file extension.
    I've associated it with Blender and since Blender does not forcefully change file extension when saving, it works just fine.
    This allows me to get exactly what I want - work with both default and custom Blender importer, with decision made for each individual file just by adjusting extension.

    I hope this helps some of you.

    But that's Blender. Some other software may not be so cooperative when we want it to work with different file extensions than it's original ones.

    That being said - I'd love a callback method in Unity Editor that tells me it indends to use importer X for asset Y and lets me provide alternate importer if I want to