Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 tr1stan · Aug 15, 2012 at 11:45 AM · editorasset

How to save an asset

Hello,

I have written a script to save data set into unity universal asset format (.asset format) at runtime. This method works fine and all data within that .asset file were kept as long as the unity editor is running. But if I quit unity editor application and re-open this project, all data were lost. why?

Somebody please let me know how to successfully save that asset.

Thanks in advance

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by AYoung_PikPok · Apr 20, 2015 at 03:09 AM

You have to first mark the asset as dirty using EditorUtility.SetDirty(assetObject) Then it will be saved to disk when AssetDatabase.SaveAssets is called, either manually or when the project is saved.

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 PaulUsul · Apr 26 at 01:56 PM 0
Share

SetDirty works well! Additionally If the object is part of a Scene, the Scene is marked dirty. If the object may be part of a Prefab instance, you additionally need to call PrefabUtility.RecordPrefabInstancePropertyModifications to ensure a Prefab override is created. from: https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html

avatar image
1

Answer by DaveA · Aug 16, 2012 at 12:49 AM

You probably want something in the AssetDatabase collection: http://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.SaveAssets.html

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 DESTRUKTORR · Aug 17, 2012 at 02:32 AM 0
Share

That looks like what he's lookin' for.

avatar image
-1

Answer by DESTRUKTORR · Aug 15, 2012 at 03:32 PM

There is no simple answer to this question, and many have asked similar questions, but there aren't really any efficient methods to save all assets' data at runtime, in Unity, like a game-save.

However, there are several methods to save large amounts of data that are commonly used:

  1. XML Serialization.

  2. I/O (Input/Output)(Javascript) (C#) <--- C# is much easier than JS, for this... trust me :P

  3. Check out this answer.

Personally, I prefer C# I/O. It allows for a lot easier customization of your system, once you've got the base methods down (eg. where the files go, what they're called, all that fun stuff).

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 tr1stan · Aug 16, 2012 at 12:38 AM 0
Share

What Im looking for is to save a specific .asset file. Because I am building this on top of an existing project with the .asset format to keep the data, I cannot switch to I/O nor the X$$anonymous$$L format.

And it's actually not necessary to save that file at runtime. In my case my data is populated at runtime, and the .asset file is filled with the correct data as long as the unity editor application is running. But I quit unity, the data will be lost and the .asset file turns will revert to empty. So I'm looking for a method to save that before unity quits, which may not necessarily to be save at runtime.

Do you know how can I save that .asset file?

avatar image
0

Answer by AWollert · Nov 22, 2019 at 12:11 PM

Since I have the same issue now in 2019 and didn't find any satisfying answer I wake this post up again. I am building my application (why would anyone not build it?) and therefore, I cannot use the UnityEditor classes AssetDatabase. How do you save your assets at runtime?

Best regards

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 AWollert · Dec 19, 2019 at 10:10 AM 0
Share

The answer is, that anything you want to save during runtime you have to convert to a byte array and save it with File.WriteAllBytes(string path, byte[] bytes); -> https://docs.unity3d.com/ScriptReference/Windows.File.WriteAllBytes.html

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

13 People are following this question.

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

Related Questions

Editor Window with settings and Asset Creation 1 Answer

ReImport in c# of GameObjects only for Scene Objects, not assets? 1 Answer

CreateAssetMenu below builtin/default create asset menu items 1 Answer

Duplicated Terrain Object and Data STILL affects other terrains 4 Answers

Modifying sprite assets via script? 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