Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Long2904 · Apr 01, 2020 at 02:36 PM · resettilesserializable2d array

How to serialized 2d array of TileBase?

So i made a room script to help prototyping rooms faster and it would save all the tiles (TileBase) in a 2d array. The problem with that is when i entered play mode the array would be reset and become null. Ater a little research i knew that multidimensional array couldn't be serializable so i tried to use binaryformatter and memorystream to serialized it but failed. Then i learned that i can only use that if TileBase itself is serializable but it's not. So are there any ways to do this?

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

1 Reply

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

Answer by Long2904 · Apr 03, 2020 at 01:05 PM

Okay i figured it out! Make a serializable class that store the tilebase (this class don't inheritant from mono behaviour) and have reference to this class instead. For example:

Before:

 public class Rooms : MonoBehaviour
     {
         private TileBase[,] allTiles;
         private Matrix4x4[,] tilesRotation;
     }

After:

 public class Rooms : MonoBehaviour
     {
         public[] SerializableTile serializableTiles;
 
 [Serializable] // this is the class unity will save so don't inheritant from mono behaviour.
     public class SerializableTile
     {
         public TileBase tile;
         public Matrix4x4 rotation;
 
         public SerializableTile(TileBase _tile, Matrix4x4 _rotation)
         {
             tile = _tile;
             rotation = _rotation;
         }
     }
     }



Comment
Add comment · Show 4 · 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 Tomatech · Apr 15, 2020 at 11:06 AM 0
Share

Thank you! I had been building my save system under the assumption that TileBase could not be serialized, knowing that it can has made things much easier. Stay safe during the CoralVita$$anonymous$$

avatar image HuldaGnodima · Apr 02, 2021 at 10:27 AM 0
Share

Could you share how you then save the TileBases? I'm using json/Easy Save, but for some reason some TileBases can't be found upon loading (it's like they dissappear in the saving process). If you got it to work I would LOVE to see how to do it!

avatar image Tomatech HuldaGnodima · Apr 04, 2021 at 12:46 AM 0
Share

Ah... since then, I found out that this solution was serializing tiles as scriptableObject instances, which can change periodically. At the time, I assumed it worked because the instance id’s weren’t changing.

The simple way: create a dictionary of all tiles you want to save (before starting the game). On start, generate a second dictionary that swaps the keys and values of the first dictionary. When saving, use the tileBase as a key to get an int/string to save the time as. When loading, use the other dictionary to turn that string/int back into a tileBase. This will work even for extended tiles like rule tiles

avatar image HuldaGnodima Tomatech · Apr 04, 2021 at 07:23 AM 0
Share

Thank you so much for replying!

I also found out that their ID will change periodically, and yesterday I did a similar solution as the one you're describing. I made a Dictionary that holds tile-names and the tile connected to that name. Everytime I save, I save the name of the tile connected to a tile-coordinate, I then use the name to replace all the tiles when I load.

Your solution sounds a little simpler even! If you wouldn't $$anonymous$$d I would LOVE to see your code how you load/save the tiles specifically? I really searched the web for like 3 days finding nothing, so if you feel comfortable sharing I really think it could be a help to a lot of people!

Again thank you for your reply :)

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

126 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

Related Questions

Objects created in editor tool, fields reset on play and unable to serialize. 2 Answers

can somebody help for the algorithm or code how implement this crossword math? 1 Answer

How to use binaryformatter with custom class in unity? 1 Answer

How to reset a variable when activated 1 Answer

Add seconds to timer countdown. 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