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
0
Question by regularkid · Mar 11, 2012 at 01:00 PM · assetsavecustomcreate

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!

Comment
Add comment · Show 2
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 syclamoth · Mar 11, 2012 at 01:03 PM 0
Share

In the editor, or in the game? One of these things is quite easy, the other... not so much.

avatar image regularkid · Mar 11, 2012 at 05:04 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · 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 regularkid · Mar 11, 2012 at 05:08 PM 0
Share

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.

avatar image regularkid · Mar 12, 2012 at 01:31 AM 0
Share

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!

avatar image syclamoth · Mar 12, 2012 at 01:36 AM 0
Share

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!

avatar image regularkid · Mar 12, 2012 at 01:44 AM 0
Share

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?

avatar image regularkid · Mar 12, 2012 at 01:49 AM 0
Share

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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