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 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to detect if Prefab was added to the scene in Edit Mode? 1 Answer

How can I create and override Prefabs from a script? 0 Answers

How to save prefabs with private data through inheritance 1 Answer

Serialization with custom editor, prefab not saving (again) 1 Answer

Can a class's Editor call its component's Editor? Likewise, can a derived class's Editor call its base class's Editor? 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