diff --git a/Editor/EditorAddons/BetterAttributesEditor.cs.meta b/Editor/EditorAddons/BetterAttributesEditor.cs.meta deleted file mode 100644 index 374a57e..0000000 --- a/Editor/EditorAddons/BetterAttributesEditor.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 20f290ac9e7e12c47bbd79e0db963b19 -timeCreated: 1656879186 \ No newline at end of file diff --git a/Editor/EditorAddons/CustomEditors.meta b/Editor/EditorAddons/CustomEditors.meta new file mode 100644 index 0000000..6098b09 --- /dev/null +++ b/Editor/EditorAddons/CustomEditors.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c11fea05982447bb840a231134b6b14f +timeCreated: 1682216202 \ No newline at end of file diff --git a/Editor/EditorAddons/BetterAttributesEditor.cs b/Editor/EditorAddons/CustomEditors/BetterButtonsEditor.cs similarity index 75% rename from Editor/EditorAddons/BetterAttributesEditor.cs rename to Editor/EditorAddons/CustomEditors/BetterButtonsEditor.cs index 5de9dbd..9a9014f 100644 --- a/Editor/EditorAddons/BetterAttributesEditor.cs +++ b/Editor/EditorAddons/CustomEditors/BetterButtonsEditor.cs @@ -2,36 +2,41 @@ using System.Linq; using System.Reflection; using Better.Attributes.Runtime; +using Better.EditorTools.CustomEditors; using Better.Extensions.Runtime; using UnityEditor; using UnityEngine; +using Object = UnityEngine.Object; -namespace Better.Attributes.EditorAddons +namespace Better.Attributes.EditorAddons.CustomEditors { - [CanEditMultipleObjects] - [CustomEditor(typeof(Object), true)] - public class BetterAttributesEditor : Editor + [BetterEditor(typeof(Object), true, Order = 999)] + public class BetterButtonsEditor : EditorExtension { private Dictionary>> _methodButtonsAttributes = new Dictionary>>(); + + public BetterButtonsEditor(Object target, SerializedObject serializedObject) : base(target, serializedObject) + { + } - protected Object _bufferTarget; + public override void OnDisable() + { + } - protected virtual void OnEnable() + public override void OnEnable() { - _bufferTarget = target; - var type = _bufferTarget.GetType(); + var type = _target.GetType(); _methodButtonsAttributes = type.GetSortedMethodAttributes(); } - private void DrawButton(KeyValuePair button, - GUIStyle guiStyle) + private void DrawButton(KeyValuePair button, GUIStyle guiStyle) { var attribute = button.Value; var methodInfo = button.Key; if (GUILayout.Button(attribute.GetDisplayName(methodInfo.PrettyMemberName()), guiStyle)) - methodInfo.Invoke(_bufferTarget, attribute.InvokeParams); + methodInfo.Invoke(_target, attribute.InvokeParams); } private void DrawButtons(Dictionary>> buttons) @@ -76,10 +81,14 @@ private void DrawButtons(Dictionary(); + if (data == null) + { + continue; + } + + if (data.Count <= 0) continue; + attributeFound = true; + break; + } + + return attributeFound; + } + + public override void OnInspectorGUI() + { + if (_hideTransformDrawer != null) + { + _hideTransformDrawer.DrawHideTransformButton(); + } + } + + public override void OnChanged() + { + var attributeFound = AttributeFound(); + _hideTransformDrawer = attributeFound ? new HideTransformButtonUtility() : null; + } + } +} \ No newline at end of file diff --git a/Editor/EditorAddons/CustomEditors/GizmosEditor.cs.meta b/Editor/EditorAddons/CustomEditors/GizmosEditor.cs.meta new file mode 100644 index 0000000..d1bb368 --- /dev/null +++ b/Editor/EditorAddons/CustomEditors/GizmosEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 60771ff3ebf349c2b83d7c4ec8066d93 +timeCreated: 1682216447 \ No newline at end of file diff --git a/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs index d52efff..f07cb2f 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using Better.Attributes.EditorAddons.Drawers.Utilities; using Better.EditorTools.Utilities; using UnityEditor; @@ -12,25 +13,21 @@ namespace Better.Attributes.EditorAddons.Drawers.Gizmo public abstract class GizmoWrapper : UtilityWrapper { private protected SerializedProperty _serializedProperty; - private protected Type _objectType; private protected readonly Quaternion _defaultRotation = Quaternion.identity; private protected readonly Vector3 _defaultPosition = Vector3.zero; private bool _showInSceneView = true; private Type _fieldType; + private string _path; + public bool ShowInSceneView => _showInSceneView; public virtual void SetProperty(SerializedProperty property, Type fieldType) { - _fieldType = fieldType; _serializedProperty = property; - _objectType = _serializedProperty.serializedObject.targetObject.GetType(); - } - - public override void Deconstruct() - { - GizmoUtility.Instance.RemoveButtonDrawn(_objectType); + _path = _serializedProperty.propertyPath; + _fieldType = fieldType; } public void SwitchShowMode() @@ -40,13 +37,22 @@ public void SwitchShowMode() public abstract void Apply(SceneView sceneView); - private protected virtual bool ValidateSerializedObject() + public override void Deconstruct() { - var serializedObject = _serializedProperty?.serializedObject; + _serializedProperty = null; + } - if (serializedObject == null) return false; + public virtual bool ValidateSerializedObject() + { try { + if (_serializedProperty == null) return false; + var serializedObject = _serializedProperty.serializedObject; + + if (!string.Equals(_serializedProperty.propertyPath, _path)) return false; + if (serializedObject == null) return false; + if (!_serializedProperty.Copy().Next(true)) return false; + if (!GizmoUtility.Instance.IsSupported(_fieldType)) return false; return serializedObject.targetObject != null; } diff --git a/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs b/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs index 06649da..6b809ec 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs @@ -21,7 +21,11 @@ private GizmoWrappers Collection { get { - _wrappers ??= GenerateCollection(); + if (_wrappers == null) + { + _wrappers = GenerateCollection(); + } + return _wrappers as GizmoWrappers; } } @@ -37,6 +41,10 @@ private void OnSceneGUIDelegate(SceneView sceneView) { if (sceneView.drawGizmos) { + if (_wrappers == null) + { + _wrappers = GenerateCollection(); + } GizmoUtility.Instance.ValidateCachedProperties(_wrappers); Collection?.Apply(sceneView); } @@ -53,11 +61,6 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property, var fieldType = fieldInfo.FieldType; var attributeType = attribute.GetType(); - if (_hideTransformDrawer == null && property.IsTargetComponent(out _)) - { - _hideTransformDrawer = new HideTransformButtonUtility(property, GizmoUtility.Instance); - } - if (!GizmoUtility.Instance.IsSupported(fieldType)) { EditorGUI.BeginChangeCheck(); @@ -73,11 +76,6 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property, EditorGUI.BeginChangeCheck(); - if (_hideTransformDrawer != null) - { - _hideTransformDrawer.DrawHideTransformButton(); - } - return true; } diff --git a/Editor/EditorAddons/Drawers/Gizmo/HideTransformDrawer.cs b/Editor/EditorAddons/Drawers/Gizmo/HideTransformDrawer.cs index 332919c..0f215f9 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/HideTransformDrawer.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/HideTransformDrawer.cs @@ -1,38 +1,13 @@ using Better.EditorTools; using UnityEditor; using UnityEngine; -#if UNITY_2022_1_OR_NEWER -using GizmoUtility = Better.Attributes.EditorAddons.Drawers.Utilities.GizmoUtility; -#else -using Better.Attributes.EditorAddons.Drawers.Utilities; -#endif namespace Better.Attributes.EditorAddons.Drawers.Gizmo { public class HideTransformButtonUtility { - private bool _isChecked = false; - private bool _isButtonDrawn; - private readonly SerializedProperty _serializedProperty; - private readonly GizmoUtility _gizmoDrawerUtility; - - public HideTransformButtonUtility(SerializedProperty property, GizmoUtility gizmoDrawerUtility) - { - _serializedProperty = property; - _gizmoDrawerUtility = gizmoDrawerUtility; - } - public void DrawHideTransformButton() { - if (!_serializedProperty.IsTargetComponent(out var component)) return; - var type = component.GetType(); - if (!_isChecked) - { - _isButtonDrawn = _gizmoDrawerUtility.IsButtonDrawn(type); - _isChecked = true; - } - - if (_isButtonDrawn) return; var text = Tools.hidden ? "Show" : "Hide"; if (GUILayout.Button($"{text} Transform handles")) { diff --git a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/BoundsLocalWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/BoundsLocalWrapper.cs index 0d229d0..bb3c082 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/BoundsLocalWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/BoundsLocalWrapper.cs @@ -9,7 +9,6 @@ public class BoundsLocalWrapper : BoundsBaseWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; if (_serializedProperty.IsTargetComponent(out var component)) { var transform = component.transform; diff --git a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs index fbb348d..008fd75 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs @@ -12,7 +12,6 @@ public class QuaternionLocalWrapper : GizmoWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; if (_serializedProperty.IsTargetComponent(out var component)) { var transform = component.transform; diff --git a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs index 3026afe..ccf7b37 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs @@ -12,7 +12,6 @@ public class Vector2LocalWrapper : GizmoWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; if (_serializedProperty.IsTargetComponent(out var component)) { var transform = component.transform; diff --git a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs index a7ffa55..0e50a74 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs @@ -12,7 +12,6 @@ public class Vector3LocalWrapper : GizmoWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; if (_serializedProperty.IsTargetComponent(out var component)) { var transform = component.transform; diff --git a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/BoundsWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/BoundsWrapper.cs index a73c29d..18def9d 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/BoundsWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/BoundsWrapper.cs @@ -7,7 +7,6 @@ public class BoundsWrapper : BoundsBaseWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; DrawLabel($"{_serializedProperty.name}:\nCenter: {_bounds.center}\nSize: {_bounds.size}", _bounds.center, _defaultRotation, sceneView); _bounds.center = Handles.PositionHandle(_bounds.center, _defaultRotation); diff --git a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs index b94a70c..f2f0a09 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs @@ -11,7 +11,6 @@ public class QuaternionWrapper : GizmoWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; DrawLabel($"{_serializedProperty.name}:\n{_quaternion.eulerAngles}", _defaultPosition, _quaternion, sceneView); _quaternion = Handles.RotationHandle(_quaternion, _defaultPosition); diff --git a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs index 67adf56..6ca0b0f 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs @@ -11,7 +11,6 @@ public class Vector2Wrapper : GizmoWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; DrawLabel($"{_serializedProperty.name}:\n{_vector2}", _vector2, _defaultRotation, sceneView); _vector2 = Handles.PositionHandle(_vector2, _defaultRotation); diff --git a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs index 639722b..916fd30 100644 --- a/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs +++ b/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs @@ -11,10 +11,8 @@ public class Vector3Wrapper : GizmoWrapper public override void Apply(SceneView sceneView) { if (!ShowInSceneView) return; - if (!ValidateSerializedObject()) return; DrawLabel($"{_serializedProperty.name}:\n{_vector3}", _vector3, _defaultRotation, sceneView); _vector3 = Handles.PositionHandle(_vector3, _defaultRotation); - _serializedProperty.vector3Value = _vector3; SetValueAndApply(_vector3); } diff --git a/Editor/EditorAddons/Drawers/Utilities/GizmoUtility.cs b/Editor/EditorAddons/Drawers/Utilities/GizmoUtility.cs index dd6680d..ba65edc 100644 --- a/Editor/EditorAddons/Drawers/Utilities/GizmoUtility.cs +++ b/Editor/EditorAddons/Drawers/Utilities/GizmoUtility.cs @@ -11,42 +11,6 @@ namespace Better.Attributes.EditorAddons.Drawers.Utilities { public class GizmoUtility : BaseUtility { - private readonly Dictionary hideTransformRegistered = new Dictionary(); - - [DidReloadScripts] - private static void OnReloadScripts() - { - Instance.hideTransformRegistered.Clear(); - } - - public bool IsButtonDrawn(Type type) - { - if (hideTransformRegistered.TryGetValue(type, out var count)) - { - count++; - hideTransformRegistered[type] = count; - return true; - } - - hideTransformRegistered.Add(type, 1); - return false; - } - - public void RemoveButtonDrawn(Type type) - { - if (hideTransformRegistered.TryGetValue(type, out var count)) - { - count--; - if (count <= 1) - { - hideTransformRegistered.Remove(type); - return; - } - - hideTransformRegistered[type] = count; - } - } - protected override WrappersTypeCollection GenerateCollection() { return new WrappersTypeCollection() diff --git a/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs b/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs index bf9a43d..1083958 100644 --- a/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs +++ b/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Better.Attributes.EditorAddons.Drawers.Gizmo; using Better.EditorTools.Drawers.Base; using UnityEditor; @@ -9,9 +10,30 @@ public class GizmoWrappers : WrapperCollection { public void Apply(SceneView sceneView) { - foreach (var gizmo in Values) + List keysToRemove = null; + foreach (var gizmo in this) { - gizmo.Wrapper.Apply(sceneView); + var valueWrapper = gizmo.Value.Wrapper; + if(valueWrapper.ValidateSerializedObject()) + { + valueWrapper.Apply(sceneView); + } + else + { + if (keysToRemove == null) + { + keysToRemove = new List(); + } + keysToRemove.Add(gizmo.Key); + } + } + + if (keysToRemove != null) + { + foreach (var property in keysToRemove) + { + Remove(property); + } } } diff --git a/package.json b/package.json index f6af4d3..1895bcf 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "com.uurha.betterattributes", "displayName": "Better Attributes", - "version": "1.6.6", + "version": "1.7.3", "unity": "2018.3", "description": "Unity attributes, allows to serialize interfaces, draw handles for Vector3/Vector2/Quaternion/Bounds, create read only fields.", "dependencies": { - "com.uurha.bettereditortools" : "0.3.5" + "com.uurha.bettereditortools" : "0.4.9" }, "author": { "name": "Arcueid D'athemon",