#String Based Type Selection In An Attribute

1 messages · Page 1 of 1 (latest)

fresh wind
#

Disclosure: This question stems from limitations related to game modding, it is not about modding specifically but that is why it's a weird question. If there isn't a suitable answer that's fine.

For attributes that require a type reference, eg. [CustomEditor(Type)] is there a way I can provide a name of a type via a string and let Unity attempt to resolve that somehow? ideally i'd want like [CustomEditor("Item")] or something rather but I'm unfamiliar of the ways i can and can't be dynamic with values in an Attribute deceleration context.

#

I was considering posting this in #↕️┃editor-extensions since that's relevent to the example but theres a couple other places i'm curious about too that's runtime applicable

astral steppe
#

Give your attribute a string parameter and then resolve the string to a type at runtime as needed

fallow ore
#

for existing attributes there's not much way around it, attribute values are baked into the compiled assembly and can't really be dynamic in the slightest

#

you might be able to do something with cecil/a unity IL postprocessor to insert the right values into your assembly after compilation

astral plover
forest shell
#

nameof(TypeName) will return a string with the name of the type

#

or typeof(TypeName) will return the Type itself

astral steppe
forest shell
#

typeof returns the type not the string

#

nameof will return a string

astral steppe
#

you can't do:

string typeName = "MonoBehaviour";
Type t = typeof(typeName);```
#

it doesn't take a string

#

it isn't dynamic, it's resolved at compile time

forest shell
#

I am confused at what exactly are you trying to do

astral steppe
#

I'm not trying to do anything

#

is there a way I can provide a name of a type via a string and let Unity attempt to resolve that somehow? ideally i'd want like [CustomEditor("Item")] or something rather but I'm unfamiliar of the ways i can and can't be dynamic with values in an Attribute deceleration context.

#

Op wrote this^