• 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 adaleigh · Jul 13, 2014 at 02:14 AM · prefabinheritancecustom editor

Custom Editor not Persisting Changes to Prefab

So, I am creating a procedural iso tile map generator. I created a custom editor for tiles. You can attach a sprite to a tile, specify it's z order, and give a list of tile indices that can be next to it in each direction (N, NW, W, SW, S, SE, E, NE). The problem is that the tile indices per direction will not persist in a prefab.

The relevant code for Tile is:

 public class Tile : MonoBehaviour {
   public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST};
   //hack
   [System.Serializable]
   public class SillyHashSet : HashSet<int> { }
   [System.Serializable]
   public class SillyDictionary : Dictionary<Direction, SillyHashSet> { }
 
   public int zOrder = 0;
   public SillyDictionary neighborTiles;
   public Sprite sprite;
 
   public void Awake() {
 
   }
 }

I had to use that inheritance hack to get the special instance of templating to serialize into the unity editor. Here is the relevant part of my editor script.

 [CustomEditor(typeof(Tile))]
 public class TileEditor : Editor {
     private Tile.Direction direction = Tile.Direction.NORTH;
     private int addingInt = 0;
     public override void OnInspectorGUI() {
         Tile tile = (Tile)target;
         if(tile.neighborTiles == null) {
             tile.neighborTiles = new Tile.SillyDictionary();
         }
         addingInt = EditorGUILayout.IntField("Add Tile Index As Neighbor", addingInt);
         EditorGUILayout.BeginHorizontal();
         if(GUILayout.Button("Add")) {
             if(!tile.neighborTiles.ContainsKey(direction)) {
                 tile.neighborTiles[direction] = new Tile.SillyHashSet();
             }
             tile.neighborTiles[direction].Add(addingInt);
         }
         EditorGUILayout.EndHorizontal();
         if(tile.neighborTiles.ContainsKey(direction)) {
             List<int> removes = new List<int>();
             foreach(int index in tile.neighborTiles[direction]) {
                 EditorGUILayout.BeginHorizontal();
               EditorGUILayout.PrefixLabel(index.ToString());
               if(GUILayout.Button("Remove")) {
                   removes.Add(index);
               }
               EditorGUILayout.EndHorizontal();
             }
             foreach(int r in removes) {
                 tile.neighborTiles[direction].Remove(r);
             }
         }
     }
 }

What could be going wrong here?

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

Answer by skalev · Jul 13, 2014 at 07:04 AM

you are trying to save data into a HashSet, and a Dictionary.

Marked Serialiazable or not, Unity can't do this.

You'll have to use data types unity can work with.

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 adaleigh · Jul 13, 2014 at 06:01 PM 0
Share

According to this: http://answers.unity3d.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html

You can. And it is true, they weren't showing up in the inspector AT ALL until I did this.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Marking property as dirty in property drawer after changing its objectReferenceValue 1 Answer

Using a custom editor script with a prefab instance causes my variables to reset on Play 3 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Automatically increment an integer field in a bunch of prefab gameobjects in the unity editor. 0 Answers

Prefebs and 'diffrent' enemy types 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