diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs index f173c8b..1dafadc 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs @@ -3,6 +3,7 @@ using System.Text.RegularExpressions; using Better.Attributes.EditorAddons.Drawers.Utilities; using Better.EditorTools; +using Better.EditorTools.Drawers.Base; using Better.EditorTools.Utilities; using UnityEditor; using UnityEngine; @@ -88,21 +89,23 @@ public virtual bool Validate() } } - private protected void SetValueAndApply(T value) where T : struct + private protected void SetValueAndApply(object value) { if (!Validate()) { return; } - if (_fieldType.IsEquivalentTo(typeof(T))) - { - _serializedProperty.SetValueNoRecord(value); - } + if (_fieldType.IsEquivalentTo(typeof(Vector2))) + _serializedProperty.vector2Value = (Vector2)value; + else if (_fieldType.IsEquivalentTo(typeof(Vector3))) + _serializedProperty.vector3Value = (Vector3)value; + else if (_fieldType.IsEquivalentTo(typeof(Bounds))) + _serializedProperty.boundsValue = (Bounds)value; + else if (_fieldType.IsEquivalentTo(typeof(Quaternion))) + _serializedProperty.quaternionValue = (Quaternion)value; else - { throw new ArgumentOutOfRangeException(); - } _serializedProperty.serializedObject.ApplyModifiedProperties(); } @@ -128,5 +131,10 @@ private protected virtual Vector3 GetPosition(Vector3 position, Quaternion rotat return rotation * (position + Vector3.up * HandleUtility.GetHandleSize(position) + sceneView.camera.transform.right * 0.2f * HandleUtility.GetHandleSize(position)); } + + public virtual HeightCache GetHeight(GUIContent label) + { + return HeightCache.GetAdditive(0f); + } } } \ No newline at end of file diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs index 5ff876e..a30de0d 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs @@ -99,13 +99,14 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property, protected override void DrawField(Rect position, SerializedProperty property, GUIContent label) { - var cache = ValidateCachedProperties(property, GizmoUtility.Instance); - if (!cache.IsValid) + if (!Collection.IsValid(property)) { + var cache = ValidateCachedProperties(property, GizmoUtility.Instance); + var fieldType = GetFieldOrElementType(); - Collection.SetProperty(property, fieldType); + cache.Value.Wrapper.SetProperty(property, fieldType); } - + Collection.DrawField(position, property, label); } @@ -154,7 +155,7 @@ protected override HeightCache GetPropertyHeight(SerializedProperty property, GU return HeightCache.GetAdditive(additive + DrawersHelper.SpaceHeight * 2); } - return HeightCache.GetAdditive(0); + return Collection.GetHeight(property, label); } } } \ No newline at end of file diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs index 017f471..1750b20 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/QuaternionLocalWrapper.cs @@ -1,5 +1,6 @@ using System; using Better.EditorTools; +using Better.EditorTools.Drawers.Base; using Better.Extensions.Runtime.MathfExtensions; using UnityEditor; using UnityEngine; @@ -36,17 +37,26 @@ public override void Apply(SceneView sceneView) public override void DrawField(Rect position, GUIContent label) { - using (var change = new EditorGUI.ChangeCheckScope()) + if (_serializedProperty.IsTargetComponent(out var component)) { - var eulerRotation = EditorGUI.Vector3Field(position, label, _quaternion.eulerAngles); - _quaternion = Quaternion.Euler(eulerRotation); - if (change.changed) + using (var change = new EditorGUI.ChangeCheckScope()) { - SetValueAndApply(_quaternion); + var eulerRotation = EditorGUI.Vector3Field(position, label, _quaternion.eulerAngles); + + if (change.changed) + { + _quaternion = Quaternion.Euler(eulerRotation); + SetValueAndApply(_quaternion); + } } } } + public override HeightCache GetHeight(GUIContent label) + { + return HeightCache.GetFull(EditorGUIUtility.singleLineHeight); + } + public override void SetProperty(SerializedProperty property, Type fieldType) { _quaternion = property.quaternionValue; diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs index bd9069f..b5b7636 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector2LocalWrapper.cs @@ -22,6 +22,7 @@ public override void Apply(SceneView sceneView) if (!Vector3Math.Approximately(_vector2, (Vector2)buffer)) { + _vector2 = buffer; SetValueAndApply(_vector2); } } diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs index f410886..c0e8a99 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/LocalWrappers/Vector3LocalWrapper.cs @@ -22,6 +22,7 @@ public override void Apply(SceneView sceneView) if (!Vector3Math.Approximately(_vector3, buffer)) { + _vector3 = buffer; SetValueAndApply(_vector3); } } diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs index 222f727..5c6e93b 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/QuaternionWrapper.cs @@ -1,4 +1,5 @@ using System; +using Better.EditorTools.Drawers.Base; using Better.Extensions.Runtime.MathfExtensions; using UnityEditor; using UnityEngine; @@ -22,23 +23,29 @@ public override void Apply(SceneView sceneView) if (!Vector3Math.Approximately(_quaternion, buffer)) { + _quaternion = buffer; SetValueAndApply(_quaternion); } } - + public override void DrawField(Rect position, GUIContent label) { using (var change = new EditorGUI.ChangeCheckScope()) { var eulerRotation = EditorGUI.Vector3Field(position, label, _quaternion.eulerAngles); - _quaternion = Quaternion.Euler(eulerRotation); if (change.changed) { + _quaternion = Quaternion.Euler(eulerRotation); SetValueAndApply(_quaternion); } } } + public override HeightCache GetHeight(GUIContent label) + { + return HeightCache.GetFull(EditorGUIUtility.singleLineHeight); + } + public override void SetProperty(SerializedProperty property, Type fieldType) { _quaternion = property.quaternionValue; diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs index 62dd5f0..a771409 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector2Wrapper.cs @@ -17,6 +17,7 @@ public override void Apply(SceneView sceneView) if (!Vector3Math.Approximately(_vector2, (Vector2)buffer)) { + _vector2 = buffer; SetValueAndApply(_vector2); } } diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs index 6b1813c..1d64cf5 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/Gizmo/WorldWrappers/Vector3Wrapper.cs @@ -17,6 +17,7 @@ public override void Apply(SceneView sceneView) if (!Vector3Math.Approximately(_vector3, buffer)) { + _vector3 = buffer; SetValueAndApply(_vector3); } } diff --git a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs index b839d15..f25ecd0 100644 --- a/Assets/BetterAttributes/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs +++ b/Assets/BetterAttributes/Editor/EditorAddons/Drawers/WrapperCollections/GizmoWrappers.cs @@ -15,7 +15,7 @@ public void Apply(SceneView sceneView) foreach (var gizmo in this) { var valueWrapper = gizmo.Value.Wrapper; - if(valueWrapper.Validate()) + if (valueWrapper.Validate()) { valueWrapper.Apply(sceneView); } @@ -25,6 +25,7 @@ public void Apply(SceneView sceneView) { keysToRemove = new List(); } + keysToRemove.Add(gizmo.Key); } } @@ -71,5 +72,25 @@ public void DrawField(Rect position, SerializedProperty property, GUIContent lab gizmoWrapper.Wrapper.DrawField(position, label); } } + + public bool IsValid(SerializedProperty property) + { + if (TryGetValue(property, out var gizmoWrapper)) + { + return gizmoWrapper.Wrapper.Validate(); + } + + return false; + } + + public HeightCache GetHeight(SerializedProperty property, GUIContent label) + { + if (TryGetValue(property, out var wrapper)) + { + return wrapper.Wrapper.GetHeight(label); + } + + return HeightCache.GetAdditive(0); + } } } \ No newline at end of file diff --git a/Assets/BetterAttributes/Samples~/TestSamples/New Prefab.prefab b/Assets/BetterAttributes/Samples~/TestSamples/New Prefab.prefab index 1cc7e3f..8dd6788 100644 --- a/Assets/BetterAttributes/Samples~/TestSamples/New Prefab.prefab +++ b/Assets/BetterAttributes/Samples~/TestSamples/New Prefab.prefab @@ -445,8 +445,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2489651872190957466} serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalRotation: {x: 0.3576463, y: -0.6084306, z: -0.6162748, w: -0.3494378} + m_LocalPosition: {x: 1.806, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: @@ -455,7 +455,7 @@ Transform: - {fileID: 7922880496053330287} - {fileID: 758694063010868647} m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 270.92, y: 643.7, z: 196.3} --- !u!114 &-745862845828164141 MonoBehaviour: m_ObjectHideFlags: 0 @@ -470,4 +470,4 @@ MonoBehaviour: m_EditorClassIdentifier: sprite: {fileID: 21300000, guid: 78b772f3f305147419de3cc97ca72bd7, type: 3} pos: {x: 0, y: 1.1718663, z: 1.3363248} - rot: {x: -0.068464614, y: 0.018447453, z: 0.0024131201, w: 0.99748003} + rot: {x: 0.000000019045045, y: 0.5995843, z: -0.000000026187257, w: 0.80031174} diff --git a/Assets/BetterAttributes/Samples~/TestSamples/Scripts/PreviewTest.cs b/Assets/BetterAttributes/Samples~/TestSamples/Scripts/PreviewTest.cs index d26b5a4..211cfa7 100644 --- a/Assets/BetterAttributes/Samples~/TestSamples/Scripts/PreviewTest.cs +++ b/Assets/BetterAttributes/Samples~/TestSamples/Scripts/PreviewTest.cs @@ -7,7 +7,7 @@ public class PreviewTest : MonoBehaviour { [SerializeField] private Sprite sprite; [Gizmo] [SerializeField] private Vector3 pos; - [Gizmo] [SerializeField] private Quaternion rot; + [GizmoLocal] [SerializeField] private Quaternion rot; } } \ No newline at end of file diff --git a/Assets/BetterAttributes/package.json b/Assets/BetterAttributes/package.json index 5b72f18..533f2a9 100644 --- a/Assets/BetterAttributes/package.json +++ b/Assets/BetterAttributes/package.json @@ -1,7 +1,7 @@ { "name": "com.uurha.betterattributes", "displayName": "Better Attributes", - "version": "2.1.42", + "version": "2.1.44", "unity": "2020.1", "description": "Unity attributes, allows to serialize interfaces, draw handles for Vector3/Vector2/Quaternion/Bounds, create read only fields.", "dependencies": {