Advertisement
This is comment for paste
SceneView PropertyDrawer Editor
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using RectEx;
- using System;
- using System.Collections.Generic;
- using Unity.Mathematics;
- #if UNITY_EDITOR
- using UnityEditor;
- using UnityEditorInternal;
- #endif
- using UnityEngine;
- namespace Pickuppable
- {
- /// <summary>
- /// Example of using SceneViewDrawerExtensions
- /// Packages/manifest.json:
- /// {
- /// "dependencies": {
- /// "st.rect-ex": "https://github.com/slavniyteo/rect-ex.git#master"
- /// }
- /// Installed Git
- /// </summary>
- public class PickuppableAnimationInterface : MonoBehaviour
- {
- [SerializeField]
- List<AnchorPoint> _anchorPoints;
- [Serializable]
- public class AnchorPoint
- {
- public Vector3 localPosition;
- public Quaternion localRotation = quaternion.identity;
- public Quaternion GetWorldRotation(Transform transform) => transform.rotation * localRotation;
- public void SetWorldRotation(Quaternion rotation, Transform parent)
- {
- localRotation = rotation * Quaternion.Inverse(parent.rotation);
- }
- #if UNITY_EDITOR
- [CustomPropertyDrawer(typeof(AnchorPoint))]
- public class AnchorPointInspector : PropertyDrawer
- {
- private static float[] _weightsWithLabel = new float[] { 0, .15f, 1, .15f, 1, 0 };
- private static float[] _widthesWithLabel = new float[] { EditorGUIUtility.labelWidth, 0, 0, 0, 0, 80 };
- private static float[] _weightsWithNoLabel = new float[] { .15f, 1, .15f, 1, 0 };
- private static float[] _widthesWithNoLabel = new float[] { 0, 0, 0, 0, 80 };
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
- {
- bool hideLabel = label.text.Length == 0;
- var rects = position.Row(weights: hideLabel ? _weightsWithNoLabel : _weightsWithLabel, widthes: hideLabel ? _widthesWithNoLabel : _widthesWithLabel);
- int i = 0;
- if(!hideLabel)
- EditorGUI.LabelField(rects[i++], label);
- EditorGUI.BeginChangeCheck();
- EditorGUI.LabelField(rects[i++], new GUIContent("pos"));
- EditorGUI.PropertyField(rects[i++], property.FindPropertyRelative(nameof(localPosition)), GUIContent.none);
- EditorGUI.LabelField(rects[i++], new GUIContent("rot"));
- EditorGUI.PropertyField(rects[i++], property.FindPropertyRelative(nameof(localRotation)), GUIContent.none);
- if (EditorGUI.EndChangeCheck())
- property.serializedObject.ApplyModifiedProperties();
- AnchorPointContext.TrySubscribing(property, out var ctx);
- ctx.GUIDrawEditButton(rects[i++]);
- }
- private class AnchorPointContext : SceneViewDrawerExtensions.Context<AnchorPoint, AnchorPointContext>
- {
- private AnchorPoint _point => _field;
- private Transform _transform;
- protected override void OnSceneGUI(SceneView view)
- {
- if (!InternalEditorUtility.GetIsInspectorExpanded(_object))
- return;
- if (!_transform) _transform = ((MonoBehaviour)_object).transform;
- Vector3 pos = _transform.TransformPoint(_point.localPosition);
- Quaternion rot = _point.GetWorldRotation(_transform);
- DrawEmpty(pos, rot);
- if (IsEditing && Tools.current == Tool.Move)
- {
- var newPos = Handles.PositionHandle(pos, rot);
- Vector3 newLocalPos = _transform.InverseTransformPoint(newPos);
- if (_point.localPosition == newLocalPos) return;
- Undo.RecordObject(_object, "HandPose MoveTool");
- _point.localPosition = newLocalPos;
- }
- else if (IsEditing && Tools.current == Tool.Rotate)
- {
- var newRot = Handles.RotationHandle(rot, pos);
- var newLocalRot = newRot * Quaternion.Inverse(_transform.rotation);
- if (_point.localRotation == newLocalRot) return;
- Undo.RecordObject(_object, "HandPose MoveTool");
- _point.localRotation = newLocalRot;
- }
- }
- private void DrawEmpty(Vector3 pos, Quaternion rot)
- {
- Handles.color = Color.red;
- Handles.DrawAAPolyLine(pos, pos + rot * Vector3.right);
- Handles.color = Color.green;
- Handles.DrawAAPolyLine(pos, pos + rot * Vector3.up);
- Handles.color = Color.blue;
- Handles.DrawAAPolyLine(pos, pos + rot * Vector3.forward);
- }
- }
- }
- #endif
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement