#Unity, C# Attributes & Reflection

1 messages · Page 1 of 1 (latest)

sinful meadow
#

I have come to a point where I've totally given up on Unity's serialization of interfaces. [SerializedReference] doesn't do what it's supposed to do at all, and I don't wish to create a bunch of unnecessary classes just to serialize.

Either way, now I've resorted to using UnityEngine.Object as the serializing type for these "un-serializable" objects. Now, however, I wish to create an attribute [Restrict] that restricts the type of the object.

However, I struggle with figuring out how to use reflection to loop over all the fields that use this attribute, and apply checks for them. To use FieldMember.GetValue() I require an instance, but I don't know how to get/specify this instance, seeing as I want to implement a sort of global manager that handles this reflection.

So, to be honest, I don't even know where to go from here. I've got no clue how to construct this system. If anyone has experience and knows a good way of doing such a thing I'd appreciate your help.

Thanks in advance.

stoic warren
#

As I've mentioned here, I did something like that in the past.
I don't think I finished it, and I'm not home rn. But I can see if I can dig up that code later and share it, if you think that could help.

Until then, I would recommend you have a look at Unity's SerializedProperty and try reverse-engineering it.

sinful meadow
sinful meadow
stoic warren
#

Okay, I had a look again; the reason I wrote my own system is that I wanted to be more flexible with regards to creating and managing components. So I effectively have an entire component system.
Due to that, my code may be more complex in some places than necessary for your purposes.
Also, I essentially built somewhat of a custom TypeCache, because I didn't know that class existed, back then.
There may be other places I would not implement like that anymore as well.

But anyway, here you go: https://paste.myst.rs/e9vjp6j3
PW: "serialize", expires in 1 week.
Note that this is not all the code, but mostly the editor part. The actual serialization was never finished, but the plan was to essentially convert the data pulled from TypePropertyMap and values from ComponentHolder (which is basically a MonoBehaviour implementing ISerializationCallbackReceiver) instances into a flat binary structure (and back).

sinful meadow