Skip to content

Commit a86e6ae

Browse files
authored
Merge pull request #1 from gilzoide/gameobjectorcomponent-attribute
Use GameObjectOrComponentAttribute instead of GameObjectOrComponent class
2 parents 53a94f2 + 7dc7b06 commit a86e6ae

11 files changed

+125
-99
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Gilzoide.ConditionalObjects.Editor
7+
{
8+
[CustomPropertyDrawer(typeof(GameObjectOrComponentAttribute))]
9+
[CanEditMultipleObjects]
10+
public class SelectComponentAttributeDrawer : PropertyDrawer
11+
{
12+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
13+
{
14+
if (property.propertyType != SerializedPropertyType.ObjectReference)
15+
{
16+
EditorGUI.PropertyField(position, property, label, true);
17+
return;
18+
}
19+
20+
position.height = EditorGUIUtility.singleLineHeight;
21+
22+
GameObject gameObject = null;
23+
Object targetObject = property.objectReferenceValue;
24+
if (targetObject is Component component)
25+
{
26+
EditorGUI.BeginChangeCheck();
27+
component = (Component) EditorGUI.ObjectField(position, label, component, typeof(Component), true);
28+
if (EditorGUI.EndChangeCheck())
29+
{
30+
targetObject = component;
31+
}
32+
if (component)
33+
{
34+
gameObject = component.gameObject;
35+
}
36+
}
37+
else
38+
{
39+
EditorGUI.BeginChangeCheck();
40+
gameObject = (GameObject) EditorGUI.ObjectField(position, label, targetObject as GameObject, typeof(GameObject), true);
41+
if (EditorGUI.EndChangeCheck())
42+
{
43+
targetObject = gameObject;
44+
}
45+
}
46+
47+
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
48+
EditorGUI.indentLevel++;
49+
property.objectReferenceValue = ShowComponentInput(position, gameObject, targetObject);
50+
EditorGUI.indentLevel--;
51+
}
52+
53+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
54+
{
55+
float height = EditorGUI.GetPropertyHeight(property, label, true);
56+
if (property.propertyType == SerializedPropertyType.ObjectReference)
57+
{
58+
height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
59+
}
60+
return height;
61+
}
62+
63+
private Object ShowComponentInput(Rect position, GameObject gameObject, Object currentSelected)
64+
{
65+
var componentNames = new List<string>();
66+
int selectedIndex = -1;
67+
Component[] allComponents = gameObject.GetAllComponents();
68+
if (gameObject != null)
69+
{
70+
componentNames.Add(nameof(GameObject));
71+
for (int i = 0; i < allComponents.Length; i++)
72+
{
73+
Component component = allComponents[i];
74+
string typeName = component.GetType().Name + " ";
75+
int nameCount = componentNames.Count(name => name.StartsWith(typeName));
76+
if (nameCount > 0)
77+
{
78+
typeName += $"({nameCount})";
79+
}
80+
componentNames.Add(typeName);
81+
82+
if (component == currentSelected)
83+
{
84+
selectedIndex = i;
85+
}
86+
}
87+
}
88+
89+
using (new EditorGUI.DisabledScope(gameObject == null))
90+
{
91+
selectedIndex = EditorGUI.Popup(position, "Component", selectedIndex + 1, componentNames.ToArray()) - 1;
92+
}
93+
return selectedIndex >= 0 ? allComponents[selectedIndex] : gameObject;
94+
}
95+
}
96+
}

Editor/CustomInspectors/GameObjectOrComponentPropertyDrawer.cs.meta renamed to Editor/CustomInspectors/GameObjectOrComponentAttributeDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/CustomInspectors/GameObjectOrComponentPropertyDrawer.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

Editor/CustomInspectors/PropertyVariantPropertyDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2121
EditorGUI.PropertyField(position, target);
2222
position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUI.GetPropertyHeight(target);
2323

24-
Object targetObject = target.GetGameObjectOrComponent();
24+
Object targetObject = target.objectReferenceValue;
2525
using (new EditorGUI.DisabledScope(targetObject == null))
2626
{
2727
(string[] objectPropertyNames, int index) = GetProperties(targetObject, propertyPath.stringValue);
@@ -53,7 +53,7 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
5353
+ EditorGUIUtility.standardVerticalSpacing
5454
+ EditorGUI.GetPropertyHeight(propertyPath);
5555

56-
SerializedProperty referencedProperty = GetReferencedProperty(target.GetGameObjectOrComponent(), propertyPath);
56+
SerializedProperty referencedProperty = GetReferencedProperty(target.objectReferenceValue, propertyPath);
5757
if (referencedProperty != null)
5858
{
5959
height += EditorGUIUtility.standardVerticalSpacing + EditorGUI.GetPropertyHeight(referencedProperty, true);

Runtime/DeleteObjectsModifier.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ public class DeleteObjectsModifier : AImportTimeObjectModifier
77
#if UNITY_EDITOR
88
[Space]
99
[Tooltip("Objects that should be deleted from the imported prefab/scene if the conditions above are met")]
10-
public GameObjectOrComponent[] TargetObjects;
10+
[GameObjectOrComponent] public Object[] TargetObjects;
1111

1212
protected override void Apply(bool filtersMatch)
1313
{
1414
if (filtersMatch)
1515
{
16-
foreach (GameObjectOrComponent target in TargetObjects)
16+
foreach (Object target in TargetObjects)
1717
{
18-
DestroyImmediate(target.Object, true);
18+
if (target is Component || target is GameObject)
19+
{
20+
DestroyImmediate(target, true);
21+
}
1922
}
2023
}
2124
}

Runtime/GameObjectOrComponent.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Gilzoide.ConditionalObjects
5+
{
6+
[AttributeUsage(AttributeTargets.Field)]
7+
public class GameObjectOrComponentAttribute : PropertyAttribute
8+
{
9+
}
10+
}

Runtime/GameObjectOrComponent.cs.meta renamed to Runtime/Internal/GameObjectOrComponentAttribute.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/KeepObjectsModifier.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ public class KeepObjectsModifier : AImportTimeObjectModifier
77
#if UNITY_EDITOR
88
[Space]
99
[Tooltip("Objects that should be kept in the imported prefab/scene only if the conditions above are met")]
10-
public GameObjectOrComponent[] TargetObjects;
10+
[GameObjectOrComponent] public Object[] TargetObjects;
1111

1212
protected override void Apply(bool filtersMatch)
1313
{
1414
if (!filtersMatch)
1515
{
16-
foreach (GameObjectOrComponent target in TargetObjects)
16+
foreach (Object target in TargetObjects)
1717
{
18-
DestroyImmediate(target.Object, true);
18+
if (target is Component || target is GameObject)
19+
{
20+
DestroyImmediate(target, true);
21+
}
1922
}
2023
}
2124
}

Runtime/PropertyVariant.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Gilzoide.ConditionalObjects
88
[Serializable]
99
public class PropertyVariant
1010
{
11-
public GameObjectOrComponent Target;
11+
[GameObjectOrComponent] public Object Target;
1212
public string PropertyPath;
1313

1414
public bool Boolean;
@@ -36,13 +36,12 @@ public class PropertyVariant
3636
#if UNITY_EDITOR
3737
public void Apply()
3838
{
39-
Object targetObject = Target.Object;
40-
if (targetObject == null)
39+
if (Target == null)
4140
{
4241
return;
4342
}
4443

45-
SerializedObject obj = new SerializedObject(targetObject);
44+
SerializedObject obj = new SerializedObject(Target);
4645
SerializedProperty property = obj.FindProperty(PropertyPath);
4746
switch (property.propertyType)
4847
{

0 commit comments

Comments
 (0)