Custom model importer for Unity3d. How?

How can I create custom model importer for Unity3d? e.g. such as fbx importer, but for dwg models?

dwg’s are only lines, right? So what do you want to import exactly?

Why is converting them to some other format not an option?

As far as my investigation shows, you can’t. UT saw fit to make the importer a closed loop. You can sort of wrangle a post-processor for an established format, which does a piss poor job of doing what you need, but as far as writing your own custom AssetImporter there is a dearth of documentation and zero examples, either open or closed source, to draw upon. Until UT corrects this issue asset importing of anything other than what they officially support is a ghettoized subject that cannot be discussed.

Because one of the main promises of Unity, if you haven’t been paying attention, is that you don’t need to do any of that. You don’t need to convert your PSD to a PNG or your MB to an OBJ or your MP3 to a WAV. It “just works.” Or at least, it “just works” so long as you use one of the supported formats that Unity lets you use. Unity3D is an incredibly immature professional production platform at this point.

You can’t add support for a format not already supported through the importer classes.
But there is nothing that prevents you from writting a scriptable wizard for example which you point to a file and then manually process the file generating mesh etc from it, thats completely no problem.

And I wouldn’t consider it increadibly immature. it supports many more formats than any other technology out there actually and unlike most other professional engines its not a on some ego trip that forces you to own 3DS Max or Maya to even generate usable meshes like Cry etc, it supports the two main formats for models in the field for example with FBX and Collada and any reasonable texture format (psd, png, tga, bmp and dds)

Keep drinking that KoolAid.

Let me add that Unity’s support for FBX and other model types only exists in teh editor. This also is true for many image types. Unity does not support any 3rd party importer for any 3d object at runtime and only supports PNG and JPG at runtime.

There is NOTHING that stops you from writing an importer for any format, this includes lines. It can be run at build time and run time. At build time, you cannot expect the engine to automatically know what it is though, so you will have to write an importer to handle it, and force it to see it and import it.

The same is true for images. If you write an importer for something like TGA or BMP formats, It can read them wonderfully through code and you can make whatever you need in run time and build time.

As far as the immaturity. I would disagree… they saw fit to let YOU create, and not force you down their path. I dig that. If you find that Unity’s tools are lacking… Create your own… lol

I do not know if importing a model at build time puts that mesh or image into the engine registry. You may end up building a model in the hierarchy and then be forced to build a prefab and attach it to it. I have not tested these things yet.

As far as i know big mister b is correct. You can add support for new fileformats in the editor which writing a importer makes the data for the runtime product. As in if you import a fbx, it makes mesh prefab with all the parts inside of it. The parts inside is what is in the runtime and not the actual fbx itself.

You cannot however use the importer you wrote to import at run time.

I’d agree with calling unity a little immature relating to it being a profesional production platform, The biggest problems with unity is blocked off access from script like components which do not have a script side or have all their inspectable variables. Once you get past that it really comes to the constraints of designing the work flow and poor native support. Formats is actually one thing i would say unity does well, but on the other hand importing is one of unitys worst attributes.

Please show me where and how to hook in to their asset importing system. I would like to see demonstrable working code that will import a foreign file format not currently supported by Unity and working just like Unity’s importers within the editing environment, i.e. it is hands off as far as the user is concerned, they do not need to click anything special or run a ScriptableWizard, For example, when I drop an Excel spreadsheet in to my Assets folder, the file is baked in to a suitable format for Unity to use at run-time and is stored in the Library directory or I drag across a text file with an odd extension, e.g. RIF, and Unity will fire off the appropriate custom asset importer and ensure the data makes it in to the IDE in the correct internal format. No hand waving about a custom pre-processor outside of the Unity environment that has to run as a standalone application invoked directly by the user is permitted.

Well… If you wanted to know…

Using the basics of that link. I came up with this basic Importer:

class MyAllPostprocessor extends AssetPostprocessor {
	
	static function OnPostprocessAllAssets (
			importedAssets : String[],
			deletedAssets : String[],
			movedAssets : String[],
			movedFromAssetPaths : String[]) {
		for (var str in importedAssets){
			//Debug.Log("Reimported Asset: " + str);
			var ext = (str.Substring(str.LastIndexOf(".") + 1));
			if(ext.ToLower()=="csv"){
				ConvertCSV(str);
			}
		}
	}
	
	static function ConvertCSV(filename : String){
		Debug.Log("Importing CSV File");
	}
}

I dont know how long you were searching for this, but it too is there. :wink:

That’s 101 asset importer overrides that can be found on the documentation or the wiki.

Take that CSV and convert it in to a format you can use in your engine and store the baked asset in your library.

I’ve already been down this road. The hooks, documented or undocumented, don’t exist in Unity at this time to wire up a truly custom asset importer. I ran the UnityEngine and UnityEditor DLLs through RedGate, and studied that code. When that didn’t show anything that was usable, I disassembled the Unity.exe and started studying the assembler compiled from the C++ using IDA and the various bits of .NET that were also encapsulated. I wasn’t able to locate any hooks that would permit a completely custom AssetImporter. I may well have overlooked something utterly obvious, and I suspect that I have, and someone, somewhere will pop up with a stupid simple solution that is three lines long, but I spent two solid weeks trying to figure out how to make the AssetImporter class work just like Unity’s asset importing does.

There is no way, other than through AssetPostprocessor to hook in to the asset import system and say “when you find a file of this type, run this AssetImporter class.” All of the examples or usage by various UnityAnswers, Unity Forum, and blog posts, do exactly the code that you posted, and the code that you posted doesn’t hook in to the asset importer system.

Please understand, I am not trying to be dismissive of what you are saying, I am trying to make a point about the maturity of a product.

lol, what are you talking about… use what I posted and link it to something like this:

http://unity3d.com/support/documentation/ScriptReference/EditorUtility.CreateEmptyPrefab.html

you get an instance of a file, create a mesh or whatever in game. link it up to a prefab and viola, it is a converted file. And do it all on the fly, no user involvement at all.

I gave you a CSV because I didn’t feel like going through hours of decoding a excel file. Basically you could write any type of binary or text file importer that would do the trick.

The asset importer system does exactly what you are proposing, looks for a file, then lets you convert it into a file it can use. Throw a 3ds file into unity, drag the file into the scene, and change something and what does it ask you??? Do you want to break the prefab?

What is the difference between that and a custom importer that makes a prefab.

To say “There is no way, other than through AssetPostprocessor…”, doesn’t that suggest to you, that is what the AssetPostprocessor is for? Your post processing a file that has come into the asset database and making decisions on what you want to convert.

Are you asking to modify the source and not have the script? If so, no, Unity doesn’t work like that. Ogre, Torque and almost any other engine will do that. I did my time with those systems, not going back.

Again, I suggest the maturity of the product is there, your just focusing on one aspect and not seeing the whole picture.

I think you are missing the point, but that’s okay, you believe you have solved the problem with a trivial example and I will believe that it cannot be done within the proper context (something I always hate to say) until UT exposes a hook.

Hook

#3: anything that catches; snare; trap.

OnPostprocessAllAssets

Description:
This is called after importing of any number of assets is complete (when the Assets progress bar has reached the end).

" called after importing" == “anything that catches”

What part of this do you not see?

Unity imports a CSV file. You catch the CSV file after the asset is imported into Unity. It then depends on what you want to do with it.

For sake of argument, lets say that the CSV file should represent a hallway coordinate system that is built on the fly by Unity. You create a object in Unity, create it’s mesh, do everything that you are supposed to do with it, then make it into a prefab, with meshes, textures and everything in place.

Really, not a hook?

Unity imports a text file and thats exactly where the problem comes in: Cause this processor will fire for EVERY text file in your project.

You can naturally make it work by using proper formats or naming schemes, thats the way you want to go.
But optimally unity would offer a RegisterExtension(“xxx”,HookToCall) function to hook up custom processors for unknown formats

heh, you would still require a script to parse it… still require a script to register it… It’s the exact same thing. The system is not designed to work as you wish it. This does not mean that it is immature.

It’s up to you… If you are not happy with the system as it sits, you do not have to use it. I am sorry if its just not good enough for you.

Hi all
Anything new on that front ?
I aim to import a binary custom 3D format to be read using a C++ API.