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
1
Question by wlfbck · Jun 13, 2017 at 07:12 PM · jsonenum

Serialization of an Enum Array

I have some probably easy to solve problem regarding the serialization of an Enum-array. The following code probably explains it best:

 MapGenerator.TileType[,] tilemap = new MapGenerator.TileType[2, 2];
 tilemap[0, 0] = MapGenerator.TileType.Floor;
 tilemap[0, 1] = MapGenerator.TileType.Wall;
 tilemap[1, 0] = MapGenerator.TileType.Ramp;
 tilemap[1, 1] = MapGenerator.TileType.Floor;
 Debug.Log(tilemap[0, 0].ToString());
 Debug.Log(tilemap[0, 1].ToString());
 Debug.Log(tilemap[1, 0].ToString());
 Debug.Log(tilemap[1, 1].ToString());
 
 string json = JsonUtility.ToJson(tilemap);
 Debug.Log(json);

As you can see, i'm just trying to serialize the Enum. This works on a single enum entry, but the output from my code is just {}.

For the sake of completeness here is the Enum code:

 [Serializable]
 public class MapGenerator {
 
     public enum TileType {
         Floor,
         Ramp,
         Wall
     }
 }

If someone has any ideas, that would be great!

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

3 Replies

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

Answer by wlfbck · Jun 14, 2017 at 10:47 AM

To answer my own question: rectangular arrays currently cannot be serialized. Even normal arrays can't with unitys build in serializer. You have to use a wrapper for it.

For my case, i switched to a jagged array, serialized each row with the wrapper. Put those strings in a string array and then serialized that again. It's stupid and should be build into unity itself, but it works.

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
3

Answer by edyuto · May 05, 2018 at 02:09 PM

@wlfbck, I know maybe it's too late, but have you tried this:

public class MapGenerator {

 public enum TileType {
     Floor,
     Ramp,
     Wall
 };

 [Serializable]
 public TileType tileType;

}

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 singlesaplinggames · Jun 01, 2018 at 11:52 AM 2
Share

THAN$$anonymous$$ YOU! It worked for me. I' ve been trying to make it work for almost an hour :D. <3

avatar image
1

Answer by Jamster · Jun 13, 2017 at 07:24 PM

I suspect you need to add the serializable attribute to the enum as well

  [Serializable]
  public class MapGenerator {
  
      [Serializable]
      public enum TileType {
          Floor,
          Ramp,
          Wall
      }
  }

I'm potentially wrong though

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 wlfbck · Jun 13, 2017 at 07:28 PM 0
Share

Already tried that, thank you for the suggestion though ;)

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

68 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

Related Questions

c# How to read a string from Json and convert it to an enum 3 Answers

JsonData to Enum 2 Answers

Problem with second enum selection inspector 0 Answers

how to retrive an array from SQL database in Javascript? 1 Answer

MiniJSON Multiple Rows 1 Answer


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