Search Feedback

2651
votes

Serialization of Polymorphic Data Types

General

-

-

You should be able to Serialize polymorphic data types. A quick example:

[Serializable] class Base { public int myInt; }
[Serializable] class Derived : Base { public string myString; }

class MyBehaviour : MonoBehaviour
{
public Base _myBase = new Derived(); // Error... This will NOT work, only myInt will be saved.
}

ScriptableObject does not solve this issue because they cannot be saved into prefabs. Full post on this subject here: http://www.codingjargames.com/blog/2012/11/30/advanced-unity-serialization/.

Please consider this when rewriting the serialization to support nested prefabs!

Comments (43)

  1. 67195a1a7dfa9eef8563f70a76c92a58?d=mm

    change

    Jul 31, 2017 10:49

    why not?

  2. Bd8b27c07a270dc81f2f370d7f2bfab9?d=mm

    Serhii-Horun

    Jul 29, 2017 10:23

    +8 votes from me.

  3. 78ed265f0b33e2208570927bc7b5dcb4?d=mm

    HexPix

    Jun 27, 2017 20:54

    This has been posted as a request since 2002. The community is begging for the very much needed overhaul to the serialization system. This suggestion alone has almost 2500 votes; nevermind the countless others on the same subject of basic serialization stuff unity should be able to do. Yet...Nothing... Even as a beginning developer, just finishing my first commercial-release game, this is making me seriously consider switching to Unreal4... If I'm going to spend the time to learn this, might as well learn the right stuff, in an engine that actually has a future... And respect for the community's needs regarding the engine's features; neither of which I'm seeing here in Unity. =(

  4. 21448c4be0ee84cd5ad0558811c9be81?d=mm

    s-ta-c

    May 28, 2017 19:16

    If this doesn't get implemeted soon, I may switch to unreal, or godot, or jıst any pther engine that has a sane way of scripting.
    +5 votes from me.

  5. Cfc92d15d6e1d20ea0afb32f27ed25d9?d=mm

    nuverian

    May 25, 2017 19:01

    I want 100 votes more to put here

  6. C1add2ff230213b25de9d56e8f77b97b?d=mm

    Aggressor

    Jan 23, 2017 15:52

    I'm out of votes but I support this!

  7. 3cbf274fa13971491fb6eea6c88db922?d=mm

    llamagod

    Jan 19, 2017 14:31

    You could try this: https://www.assetstore.unity3d.com/en/#!/content/79135
    It enables you to display and serialize polymorphic types, dictionaries, properties, generic types, HashSets and delegates.

  8. 38094335cb7c5bc3a9fa46926958f326?d=mm

    Polymo

    Jan 03, 2017 11:01

    Fix this already..

  9. D908420bf326ab17239235b62d4e5103?d=mm

    xarismax

    Dec 19, 2016 18:21

    Good job unity, version 5.5 has been released and nothing has changed !!!
    You force developers to write bad unmaintainable code.

    Unity Version 9.99 (Galaxy Edition) released after 1.000 years:
    a) Unity editor is 1 millisecond faster.
    b) 0.0001% better graphics. Despite 99999 assets doing the same thing = Redundancy.
    c) Serialization system not fixed, please use regular assembly for "object construction".

    Full inspector does it, why is unity team lazy to support scripting. Forcing us to write code in assembly without interfaces.

    So i have an interface with 10 implementations ? Regular c#: 1 line of code to instantiate.
    Unity Way? 11 classes of 10 lines of code each = 110 lines of code. Really 11000% more code?

    Conclusion: Unity is a piece of crap, that tries to vendor lock you to its crap.
    1) Monobehaviour? doesn't serialize interfaces.

    2) ScriptableObject? Thank you for forcing us to :
    a) create 1 asset per instance. Leading to pollution of lots small files.
    b) waste 5 min productivity loss, to "switch to project", "create new scriptable object", "lose inspector focus", "set values", "return back to scene", "return inspector focus", "remember where the hell you left it off".
    c) no way to store the scriptableObject in the "scene" like monobehaviour or in a "prefab".
    d) pollute the editor namespace with ScriptableObject types, good luck searching in a list of 1000 scriptableObject types, just to instantiate one. Those Menus LOVE YOU. I am wondering how long will it take you to reach the 999th one.

    3) Subassets ? Not supported. No editor gui, no "Drag and drop", no "create". Nothing.

    So to write "Good code" you are forced to :
    a) Never use unity gui or editor.
    b) Inline all objects in code. If you want to make a slight modification, you have to wait 1,5min compile time. Unity Loves you.
    c) assembly loves you.

  10. 5b7023acec6891ab14f4d719f14bfaa6?d=mm

    tcoursow

    Nov 23, 2016 15:42

    This would bring --> 10000% <--- percent development speed improvement...
    For example a Message System with an abstract Message Component class where you could read out all existing Message Types in the Project and select a type via custom editor and then edit this type of message...

    Or even more advanced if unity would have a build-in generic inspectors which would handle such stuff exactly like this

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea

Categories

All

(9114)

2D

(225)

Ads

(40)

AI & Navigation

(68)

Analytics

(96)

Animation

(309)

Asset Store

(229)

Assets

(493)

Audio

(158)

Cloud Build

(95)

Collaborate

(32)

Docs & Tutorials

(198)

Editor

(2104)

Everyplay

(13)

Game Performance Reporting

(13)

General

(819)

Graphics

(768)

GUI

(366)

Input

(146)

Licensing

(75)

Networking

(158)

Physics

(343)

Platforms

(405)

Profiling & Optimization

(69)

Runtime

(162)

Scripting

(1022)

Terrain

(150)

WebGL

(131)