title: How to get screen click input from anywhere with custom editor attribute?
body: <p><strong>TL;DR:</strong> How do I implement Unity's 'color from screen' functionality but with vectors?</p>
  
  <p><a href="https://i.stack.imgur.com/VWdEf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VWdEf.png" alt="Image of Unity&#39;s color picker"></a></p>
  
  <hr>
  
  
  
  <p>Ok so title is pretty simplified for what I'm trying to do:</p>
  
  <ol>
  <li><p>Have the user click a button, then click a position on the screen to have that [world] position be saved as the vector. - This is mostly working, except it won't detect left clicks outside of the inspector.</p></li>
  <li><p>Disable left click for everything else on the unity editor (so when you click a position it doesn't change focus to another GameObject). - This is the main problem.</p></li>
  </ol>
  
  <p>Tracking the mouse and getting the world position was pretty easy, it's just a bool to save if the mouse is being tracked and a SerializedProperty to save which value the mouse position is being saved to.</p>
  
  <p>Here's what my attribute looks like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class VectorPickerAttribute : PropertyAttribute {
      readonly bool relative;
  
      /// &lt;summary&gt;
      /// Works a lot like the color picker, except for vectors.
      /// &lt;/summary&gt;
      /// &lt;param name="relative"&gt;Make the final vector relative to the transform?&lt;/param&gt;
      public VectorPickerAttribute(bool relative = false) {
          this.relative = relative;
      }
  }
  </code></pre>
  
  <p>Here is the PropertyDrawer:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[CustomPropertyDrawer(typeof(VectorPickerAttribute))]
  public class VectorPickerDrawer : PropertyDrawer {
      bool trackMouse = false;
      SerializedProperty v;
  
      public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
          if(property.propertyType == SerializedPropertyType.Vector2) {
              Rect button = new Rect(position);
              button.x = position.width - 2;
              button.width = position.height;
              bool pressed = GUI.Button(button, "");
              if(pressed) {
                  trackMouse = true;
                  v = property;
              }
              else if(Input.GetMouseButtonDown(0)) trackMouse = false;
  
              bool tracking = trackMouse &amp;&amp; v.propertyPath == property.propertyPath;
              if(tracking) {
                  property.vector2Value =
                      Camera.main.ScreenToWorldPoint(
                          GUIUtility.GUIToScreenPoint(
                              Event.current.mousePosition
                  ));
              }
  
              GUI.enabled = !tracking;
              EditorGUI.Vector2Field(position, label.text, property.vector2Value);
              GUI.enabled = true;
  
              EditorUtility.SetDirty(property.serializedObject.targetObject);
          }
      }
  }
  </code></pre>
  
  <p>And here's what it does so far:</p>
  
  <p><a href="https://i.stack.imgur.com/B0oUS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B0oUS.png" alt="Image of VectorPickerAttribute in action."></a></p>
  
  <p>You click the button on the right, and it will update the vector to the mouse position until it detects a left click with <code>Input.GetMouseButtonDown(0)</code>.</p>
  
  <p>Problems with this:</p>
  
  <ul>
  <li><p>It will only detect a click when it's actually on the inspector window.</p></li>
  <li><p>When you click outside the inspector window it will either not change anything or it will select something else so it will close the inspector (but since it saves the mouse position every <code>OnGUI()</code> that point where you clicked will be saved to the vector, so I guess it works??).</p></li>
  </ul>
  
  <p>I've tried covering the screen with a blank window, but I couldn't get <code>GUI.Window</code> or <code>GUI.ModalWindow</code> to do anything in the PropertyDrawer. I've also tried using <code>GUI.UnfocusWindow()</code>, but either it doesn't work in PropertyDrawer or it's only meant for Unity's windows or something.</p>