SHARE
TWEET

[UNITY] Perspective Move Tool

JonRemedios Jul 23rd, 2019 (edited) 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public override void OnToolGUI(EditorWindow window) {
  2.     Event e = Event.current;
  3.  
  4.     switch (e.type) {
  5.         case EventType.KeyDown: {
  6.                 if (e.keyCode == KeyCode.LeftControl) {
  7.                     adjustY = true;
  8.                 }
  9.                 break;
  10.             }
  11.         case EventType.KeyUp:
  12.             if (e.keyCode == KeyCode.LeftControl) {
  13.                 adjustY = false;
  14.             }
  15.             break;
  16.     }
  17.  
  18.     EditorGUI.BeginChangeCheck();
  19.  
  20.     Vector3 worldPos = Tools.handlePosition;
  21.     Vector3 projectedPos = new Vector3(worldPos.x, worldPos.y + worldPos.z);
  22.     Vector3 newPos = projectedPos;
  23.        
  24.     Handles.color = Handles.xAxisColor;
  25.     newPos.x = Handles.Slider(projectedPos, Vector3.right).x;
  26.  
  27.     if (adjustY) {
  28.         Handles.color = Handles.yAxisColor;
  29.         newPos.y = Handles.Slider(projectedPos, Vector3.up).y;
  30.     } else {
  31.         Handles.color = Handles.zAxisColor;
  32.         float y = Handles.Slider(projectedPos, Vector3.up).y;
  33.         float delta = y - newPos.y;
  34.         newPos.z += delta;
  35.     }
  36.  
  37.     worldPos += newPos - projectedPos;
  38.  
  39.     if (EditorGUI.EndChangeCheck()) {
  40.         Vector3 delta = worldPos - Tools.handlePosition;
  41.  
  42.         Undo.RecordObjects(Selection.transforms, "Perspective Move");
  43.  
  44.         foreach (var transform in Selection.transforms) {
  45.             transform.position += delta;
  46.         }
  47.     }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top