- Home /
How to get type from a string in property drawer ?
I'm trying to make a property drawer which will draw a popup field of derived types of the base type the drawer is on. For that to happen, I first need to find the base type of the field the drawer is set on.
But I can't figure how to get the base type. I always end up with a null value. I must precise that the type I'm trying to get is a custom serializable class, derived from c# native object, not unity object.
public class SubclassEnumDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Type baseType = Assembly.GetExecutingAssembly().GetType(property.type);
// This return null.
Type baseType =Type.GetType(property.type);
// This return null too.
}
}
Hi, i'm trying to do almost the same thing. Did you find the answer for this? thanks in advance
Never$$anonymous$$d. Founded. You can use fieldInfo.FieldType
Answer by Infenix · May 19, 2020 at 03:14 PM
You may find a complete answer to what you're looking for right here : https://stackoverflow.com/questions/983030/type-checking-typeof-gettype-or-is
Well, the topic doesn't seem to talk about getting a c# Type from a string.
I know how to use typeof and obj.GetType(). But those solutions are assu$$anonymous$$g we've object of the searched Type at hand. Here all I have is a SerializedObject and the property.type string.
Try to use the as c# keyword like void function(object obj) { int a = obj as int; //returns null if obj isn't an int }
Your answer
Follow this Question
Related Questions
Getting CustomPropertyDrawer from SerializedProperty 1 Answer
How can you find private fields by reflection inside a Property Drawer? 2 Answers
Use PropertyDrawer with System.Reflection 1 Answer
How to rotate a projectile and mantain constant speed when hitting a wall? 2D 0 Answers
Bullet reflect not working properly 1 Answer