Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Serge144 · Dec 15, 2019 at 10:04 PM · terrainterraindatastoragepersistentdatapathbinaryformatter

Saving game data and not user data

I'm currently using the Application.persistentDataPath (with BinaryFormatter) in order to save some data. The problem is, this data is NOT user data (like highscores etc), it is data that is actually needed for all my scenes. (Basically, i'm just saving the terrain details (terrain.GetDetailLayer), i'm saving it because these details may be removed while playing the level, and so i must bring the details to its initial state once the level is repeated. Unity Editor is not capable of doing this (at least that i'm aware of, after removing details in runtime, it doesn't go back to its original state when i stop the game). So, the problem is, according to this page: https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html it says that "The files can still be erased by users directly", and since this data is FUNDAMENTAL to my levels, i cannot let users delete it and i must export this data in the build itself. So how can i save PRIVATE game data, that is fundamental for any of my game levels? 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Serge144 · Dec 15, 2019 at 11:06 PM

I guess what i could do is to save these files for each level in some folder (while in development stage), then, when i'm happy with it, i'll paste those files into the Assets folder, which the game will access to bring to the inicial state (Application.dataPath) Any pro that could aprove this? ^

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 Bunny83 · Dec 16, 2019 at 12:35 AM

There's no real need to manually store that data in a file. In a built game you can not possible alter the assetfiles. You can alter the loaded assets in memory but the actual serialized / packed files in the build will never change. When the game is restarted all assets will have their state as the game was build. However if you need to reset the state during a single session you should be able to just Instantiate the original TerrainData object and use / alter that one. Of course you should keep a reference to the original asset. That means whenever you start your game you would first instantiate the original TerrainData asset and replace the original with the instance. The easiest way would be a script like this:

 public TerrainData data;
 public Terrain terrain;

 void Start()
 {
     var inst = Instantiate(data);
     terrain.terrainData = inst;
     var col = terrain.GetComponent<TerrainCollider>();
     if (col != null)
         col.terrainData = inst;
 }

So you just assign your Terrain object to the terrain variable and your original TerrainData asset to the data variable. Whenever Start is called it will instantiate the original TerrainData asset and replace it in the Terrain script and the TerrainCollider that is attached to the Terrain. This way you never alter your original asset. I barely use Terrains and I have never actually tried this with the TertainData. Maybe just instantiating isn't enough (but it should). If it doesn't work you can essentially do the same thing, but in addition copy over all the individual data in case it's not properly instantiated.

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 Serge144 · Dec 16, 2019 at 09:52 AM 0
Share

Ok so basically i should do what i was saying, i must have that data in the assets folder in order to restore the details of the terrain. Basically i'll do this: design my level and draw details onto the terrain, then when i'm happy with it, i'll save that in some file, which i will need to put in the assets folder, then in script i tell it to load always from assets folder in order to always have the same details. I didn't quite understand what you said, because first you say that "There's no real need to manually store that data in a file" but then you say "Whenever Start is called it will instantiate the original TerrainData asset and replace it in the Terrain script and the TerrainCollider that is attached to the Terrain.". The first sentence you say there's no need to save, but in the second you're implying that there is some asset file.. so i got a bit confused on how i should do it.

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

144 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 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 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

How to get terrain data? 0 Answers

Terrrain Texturing with solid colors,Terrain texturing with solid color tile 0 Answers

Best Practice for loading variable Data for Different Forms of same Prefab 0 Answers

How do you make the material in the Terrain detail prototype properties update after runtime? 1 Answer

How to get a terrain's maximum height 3 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