Coming soon - Get a detailed view of why an account is flagged as spam!
view details
2
Creating Compile-time MonoBehaviour Wrappers?
Post Body

Is there a way to mark certain types with attributes, such that, when compiled, automatically generates extra classes based on it?

To clarify, I have a bunch of System.Objects that I would like to interact with using the GameObject Inspector as normal Unity Objects, but I would rather not need to create a new Wrapper class each time it's I want to expose it.

An example of what I currently do:

[Systen.Serializable]
public abstract class SystemType 
{
    <code here for System.Object type>
}

public class GenericWrapper<T> : MonoBehaviour where T : SystemType
{
    // Exposed for serialziation/inspection
    public T wrapped;
}

public class DerivedType : SystemType
{
    <derived code here>
}

public class DerivedWrapper : GenericWrapper<DerivedType>
{
}

In this paradigm, I have to create a wrapper class each time I make a new derived type, and it looks very bad.

What I would rather do is similar, but with this change instead:

[Wrap]
public class DerivedType : SystemType
{
    <derived code here>
}

Which would mark DerivedType to have DerivedWrapper automatically generated by the compiler, and is generally a lot faster, cleaner and easier to do.

Is this possible? and if it is, does it work nicely with Unity's serialization system?

I know of Vexe's framework which allows for easier serialization of these types, but that's mainly for serialization of object graphs within Unity objects, what I need is a Unity object graph that supports polymorphism and abstract classes.

Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
29,703
Link Karma
15,788
Comment Karma
13,897
Profile updated: 1 week ago
Posts updated: 8 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
9 years ago