|
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.
(comments are locked)
|
|
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:
Then you can use the standard Mono libraries to parse the files and do whatever you want in Unity. 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
Dec 29 '10 at 07:12 PM
femi
After some more research: is is possible to process custom files in
Dec 30 '10 at 10:00 AM
femi
(comments are locked)
|
|
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. Thanks for the suggestion. It gave me an error that the asset is not persistent. OnPostprocessModel 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... Mesh mesh = new Mesh(); ...populate mesh... newgo.AddComponent< MeshFilter >().sharedMesh = mesh; At runtime, I see the new GameObject with a MeshFilter, but the mesh is missing. Any ideas what is wrong?
May 23 '10 at 07:55 PM
Brian 4
(comments are locked)
|
|
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.
(comments are locked)
|
|
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:
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. 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.
May 22 '10 at 02:54 AM
Brian 4
Parsing a file shouldn't take that long. Try timing 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).
May 22 '10 at 07:45 AM
qJake
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?
Apr 13 at 10:34 PM
p_025
(comments are locked)
|
