Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

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)
     
  2. j_ho_74

    j_ho_74

    Joined:
    Aug 13, 2014
    Posts:
    19
  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

    Unity Technologies

    Joined:
    Oct 31, 2016
    Posts:
    86
    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?
     
  5. kenamis

    kenamis

    Joined:
    Feb 5, 2015
    Posts:
    360
    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,123
    I am also interested in being able to override a native importer.
     
  8. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    135
    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.
     
  9. mikest

    mikest

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

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    199
    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:
    56
    Peter77 likes this.
  12. Kleptine

    Kleptine

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

    GamerXP

    Joined:
    Mar 22, 2014
    Posts:
    56
    [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. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    135
    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:
    992
    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. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    135
    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:
    992
    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.
     
unityunity