Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AxonGenesis · Nov 22, 2019 at 07:49 PM · editor-scriptingeventsgame view

Is there a way to implement mouse events in the game view in edit mode?

I'm creating custom workflow tools in the Unity Editor and I'd like to intercept mouse events in the game view (not the scene view) to implement custom behaviors but am not having much luck. The most basic feature I want is to select an object clicked in the game view.

I've explored different approaches and I have a somewhat working script, but there are issues.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 [ExecuteInEditMode()]
 public class TestClick : MonoBehaviour
 {
 #if UNITY_EDITOR
 
     public void OnGUI()
     {
         Event e = Event.current;
         if (e == null) return;
         Debug.Log("Event:"+ e);
 
         if (e.button == 1) {
             Debug.Log("Event:" + e.type +" pos:" + e.mousePosition + " camera:" + Camera.current.name + " button:" + e.button + " isMouse:" + e.isMouse);
 
             GameObject picked = HandleUtility.PickGameObject(e.mousePosition, false);
             if (picked == null) {
                 Debug.Log("Nothing picked");
             }
             else {
                 Debug.Log("picked:" + picked.name);
             }
         }
     }
 
 #endif
 }


The first issue is that the only event types received are Repaint and Layout. For some reason OnGUI is not getting any other types of events, so even though I can detect e.button, e.isMouse is always false, making it impossible to handle the events properly.

Secondly, even though PickedGameObject is working in that returns the right object, I get these errors from it:

 GUI Window tried to begin rendering while something else had not finished rendering! Either you have a recursive OnGUI rendering, or the previous OnGUI did not clean up properly.
 
 Assertion failed on expression: 'device.IsInsideFrame()' 

I tried putting these in a Handles.BeginGUI and EndGUI block, but makes no difference.

Is there a better way to intercept mouse events in the game view in edit mode? Not opposed to making an editor window, but I could not find a way to get any mouse events outside of the window.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sarahnorthway · Feb 23, 2021 at 07:11 PM

I learned here that calling EditorUtility.SetDirty during those Layout/Repaint events will cause mouse events to fire. Not sure why? I then use a raycast to find the object under the mouse in the Game window (requires colliders though).

 #if UNITY_EDITOR
 private void OnGUI() {
     if (Event.current.type == EventType.Layout || Event.current.type == EventType.Repaint) {
         UnityEditor.EditorUtility.SetDirty(this);
     } else if (Event.current.type == EventType.MouseDown) {
         DoMouseDown();
     }
 }
 
 private void DoMouseDown() {
     Vector2 mousePos = Event.current.mousePosition;
     mousePos.y = Screen.height - mousePos.y;
     Ray ray = Camera.main.ScreenPointToRay(mousePos);
     
     bool raycastSuccess = Physics.Raycast(ray, out raycastHit, 1000);
     if (!raycastSuccess) return;
 
     Debug.Log("Clicked on " + raycastHit.collider + " at " + raycastHit.point);
 }
 #endif

(edit: found the unrelated bug that was messing with my Hierarchy view)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multi selection of grid values in a custom Editor Window. 1 Answer

Custom Inspector: Unity Events 2 Answers

Get UnityEvent reference from SerializedProperty 1 Answer

UnityEvent Serialization Problem 1 Answer

Unity Editor Scripts: GameObject Added / Removed Event? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges