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 Uraani · Feb 22, 2014 at 04:28 PM · spritetilemapserialize

I have issue with unitys serialization

Hi, I'm having a issue with unitys serialization. I'm creating a new scriptableObject and then saving it to assets and it serializes only some of its components. (I'm working on a tile map editor and it wont serialize the created sprites) The editor works just fine until i press playmode, then i have to create the array of tiles again. I have single Tile variable in a different script that inherits a monobehavior and it serializes properly

TileAtlas serializes texture but not the tiles

Sorry for the lenghty format of code

 [System.Serializable]
 
 public class TileAtlas : ScriptableObject
 
 {
 
     public static readonly int size = 8;
 
     public static readonly float uvSize = 0.125f;
 
     public Texture2D texture;
 
     public Tile[] tiles;
 
     public TileAtlas(Texture2D texture)
 
     {
 
         this.texture = texture;
 
     }
 
     public void CreateSprites()
 
     {
 
         tiles = new Tile[size*size];
 
         for (int x = 0; x < size; x++)
 
         {
 
             for (int y = 0; y < size; y++)
 
             {
 
                 Rect rect = new Rect(x*(uvSize*texture.width),y*(uvSize*texture.height),uvSize*texture.width,uvSize*texture.height);
 
                 Sprite sprite = Sprite.Create(texture,rect,new Vector2(0,0),uvSize*texture.width);
 
                 Tile tile = new Tile(sprite);
 
                 this.tiles[x+(y*size)] = tile;
 
             }
 
         }
 
     }
 
 }
 
 [System.Serializable]
 
 public class Tile 
 
 {
 
     public Sprite sprite;
 
     public Tile (Sprite sprite)
 
     {
 
         this.sprite = sprite;
 
     }
 
 }
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
1

Answer by whydoidoit · Feb 22, 2014 at 04:29 PM

Tile requires a default constructor that takes no parameters to be serialized correctly.

Comment
Add comment · Show 6 · 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 Uraani · Feb 22, 2014 at 05:40 PM 0
Share

Im assu$$anonymous$$g you mean this? It didn't work.

 [System.Serializable]
 public class Tile 
     {
     public Sprite sprite;
     public Tile ()
     {
         this.sprite = new Sprite();
     }
     public Tile (Sprite sprite)
     {
         this.sprite = sprite;
     }
 }
 
avatar image Uraani · Feb 22, 2014 at 06:22 PM 0
Share

I found this quite weird but if set the tile.sprite to a spriteRenderer then the tile.sprite gets serialized?! this makes absolutely no sense to me.

avatar image whydoidoit · Feb 23, 2014 at 05:55 AM 0
Share

Actually you should just need

   public Tile() {}

Just so it can create one. I'm not sure on what might be happening with the sprite and renderer thing though

avatar image vexe · Feb 26, 2014 at 09:37 AM 0
Share

@whydoidoit can you elaborate more on why would one needs an empty constructor for a regular class to be serialized correctly? I use regular classes quite often - but I never payed attention to that detail with no problems so far

avatar image whydoidoit · Feb 26, 2014 at 10:23 AM 2
Share

If you have a constructor - which the OP does - then the compiler no longer makes you a default one which takes no parameters. As the serialization system has no idea what you are supposed to pass to the constructors it works by using the default one and then setting the variables of the resulting object. If you don't have a default constructor then it can't make that initial instance so if you declare any constructor that takes parameters you must also define one that does not to support serialization.

Show more comments

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

21 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

Related Questions

Pixels per unit with tile palette? 1 Answer

tilepalette problems 0 Answers

How can I disable collision for 1 tile? 4 Answers

Get sprite of Tile 2 Answers

Is it possible to turn a tilemap into one unique sprite? 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