zORg_alex
Apr 8th, 2025 (edited)
25
0
Never
This is comment for paste SceneView PropertyDrawer Editor
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Example with this PropertyDrawer context class
  2.  
  3. using Scripts.Extensions;
  4. using Scripts.Extensions.Serialized;
  5. using System;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. using UnityEditorInternal;
  9. #endif
  10. using UnityEngine;
  11.  
  12. [SelectionBase]
  13. public class Interactable : MonoBehaviour
  14. {
  15.     [field:SerializeField]
  16.     public InteractionPoint Interaction { get; private set; }
  17.  
  18.     private void Start() => Initialize();
  19.     private void OnEnable()
  20.     {
  21.         this.OnAssemblyReload(Initialize);
  22.         TransformUpdated();
  23.     }
  24.  
  25.     private void TransformUpdated()
  26.     {
  27.         Interaction.TransformUpdated(transform);
  28.     }
  29.  
  30.     private void Initialize()
  31.     {
  32.        
  33.     }
  34.  
  35.     [Serializable]
  36.     public class InteractionPoint
  37.     {
  38.         private Matrix4x4 _localToWorld;
  39.         private Matrix4x4 _worldToLocal;
  40.         [SerializeField]
  41.         private Vector3 _position;
  42.         public Vector3 Position
  43.         {
  44.             get => _localToWorld.MultiplyPoint3x4(_position);
  45.             set => _position = _worldToLocal.MultiplyPoint3x4(value);
  46.         }
  47.         [SerializeField]
  48.         private Quaternion _rotation = Quaternion.identity;
  49.         public Quaternion Rotation
  50.         {
  51.             get => _localToWorld.rotation * _rotation;
  52.             set => _rotation = _worldToLocal.rotation * value;
  53.         }
  54.  
  55.         internal void TransformUpdated(Transform transform)
  56.         {
  57.             _localToWorld = transform.localToWorldMatrix;
  58.             _worldToLocal = transform.worldToLocalMatrix;
  59.         }
  60.  
  61. #if UNITY_EDITOR
  62.         [CustomPropertyDrawer(typeof(InteractionPoint))]
  63.         public class InteractionPointInspector : PropertyDrawer
  64.         {
  65.             private Context _ctx;
  66.  
  67.             public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  68.             {
  69.                 var obj = property.serializedObject.targetObject as MonoBehaviour;
  70.                 var val = property.GetValue<InteractionPoint>();
  71.                 if (obj)
  72.                     val?.TransformUpdated(obj.transform);
  73.  
  74.                 position.height = 18;
  75.                 GUI.Label(position, label);
  76.                 var button = new Rect(
  77.                     position.x + EditorGUIUtility.labelWidth, position.y,
  78.                     position.width - EditorGUIUtility.labelWidth, position.height);
  79.                 if (!Context.TrySubscribing(property, out var ctx)) return;
  80.                 ctx.GUIDrawEditButton(button);
  81.  
  82.                 EditorGUI.indentLevel++;
  83.                 position.y += 20;
  84.                 EditorGUI.PropertyField(position, property.FindPropertyRelative(nameof(InteractionPoint._position)));
  85.                 position.y += 20;
  86.                 EditorGUI.PropertyField(position, property.FindPropertyRelative(nameof(InteractionPoint._rotation)));
  87.                 EditorGUI.indentLevel--;
  88.             }
  89.             public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  90.             {
  91.                 return 18 * 3 + 2 * 2;
  92.             }
  93.             private class Context : SceneViewDrawerExtensions.Context<InteractionPoint, Context>
  94.             {
  95.                 private Tool _lastTool;
  96.                 private InteractionPoint _point => _field;
  97.                 protected override void OnSceneGUI()
  98.                 {
  99.                     if (!InternalEditorUtility.GetIsInspectorExpanded(_object))
  100.                         return;
  101.                     if (IsEditing && Tools.current != Tool.None)
  102.                     {
  103.                         _lastTool = Tools.current;
  104.                         Tools.current = Tool.None;
  105.                     }
  106.  
  107.                     DrawEmpty(_point.Position, _point.Rotation);
  108.                     if (IsEditing && _lastTool == Tool.Move)
  109.                     {
  110.                         var newPos = Handles.PositionHandle(_point.Position, _point.Rotation);
  111.                         if (newPos != _point.Position)
  112.                         {
  113.                             Undo.RecordObject(_object, "HandPose MoveTool");
  114.                             _point.Position = newPos;
  115.                         }
  116.                     }
  117.                     else if (IsEditing && _lastTool == Tool.Rotate)
  118.                     {
  119.                         var newRot = Handles.RotationHandle(_point.Rotation, _point.Position);
  120.                         if (newRot != _point.Rotation)
  121.                         {
  122.                             Undo.RecordObject(_object, "HandPose MoveTool");
  123.                             _point.Rotation = newRot;
  124.                         }
  125.                     }
  126.                 }
  127.  
  128.                 protected override void OnStopped()
  129.                 {
  130.                     Tools.current = _lastTool;
  131.                 }
  132.                 private void DrawEmpty(Vector3 pos, Quaternion rot)
  133.                 {
  134.                     var size = HandleUtility.GetHandleSize(pos);
  135.                     Handles.color = Color.red;
  136.                     Handles.DrawAAPolyLine(pos, pos + rot * Vector3.right * size);
  137.                     Handles.color = Color.green;
  138.                     Handles.DrawAAPolyLine(pos, pos + rot * Vector3.up * size);
  139.                     Handles.color = Color.blue;
  140.                     Handles.DrawAAPolyLine(pos, pos + rot * Vector3.forward * size);
  141.                 }
  142.             }
  143.         }
  144. #endif
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment