for my unity project I'm trying to make a custom property drawer in order to make inspector accept objects that are only implementing the interface I pass via attribute parameter. I am glad that I achived the task satiably for MonoBehaviour objects. However I'm having trouble in applying a similar approach for scriptable objects. There I need help.
Here is my property drawer code. The Problem is C# doesnt let me do a type-check with "is" keyword. Here are few codes I tried
if(temp_SO is requiredAttribute.requiredType) {}
if(temp_SO is T) {}
if(temp_SO.GetType().Equals(requiredAttribute.requiredType) {}
but all fails...
However as seen in the code below, if I hardcode it like if(temp_SO is interface_C)
then it works. I couldn't find a way to pass the type value dynamically. Because IDE keeps throwing "a constant value is expected" error. Any help will be appreciated. Thanks..
[CustomPropertyDrawer(typeof(SOInterfaceExposeAttribute))]
public class SOInterfaceExposeAttributeDrawer : PropertyDrawer
{
Type T;
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
if ( property . propertyType == SerializedPropertyType . ObjectReference )
{
var requiredAttribute = this . attribute as SOInterfaceExposeAttribute;
T = requiredAttribute . requiredType;
var lastValidRef = property . objectReferenceValue;
EditorGUI . BeginProperty(position , label , property);
ScriptableObject temp_SO = EditorGUI . ObjectField(position , label , property . objectReferenceValue , typeof(ScriptableObject) , true) as ScriptableObject;
// Finish drawing property field.
if ( temp_SO != null )
{
if ( temp_SO is Interface_C)
{
Debug . Log("interface component found");
property . objectReferenceValue = temp_SO as ScriptableObject;
}
else
{
Debug . Log("interface component NOT found");
property . objectReferenceValue = lastValidRef;
}
}
EditorGUI . EndProperty();
}
else
{
// If field is not reference, show error message.
// Save previous color and change GUI to red.
var previousColor = GUI . color;
GUI . color = Color . red;
// Display label with error message.
EditorGUI . LabelField(position , label , new GUIContent("Property is not a reference type"));
// Revert color change.
GUI . color = previousColor;
}
}
}