I have two classes. Both of them are placed in the editor/interface_field folder.
using System;
using UnityEngine;
namespace editor.interface_field
{
public class RequiredInterfaceAttribute : PropertyAttribute
{
public readonly Type type;
public RequiredInterfaceAttribute(Type type)
{
this.type = type;
}
}
}
using UnityEditor;
using UnityEngine;
namespace editor.interface_field
{
[CustomPropertyDrawer(typeof(RequiredInterfaceAttribute))]
public class RequireInterfaceDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if(property.propertyType == SerializedPropertyType.ObjectReference)
{
var requiredAttribute = this.attribute as RequiredInterfaceAttribute;
EditorGUI.BeginProperty(position, label, property);
property.objectReferenceValue =
EditorGUI.ObjectField(position, label, property.objectReferenceValue, requiredAttribute.type, true);
EditorGUI.EndProperty();
}
else
{
var previousColor = GUI.color;
GUI.color = Color.red;
EditorGUI.LabelField(position, label, new GUIContent("Property is not a reference type"));
GUI.color = previousColor;
}
}
}
}
I have another class.
using editor.interface_field;
using UnityEngine;
namespace interface_user.implementations
{
public interface IInterface {}
public class InterfaceUser : MonoBehaviour
{
[SerializeField]
[RequiredInterface(typeof(IInterface))]
UnityEngine.Object interfaceImplementation;
}
}
The [RequiredInterface(typeof(IInterface))] line throws the following error:
The type or namespace name 'RequiredInterfaceAttribute' could not be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]csharp(CS0246)
I can not understand why does not Unity see the RequiredInterfaceAttribute class and how could I make it see it.
*Drawerclasses in theeditorfolder and move the*Attributeclasses to a separate (non-editor) folder and then reference the*Attributeclasses from the*Drawerclasses. This way everything is working. Should I remove my question? I mean yes, you are right the other classes are outside theeditorfolder. \$\endgroup\$