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
0
Question by CorruptedTNC · Feb 07, 2017 at 04:38 AM · c#assetserializationdatabase

Assistance needed with serialization, inheritance and scriptableobject

So I've hit that roadblock of "I'm trying to serialize child classes but everything is being serialized as the parent class". I expected this, but despite the base class inheriting from ScriptableObject, I can't seem to store my instances in a .asset file, as they're showing up with a Type Mismatch error.

Here's an example: I have a generic ScriptableObjectDatabase class, which should be capable of storing whatever I pass it:

 public class ScriptableObjectDatabase<T> : ScriptableObject where T : class
 {
     [SerializeField] protected List<T> database = new List<T>();

     public static T GetDatabase<T>(string dbPath, string dbName) where T : ScriptableObject
     {
         var dbFullPath = @"Assets/" + dbPath + "/" + dbName;

         var db = AssetDatabase.LoadAssetAtPath(dbFullPath, typeof(T)) as T;

         if (db != null) return db;

         if (!AssetDatabase.IsValidFolder("Assets/" + dbPath))
             AssetDatabase.CreateFolder("Assets", dbPath);

         db = CreateInstance<T>();
         AssetDatabase.CreateAsset(db, dbFullPath);
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();

         return db;
     }
 }

Then different types of databases exist with different functionality, the one I'm working on now being the ItemDatabase which does very little. So I have an Item class, which is the base for other classes, such as Equipment etc.

 public class Item : ScriptableObject
 {
     //fields etc
 }

 public class Equipment : Item
 {
     //equipment specific fields
 }

I then have an item editor window, which is responsible for managing the items in a .asset file.

 public class ItemEditor : EditorWindow
 {
     private ItemDatabase itemDatabase;
     
     private void OnEnable()
     {
         if (itemDatabase == null)
             itemDatabase = ItemDatabase.GetDatabase<ItemDatabase>(DATABASE_PATH, ITEM_DATABASE_NAME);
     }
     
     private void OnGUI()
     {
         if (GUILayout.Button("Add"))
         {
             ItemDatabase.Add(CreateInstance<Item>());
         }
     }
 }

Then finally when I look at the database via the inspector, I see the following: Broken thing :( I'm assuming that the information inside the database is no longer saving as SetDirty(itemDatabase) isn't enough to trigger saving the scriptable object? I can edit things on the fly but writing them to disk isn't working out right now.

Any guidance on what I'm doing wrong would be greatly appreciated.

testscript.png (4.6 kB)
Comment
Add comment · Show 1
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 hexagonius · Feb 07, 2017 at 04:43 PM 0
Share

I am not totally sure about what I'm about to say, but I think the problem is working with T ins$$anonymous$$d of ScriptableObjectDatsBase. if T was a float then that's what you're using for your AssetDatabase, not the actual ScriptableObject as a whole.
furthermore, I'm pretty sure only concrete classes are capable of being serializable, but proof me wrong. the last time I tried genetics with that I needed to create subclasses for each type.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam-Mechtley · Feb 07, 2017 at 08:08 AM

Is there a reason your generic type constraint on ScriptableObjectDatabase is simply class? It seems to me like it should be ScriptableObject. I wonder if that would make the difference.

Also, I'd suggest for readability that you rewrite the signature for GetItemDatabase as something like public static TDatabase GetItemDatabase<TDatabase> (string dbPath, string dbName) where TDatabase : ScriptableObjectDatabase<T>

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

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I access assets of an asset database .asset file via c# ? 1 Answer

"Save As..." Saving level assets at Runtime 0 Answers

why are "The associated scripts can not be loaded" errors seemingly being thrown at random for .asset files 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