Skip to content

Commit 2f52b70

Browse files
committed
Merge branch 'main' into property-via-preset
2 parents 2419ee5 + a86e6ae commit 2f52b70

10 files changed

+127
-97
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/PropertyModifierEditor.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ namespace Gilzoide.ConditionalObjects.Editor
77
[CustomEditor(typeof(PropertyModifier))]
88
public class PropertyModifierEditor : AImportTimeObjectModifierEditor
99
{
10-
private SerializedProperty _gameObjectProperty;
11-
private SerializedProperty _componentIndexProperty;
10+
private SerializedProperty _targetProperty;
1211
private SerializedProperty _presetProperty;
1312
private UnityEditor.Editor _presetEditor;
1413

1514
void OnEnable()
1615
{
17-
_gameObjectProperty = serializedObject.FindProperty($"{nameof(PropertyModifier.Target)}.{nameof(GameObjectOrComponent.GameObject)}");
18-
_componentIndexProperty = serializedObject.FindProperty($"{nameof(PropertyModifier.Target)}.{nameof(GameObjectOrComponent.ComponentIndex)}");
16+
_targetProperty = serializedObject.FindProperty($"{nameof(PropertyModifier.Target)}");
1917
_presetProperty = serializedObject.FindProperty(nameof(PropertyModifier._preset));
2018
}
2119

2220
void OnDisable()
2321
{
24-
_gameObjectProperty.Dispose();
25-
_componentIndexProperty.Dispose();
22+
_targetProperty.Dispose();
2623
DestroyImmediate(_presetEditor);
2724
}
2825

@@ -66,7 +63,7 @@ public override void OnInspectorGUI()
6663

6764
private Object GetTarget()
6865
{
69-
return ((GameObject) _gameObjectProperty.objectReferenceValue).ComponentAtIndexOrSelf(_componentIndexProperty.intValue);
66+
return _targetProperty.objectReferenceValue;
7067
}
7168
}
7269
}

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/Internal/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/Internal/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/PropertyModifier.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ public class PropertyModifier : AImportTimeObjectModifier
88
#if UNITY_EDITOR
99
[Space]
1010
[Tooltip("Target object that will be modified in the imported prefab/scene if the conditions above are met")]
11-
public GameObjectOrComponent Target;
11+
[GameObjectOrComponent] public Object Target;
1212
[SerializeField, ReadOnlyProperty] internal Preset _preset;
1313

1414
protected override void Apply(bool filtersMatch)
1515
{
16-
Object target = Target.Object;
17-
if (filtersMatch && target != null && _preset != null)
16+
if (filtersMatch && (Target is GameObject || Target is Component) && _preset != null)
1817
{
19-
_preset.ApplyTo(target);
18+
_preset.ApplyTo(Target);
2019
}
2120
}
2221
#endif

0 commit comments

Comments
 (0)