How can I create a custom import object?

I would like to create my own file format, which is text based, which is imported by the editor and then made available to the runtime player as an prefab object.

I other words, I would like to mimic what happens with fbx. The editor reads it, shows any editable properties in the project window, it becomes part of the build, and then the player can instantiate a copy of it at runtime.

Is it reasonable to think I can do this with the current C# API?

I know I can load a txt extension as a TextAsset, and then process it at runtime. But I'm burning cycles at runtime and I would rather the importer did most of the processing.

I know this is kind of old, but it really bugs me that the only answers here are to use TextAssets which you then parse at runtime.

The correct answer is to create an AssetPostprocessor and override the OnPostprocessAllAssets method. Like so:

using UnityEngine;
using UnityEditor;
public class CustomPostprocessor : AssetPostprocessor
{
    /// <summary>
    /// Handles when ANY asset is imported, deleted, or moved.  Each parameter is the full path of the asset, including filename and extension.
    /// </summary>
    /// <param name="importedAssets">The array of assets that were imported.</param>
    /// <param name="deletedAssets">The array of assets that were deleted.</param>
    /// <param name="movedAssets">The array of assets that were moved.  These are the new file paths.</param>
    /// <param name="movedFromPath">The array of assets that were moved.  These are the old file paths.</param>
    private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
    {
        foreach(string asset in importedAssets)
        {
            Debug.Log("Imported: " + asset);
        }

        foreach (string asset in deletedAssets)
        {
            Debug.Log("Deleted: " + asset);
        }

        for (int i = 0; i < movedAssets.Length; i++ )
        {
            Debug.Log("Moved: from " + movedFromPath _+ " to " + movedAssets*);*_

}
}
}

Then you can use the standard Mono libraries to parse the files and do whatever you want in Unity.

I have never tried this, but you could give this a shot: Make your file be imported as a textfile. Write an AssetPostProcessor, and in that do the parsing of the text, create a bunch of new objects (texture2d, material, custom scriptableobjects, AnimationClips, whatever), and use AssetDatabase.AddObjectToAsset() to add it to the just imported file.

Again, I'm not 100% sure this will work, but it's a good first shot. Please be aware that Unity2.6.1 has a bug that makes a textasset being read as binary be garbled, so use an actual textformat, and not a binary one. I fixed this for Unity3.

This question is very old but still comes up in web search results, so I’ll give a more modern answer.

You can now use ScriptedImporter to import custom file types with whatever extension you want.

You can't create your own file formats that Unity imports, as far as I know. Sure, you can stick whatever files you want inside your Assets folder, and you could MAYBE write an editor script to have a custom Inspector for those files, but that's unreasonable. Here's what you can do that should be relatively easy:

  • Create an empty game object.
  • Create a script.
  • Create a TextAsset object (something like: `public TextAsset myTextFile;`)
  • Apply the script to the empty game object.
  • Drag the text file from your Unity Assets into your script, so the script has a reference to it.
  • Create a prefab in your assets, and drag the game object with the script on it, into the prefab slot.

Now, in that script, you'll have your text file with all its data in it. You can read how to get the data of the text file by checking out the TextAsset documentation.

Hi!
I would like to import my own formats, too. These are to then emerge also in the Project folder and also the characteristics them have to point out. For example Xpresso tags or simply only dependence of each other. I have already tried with the function AssetDatabase.AddObjectToAsset () to get ahead. however unsuccessfully. Can someone help me?
Thanks a lot.