- Home /
Creating an asset from script?
Hi, I'm new to Unity scripting ( using C# ) and have a quick question:
I'd like to create some custom data that gets saved out as a resource or asset, that I can then load back in later on. The asset creation code will only be run once on my end to generate the custom chunk of data and save it to a file. Then, the actual game code run by the user will read in that data and use it.
What's the best approach for doing this? Thanks in advance!
In the editor, or in the game? One of these things is quite easy, the other... not so much.
Basically I have some mesh data along with some other metadata that gets generated at runtime. The generation process is slow, so I'd like to run the game once on my end to generate that data and save it off as an asset or resource or something. Then, I can disable the generation code and simply read it in from that file from then on out to make it super speedy.
Answer by kalvinlyle · Mar 11, 2012 at 04:50 PM
If you want to write your data to a file you can do that as answered in this question: http://answers.unity3d.com/questions/23578/how-do-i-write-a-line-of-text-to-a-file.html
To properly answer your question some more detail would be nice. Perhaps the solution you are looking for is not ideal and someone could suggest a better path for you with more details.
Thank you! I had seen this post ( should have mentioned that, sorry ), but wasn't sure if it was the best method for what I am doing. Plus I'm not sure how to then include that file into the game project as an asset ( is it as simple as putting the file into the resources directory? ) and read it back in again.
Also, I wrote a more detailed description of what I'd like to save to file and why in a comment above on my original question if that helps at all.
Ok, I am able to save/load my custom binary data using BinaryWriter / BinaryReader and a FileStream! Yay!
I'm creating the file inside the Assets folder which makes it show up in my project hierarchy now... is there anything special I need to do to ensure that the file gets included in my build? Does the entire Assets directory get included into the build? If so, then everything should work in the final product, right? If not, how do I tell Unity to make sure to include this file into the final build?
The file creation and file loading code looks like this:
FileStream stream = new FileStream("Assets/Test.bytes", File$$anonymous$$ode.OpenOrCreate);
Will this work in a final build? Thanks!
Include it in the Resources folder- they all get included in the build. Then you can use Resources.Load on Test.bytes and pass it through your binaryformatter to get the asset back out again!
Sorry, forgive me if this is a stupid question: since Resources.Load() returns a generice object, how do I then transform that back into a Stream to use with BinaryReader?
Never$$anonymous$$d... figured it out :) Used Resources.Load() as TextAsset and then used newObject.bytes!
Thank you very much for all the help!
Your answer
Follow this Question
Related Questions
How to create a gui that the user can write in and save? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do we create a custom class in scripting? 1 Answer
Storing a part of a complex asset in an asset bundle 1 Answer
Unity editor and inspector header 0 Answers