Skip to content

Commit f20c160

Browse files
author
Cippo
committed
now you can expose ''single line'' the target
1 parent 1937ea8 commit f20c160

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

InterfaceReference/Examples/Examples.unity

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ GameObject:
284284
- component: {fileID: 793930335}
285285
- component: {fileID: 793930334}
286286
m_Layer: 0
287-
m_Name: Component with Type Filter
287+
m_Name: Component with Type Filter Attribute
288288
m_TagString: Untagged
289289
m_Icon: {fileID: 0}
290290
m_NavMeshLayer: 0
@@ -404,8 +404,8 @@ MonoBehaviour:
404404
- FavParams: 0
405405
FavString: []
406406
list:
407-
- {fileID: 122467412}
408-
ActiveGameObject: {fileID: 122467412}
407+
- {fileID: 793930333}
408+
ActiveGameObject: {fileID: 793930333}
409409
GUIDsList: []
410410
GUIDsActiveGameObject:
411411
PATHsList: []
@@ -416,8 +416,8 @@ MonoBehaviour:
416416
- FavParams: 0
417417
FavString: []
418418
list:
419-
- {fileID: 1824733537}
420-
ActiveGameObject: {fileID: 1824733537}
419+
- {fileID: 122467412}
420+
ActiveGameObject: {fileID: 122467412}
421421
GUIDsList: []
422422
GUIDsActiveGameObject:
423423
PATHsList: []
@@ -428,8 +428,8 @@ MonoBehaviour:
428428
- FavParams: 0
429429
FavString: []
430430
list:
431-
- {fileID: 793930333}
432-
ActiveGameObject: {fileID: 793930333}
431+
- {fileID: 1824733537}
432+
ActiveGameObject: {fileID: 1824733537}
433433
GUIDsList: []
434434
GUIDsActiveGameObject:
435435
PATHsList: []
@@ -606,7 +606,7 @@ GameObject:
606606
- component: {fileID: 1968288555}
607607
- component: {fileID: 1968288554}
608608
m_Layer: 0
609-
m_Name: Component with Exposed Reference
609+
m_Name: Component with Expose Attribute
610610
m_TagString: Untagged
611611
m_Icon: {fileID: 0}
612612
m_NavMeshLayer: 0
@@ -623,6 +623,9 @@ MonoBehaviour:
623623
m_Script: {fileID: 11500000, guid: 5d7b9862a7a50ed478a95fe23ae1a3c7, type: 3}
624624
m_Name:
625625
m_EditorClassIdentifier:
626+
customInterfaceReference:
627+
name: Component that Implements Interface (ICustomInterfaceExample)
628+
target: {fileID: 1308923936}
626629
--- !u!4 &1968288555
627630
Transform:
628631
m_ObjectHideFlags: 0

InterfaceReference/Scripts/Attributes/Expose.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
using CippSharp.Interfaces;
77
using UnityEditor;
88
using UnityEngine;
9-
using Object = UnityEngine.Object;
109

1110
namespace CippSharp.Interfaces
1211
{
13-
public class ExposeAttribute : Attribute
12+
public class ExposeAttribute : PropertyAttribute
1413
{
15-
ExposedReference<>
14+
1615
}
1716
}
1817

@@ -26,13 +25,7 @@ namespace CippSharpEditor.Interfaces
2625
[CustomPropertyDrawer(typeof(ExposeAttribute))]
2726
public class ExposeAttributeDrawer : PropertyDrawer
2827
{
29-
private object target = null;
30-
private readonly Object nullObject = null;
31-
private Object TargetObject
32-
{
33-
get { return target != null ? (Object)target : nullObject; }
34-
set { target = value; }
35-
}
28+
private SerializedProperty ser_target = null;
3629

3730
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
3831
{
@@ -42,35 +35,32 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
4235
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4336
{
4437
ExposeAttribute exposeAttribute = attribute as ExposeAttribute;
45-
Type fieldType = fieldInfo.FieldType;
46-
if (property.propertyType == SerializedPropertyType.Generic && fieldType.IsInterface)
38+
if (property.propertyType == SerializedPropertyType.Generic)
4739
{
48-
if (exposeAttribute != null)
40+
if (ser_target == null)
4941
{
50-
target = fieldInfo.GetValue(property.serializedObject.targetObject);
51-
if (target == null)
52-
{
53-
TargetObject = EditorGUI.ObjectField(position, TargetObject, typeof(Object), true);
54-
fieldInfo.SetValue(property.serializedObject.targetObject, TargetObject);
55-
}
56-
else if (target is Object)
42+
ser_target = property.FindPropertyRelative("target");
43+
}
44+
45+
if (ser_target != null)
46+
{
47+
if (exposeAttribute != null)
5748
{
58-
TargetObject = EditorGUI.ObjectField(position, TargetObject, typeof(Object), true);
59-
fieldInfo.SetValue(property.serializedObject.targetObject, TargetObject);
49+
EditorGUIUtils.DrawProperty(position, ser_target, label);
6050
}
6151
else
6252
{
63-
//Not implemented for interfaces that aren't also of type Object.
53+
Debug.LogError("Failed to assign the attribute.");
6454
}
6555
}
6656
else
6757
{
68-
Debug.LogError("Failed to assign the attribute.");
58+
Debug.LogWarning("Expose Attribute supports only Interface Reference fields. (Or fields with a sub-"+typeof(SerializedProperty).Name+" with name <i>target</i>");
6959
}
7060
}
7161
else
7262
{
73-
Debug.LogWarning("Expose Attribute supports only Interface Reference fields.");
63+
Debug.LogWarning("Serialized Property is not of Generic type.");
7464
}
7565
}
7666
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
namespace CippSharp.Interfaces.Examples
45
{
56
public class ExposedInterfaceReference : MonoBehaviour
67
{
7-
[SerializeField, Expose] public ICustomInterfaceExample customInterfaceExample = null;
8+
[Serializable]
9+
public class CustomInterfaceReference : InterfaceReference<ICustomInterfaceExample>
10+
{
11+
12+
}
13+
14+
15+
[Header("Interface:")]
16+
[Expose] public CustomInterfaceReference customInterfaceReference = new CustomInterfaceReference();
17+
818
}
919
}

0 commit comments

Comments
 (0)