Skip to content

Commit 14f3dd0

Browse files
committed
Shared update of NaughtyAttributes
1 parent a793277 commit 14f3dd0

9 files changed

Lines changed: 96 additions & 17 deletions

File tree

Assets/Plugins/NaughtyAttributes/Scripts/Core/DrawerAttributes/ButtonAttribute.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ namespace NaughtyAttributes
55
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
66
public class ButtonAttribute : DrawerAttribute
77
{
8+
public enum ButtonInvocationTarget
9+
{
10+
Default,
11+
First,
12+
All
13+
}
14+
815
public string Text { get; private set; }
916

17+
public ButtonInvocationTarget Target { get; set; }
18+
1019
public ButtonAttribute(string text = null)
1120
{
1221
this.Text = text;

Assets/Plugins/NaughtyAttributes/Scripts/Core/GroupAttributes/BoxGroupAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NaughtyAttributes
44
{
5-
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
5+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
66
public class BoxGroupAttribute : GroupAttribute
77
{
88
public BoxGroupAttribute(string name = "")

Assets/Plugins/NaughtyAttributes/Scripts/Editor/Editors/InspectorEditor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,13 @@ public override void OnInspectorGUI()
185185
MethodDrawer methodDrawer = MethodDrawerDatabase.GetDrawerForAttribute(drawerAttribute.GetType());
186186
if (methodDrawer != null)
187187
{
188-
methodDrawer.DrawMethod(this.target, method);
188+
189+
if (methodDrawer.DrawMethod(this.target, method))
190+
{
191+
this.serializedObject.ApplyModifiedProperties();
192+
this.serializedObject.Update();
193+
PropertyDrawerDatabase.ClearCache();
194+
}
189195
}
190196
}
191197
}

Assets/Plugins/NaughtyAttributes/Scripts/Editor/MethodDrawers/ButtonMethodDrawer.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,56 @@ namespace NaughtyAttributes.Editor
77
[MethodDrawer(typeof(ButtonAttribute))]
88
public class ButtonMethodDrawer : MethodDrawer
99
{
10-
public override void DrawMethod(UnityEngine.Object target, MethodInfo methodInfo)
10+
public override bool DrawMethod(UnityEngine.Object target, MethodInfo methodInfo)
1111
{
1212
if (methodInfo.GetParameters().Length == 0)
1313
{
14-
ButtonAttribute buttonAttribute = (ButtonAttribute)methodInfo.GetCustomAttributes(typeof(ButtonAttribute), true)[0];
14+
ButtonAttribute buttonAttribute = (ButtonAttribute) methodInfo.GetCustomAttributes(typeof(ButtonAttribute), true)[0];
1515
string buttonText = string.IsNullOrEmpty(buttonAttribute.Text) ? methodInfo.Name : buttonAttribute.Text;
1616

1717
if (GUILayout.Button(buttonText))
1818
{
19-
methodInfo.Invoke(target, null);
19+
EditorGUI.BeginChangeCheck();
20+
21+
var gameObjects = Selection.gameObjects;
22+
if (gameObjects.Length <= 1 || buttonAttribute.Target == ButtonAttribute.ButtonInvocationTarget.Default)
23+
{
24+
methodInfo.Invoke(target, null);
25+
}
26+
else if (target is Component)
27+
{
28+
var targetType = target.GetType();
29+
if (buttonAttribute.Target == ButtonAttribute.ButtonInvocationTarget.First)
30+
{
31+
var gameObject = Selection.gameObjects[0];
32+
var targetComponent = gameObject.GetComponent(targetType);
33+
methodInfo.Invoke(targetComponent, null);
34+
}
35+
else if (buttonAttribute.Target == ButtonAttribute.ButtonInvocationTarget.All)
36+
{
37+
foreach (var gameObject in Selection.gameObjects)
38+
{
39+
var targetComponent = gameObject.GetComponent(targetType);
40+
methodInfo.Invoke(targetComponent, null);
41+
}
42+
}
43+
}
44+
45+
if (EditorGUI.EndChangeCheck())
46+
{
47+
Undo.RecordObject(target, "Method Invocation: " + buttonText);
48+
}
49+
50+
return true;
2051
}
2152
}
2253
else
2354
{
2455
string warning = typeof(ButtonAttribute).Name + " works only on methods with no parameters";
2556
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
2657
}
58+
59+
return false;
2760
}
2861
}
2962
}

Assets/Plugins/NaughtyAttributes/Scripts/Editor/MethodDrawers/MethodDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace NaughtyAttributes.Editor
44
{
55
public abstract class MethodDrawer
66
{
7-
public abstract void DrawMethod(UnityEngine.Object target, MethodInfo methodInfo);
7+
public abstract bool DrawMethod(UnityEngine.Object target, MethodInfo methodInfo);
88
}
99
}

Assets/Plugins/NaughtyAttributes/Scripts/Editor/NativePropertyDrawers/ShowNativePropertyNativePropertyDrawer.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Reflection;
23
using UnityEditor;
34

@@ -8,12 +9,23 @@ public class ShowNativePropertyNativePropertyDrawer : NativePropertyDrawer
89
{
910
public override void DrawNativeProperty(UnityEngine.Object target, PropertyInfo property)
1011
{
11-
object value = property.GetValue(target, null);
12+
try
13+
{
14+
15+
object value = property.GetValue(target, null);
1216

13-
if (!EditorDrawUtility.DrawLayoutField(value, ObjectNames.NicifyVariableName(property.Name), property.PropertyType))
17+
if (!EditorDrawUtility.DrawLayoutField(value, ObjectNames.NicifyVariableName(property.Name),
18+
property.PropertyType))
19+
{
20+
string warning = string.Format("{0} doesn't support {1} types",
21+
typeof(ShowNativePropertyNativePropertyDrawer).Name,
22+
property.PropertyType.Name);
23+
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
24+
}
25+
}
26+
catch(Exception e)
1427
{
15-
string warning = string.Format("{0} doesn't support {1} types", typeof(ShowNativePropertyNativePropertyDrawer).Name, property.PropertyType.Name);
16-
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
28+
EditorDrawUtility.DrawHelpBox("Error: " + e, MessageType.Error, logToConsole: true, context: target);
1729
}
1830
}
1931
}

Assets/Plugins/NaughtyAttributes/Scripts/Editor/PropertyDrawers/MinMaxSliderPropertyDrawer.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public override void DrawProperty(SerializedProperty property)
1212

1313
MinMaxSliderAttribute minMaxSliderAttribute = PropertyUtility.GetAttribute<MinMaxSliderAttribute>(property);
1414

15-
if (property.propertyType == SerializedPropertyType.Vector2)
15+
if (property.propertyType == SerializedPropertyType.Vector2 ||
16+
property.propertyType == SerializedPropertyType.Vector2Int)
1617
{
1718
Rect controlRect = EditorGUILayout.GetControlRect();
1819
float labelWidth = EditorGUIUtility.labelWidth;
@@ -50,7 +51,16 @@ public override void DrawProperty(SerializedProperty property)
5051
// Draw the slider
5152
EditorGUI.BeginChangeCheck();
5253

53-
Vector2 sliderValue = property.vector2Value;
54+
Vector2 sliderValue;
55+
if (property.propertyType == SerializedPropertyType.Vector2)
56+
{
57+
sliderValue = property.vector2Value;
58+
}
59+
else
60+
{
61+
sliderValue = property.vector2IntValue;
62+
}
63+
5464
EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue);
5565

5666
sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x);
@@ -61,7 +71,14 @@ public override void DrawProperty(SerializedProperty property)
6171

6272
if (EditorGUI.EndChangeCheck())
6373
{
64-
property.vector2Value = sliderValue;
74+
if (property.propertyType == SerializedPropertyType.Vector2)
75+
{
76+
property.vector2Value = sliderValue;
77+
}
78+
else
79+
{
80+
property.vector2IntValue = new Vector2Int((int)sliderValue.x, (int) sliderValue.y);
81+
}
6582
}
6683
}
6784
else

Assets/Plugins/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ReorderableListPropertyDrawer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@ public class ReorderableListPropertyDrawer : PropertyDrawer
1212
struct PropertyKey : IEquatable<PropertyKey>
1313
{
1414
readonly string fieldName;
15-
readonly int instanceId;
1615
readonly object sourceObject;
1716

1817
public PropertyKey(SerializedProperty property)
1918
{
2019
fieldName = property.name;
21-
instanceId = property.objectReferenceInstanceIDValue;
2220
sourceObject = property.serializedObject;
2321
}
2422

2523
public bool Equals(PropertyKey other)
2624
{
27-
return string.Equals(fieldName, other.fieldName) && instanceId == other.instanceId && Equals(sourceObject, other.sourceObject);
25+
return string.Equals(fieldName, other.fieldName) &&
26+
Equals(sourceObject, other.sourceObject);
2827
}
2928

3029
public override bool Equals(object obj)
@@ -42,7 +41,6 @@ public override int GetHashCode()
4241
unchecked
4342
{
4443
var hashCode = (fieldName != null ? fieldName.GetHashCode() : 0);
45-
hashCode = (hashCode * 397) ^ instanceId;
4644
hashCode = (hashCode * 397) ^ (sourceObject != null ? sourceObject.GetHashCode() : 0);
4745
return hashCode;
4846
}

Assets/Plugins/NaughtyAttributes/Scripts/Editor/Utility/EditorDrawUtility.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public static bool DrawLayoutField(object value, string label, Type targetType)
127127
{
128128
EditorGUILayout.ObjectField(label, (UnityEngine.Object)value, valueType, true);
129129
}
130+
else
131+
{
132+
EditorGUILayout.TextField(label, $"{value}");
133+
}
130134
}
131135
else if (typeof(UnityEngine.Object).IsAssignableFrom(valueType))
132136
{

0 commit comments

Comments
 (0)