public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (IsValidType(property) == false) { GUI.enabled = false; position.height = lineHeight; EditorGUI.PropertyField(new Rect(position.x, position.y, position.width - margin, lineHeight), property); GUI.enabled = true; position.y += lineHeight + margin; EditorGUI.HelpBox(position, InvalidTypeMessage, MessageType.Error); return; } InterfaceSearch search = GetAutoSearch(); SerializedProperty gameObjectProperty = GetGameObjectProperty(property); if (triedToAutoFind == false && gameObjectProperty.objectReferenceValue == null) { if (search.HasFlag(InterfaceSearch.Scene)) { if (FindInScene(property, gameObjectProperty)) { triedToAutoFind = true; } } if (triedToAutoFind == false && search.HasFlag(InterfaceSearch.Assets)) { FindInAssets(property, gameObjectProperty); } triedToAutoFind = true; } GameObject go = GetGameObject(property); Rect originalRect = position; originalRect.height = lineHeight; position.width -= validMarkWidth; position.height = lineHeight; bool isValid = IsValid(go, property); if (go == null) { EditorGUI.PropertyField(new Rect(position.x, position.y, position.width - margin, position.height), gameObjectProperty, label); Rect buttonRect = originalRect; buttonRect.width = validMarkWidth / 3; buttonRect.x += originalRect.width - validMarkWidth; if (GUI.Button(buttonRect, "Scene")) { FindInScene(property, gameObjectProperty); } buttonRect.x += validMarkWidth / 3; if (GUI.Button(buttonRect, "Assets")) { FindInAssets(property, gameObjectProperty); } buttonRect.x += validMarkWidth / 3; if (GUI.Button(buttonRect, "Child")) { FindInChildren(property, gameObjectProperty); } } else { EditorGUI.PropertyField(new Rect(position.x, position.y, isValid ? position.width - margin : originalRect.width, position.height), gameObjectProperty, label); } position.y += lineHeight; if (isValid == false) { var interfaces = GetInterfaces(property); if (triedToAutoConvert == false && go != null) { triedToAutoConvert = true; OnTryToConvertToWrapper(go, interfaces.FirstOrDefaultFast()); } position.y += margin; foreach (Type @interface in interfaces) { Color color = GUI.color; bool hasInterface = go != null && go.GetComponent(@interface); if (go != null) { GUI.color = hasInterface ? Color.green : Color.red; } position.width = originalRect.width; EditorGUI.HelpBox(position, @interface.Name, hasInterface ? MessageType.Info : MessageType.Error); position.y += lineHeight + margin; GUI.color = color; } } else { Color c = GUI.color; GUI.color = Color.green; GUI.enabled = false; GUI.Button(new Rect(originalRect.x + position.width, originalRect.y, validMarkWidth, lineHeight), "Valid"); GUI.enabled = true; GUI.color = c; } }
public CheckInterfaceAttribute(InterfaceSearch autoSearch, params Type[] interfaces) { this.autoSearch = autoSearch; this.interfaces = new List <Type>(interfaces); }