Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Brian 4 · May 21, 2010 at 09:48 PM · assetcustomimporter

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by GlitchEnzo2 · Dec 06, 2010 at 06:48 PM

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 &lt; movedAssets.Length; i++ )
         {
             Debug.Log("Moved: from " + movedFromPath[i] + " to " + movedAssets[i]);
         }
     }
 }

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image femi · Dec 29, 2010 at 07:12 PM 0
Share

Do you have an example of the code that actually imports something in OnPostprocessAllAssets()? I'm trying to create a GameObject and save it as asset with AssetDatabase.CreateAsset(go,path+".asset"); but the result seems broken somehow (unity crashes eventually). Also, the GameObject immediately appears in the current scene, which is not my intention...

avatar image femi · Dec 30, 2010 at 10:00 AM 0
Share

After some more research: is is possible to process custom files in OnPostprocessAllAssets() and create assets but impossible to create prefabs with hierarchy of game objects. That's because it's impossible to create a GameObject suitable for storing in a prefab (like OnPostprocess$$anonymous$$odel() receives) on your own.

avatar image
2

Answer by Lucas Meijer 1 · May 22, 2010 at 10:31 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brian 4 · May 23, 2010 at 07:55 PM 0
Share

Thanks for the suggestion. It gave me an error that the asset is not persistent.

OnPostprocess$$anonymous$$odel is almost working. When I get this event for the FBX import, I load the associated text file. I can then create additional GameObjects...

GameObject newgo = new GameObject( name ); newgo.transform.parent = go.transform;

I then create a mesh based on the text file...

$$anonymous$$esh mesh = new $$anonymous$$esh(); ...populate mesh... newgo.AddComponent< $$anonymous$$eshFilter >().shared$$anonymous$$esh = mesh;

At runtime, I see the new GameObject with a $$anonymous$$eshFilter, but the mesh is missing. Any ideas what is wrong?

avatar image
0

Answer by qJake · May 21, 2010 at 10:15 PM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brian 4 · May 22, 2010 at 02:54 AM 0
Share

Yeah, right now I'm basically doing that. But it means that all the parsing is happening at runtime, which is slow.

I would rather have the editor parse the file during import and create an object that I can then include with the build and instantiate as needed.

The documentation seems to elude to having classes that will do this, but it isn't very clear.

avatar image qJake · May 22, 2010 at 07:45 AM 0
Share

Parsing a file shouldn't take that long. Try ti$$anonymous$$g it to see how fast it actually goes. You could also just parse it in Start() or Awake(), which is where most of your "slow code" should go anyway. You can then store references to your created objects and just pass them around as needed (which is extremely fast and efficient).

avatar image p_025 · Apr 13, 2013 at 10:34 PM 0
Share

Would it be possible to run the script to parse the text file once, and save the resulting GameObjects while the editor is running the game? That way, once you stop the editor running the engine you still have your generated GameObjects that you can include with the build, and remove your TextAsset and generator script?

avatar image CrossV4 · Apr 17, 2021 at 12:12 PM 0
Share

hi from future :D, but i need some help. My friend has sent me spacecraft.bytes file. I asked him he tolds it's a 3D object. I have searched it how do i open or how do i convert this file to orginal format but i couldn't find anything. They all says use TextAsset. I looked up the documentation of TextAsset the unity only explained Texture2D. Could you please introduce me how do i convert or open it directly as expected 3d object in unity .?

avatar image
0

Answer by hunn3s · Jun 01, 2011 at 12:31 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MattRix · Mar 17, 2021 at 08:26 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CrossV4 · Apr 17, 2021 at 12:12 PM 0
Share

do you know how to convert .bytes files to original formatted file ?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Editor Window with settings and Asset Creation 1 Answer

Creating an asset from script? 1 Answer

Converting all MA files to FBX without changing GUIDs 2 Answers

Is it possible to reference a custom file type in the inspector? 1 Answer

Custom Inspector when Selecting from Project Tree? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges