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 Skyrunner · Mar 30, 2017 at 10:29 AM · scriptableobjectdictionary

Dictionaries don't serialize within scriptable objects

Hello. I've been working on the problem of dictionaries within scriptable objects. The most common solution of making a non-generic dictionary that implements ISerializationCallbackReceiver doesn't actually seem to work for me. When I use the Asset menu to create a new ItemTemplate (which is scriptableobject) and then start/close the Game view, because the deserializer sees nothing within the Lists within the SerializeableDictionary it wipes everything.

Here's the relevant code:

This is the ScriptableObject:

 public class ItemTemplate : ScriptableObject
 {
     public SDict_StrStr tags_string;
     public Dictionary_StrInt tags_int;
     public int test;
     public string nametest;
     
     public ItemTemplate()
     {
         tags_string = new Dictionary_StrStr();
         tags_int = new Dictionary_StrInt();
     }
 
     public void setTestTags()
     {
         tags_string.Add("name", "base herb");
         tags_string.Add("description", "It's a base herb.");
 
         tags_int.Add("basecost", 100);
 
         tags_float.Add("volume", 5);
         tags_float.Add("trait:base", 1);
 
         tags_bool.Add("isPlant", true);
 
         test = 8;
         nametest = "base herb";
     }
 }

This is the SerializableDictionary:

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using UnityEngine;
 
 
 // adapted from http://answers.unity3d.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html
 
 [Serializable]
 public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
 {
     [SerializeField] public List<TKey> keys = new List<TKey>();
     [SerializeField] public List<TValue> vals = new List<TValue>();
 
     public SerializableDictionary() {
     }
 
     public void OnAfterDeserialize()
     {
         if (keys.Count != vals.Count)
         {
             throw new ArgumentException(string.Format("{0} things in keys and {1} things in vals. Mismatch."
                 , keys.Count, vals.Count));
         }
         for(int i = 0; i < keys.Count; i++)
         {
             Add(keys[i], vals[i]);
         }
     }
 
     public void OnBeforeSerialize()
     {
         keys.Clear();
         vals.Clear();
         foreach (var kp in this)
         {
             keys.Add(kp.Key);
             vals.Add(kp.Value);
         }
     }
 }
 
 [Serializable]
 public class Dictionary_StrStr: SerializableDictionary<string, string>, ISerializationCallbackReceiver
 {
 }
 
 [Serializable]
 public class Dictionary_StrInt : SerializableDictionary<string, int>
 {
 
 }


The serializer does serialize primitive fields like int/str within ItemTemplate, so it's being serialized. Just not completely. How can I make the dictionaries within ItemTemplate be serialized?

Also, could the fact that I'm using a MenuItem to create ItemTemplates be the problem? The code is like such:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class MakeItemTemplate {
 
     [MenuItem("Assets/Create/Item Template")]
     public static ItemTemplate Create() {
         string path = EditorUtility.SaveFilePanelInProject("Create new ItemTemplate",
                         "NewItemTemplate.asset",
                         "asset",
                         "Please select file name to save ItemTemplate as:");
 
         ItemTemplate asset = ScriptableObject.CreateInstance<ItemTemplate>();
         if (!string.IsNullOrEmpty(path))
         {
             asset.setTestTags();
             AssetDatabase.CreateAsset(asset, path);
             AssetDatabase.SaveAssets();
             return asset;
         }
         return asset;
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

Assigning scriptable objects to dictionary through array? 0 Answers

How to use ScriptableObject for Inventory System 0 Answers

Serializing Dictionary: Data lost between sessions 0 Answers

[Solved]How to serialize Dictionary with Unity Serialization System 6 Answers

Add every ScriptableObject to a dictionary? 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