Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by zardini123 · Nov 24, 2013 at 10:00 PM · editorevent

Event Mouse not working correctly

Im working on a custom editor, and the problem is, a simple Event.MouseDown and Event.MouseUp isn't working correctly at all. Code:

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Collections;
 
 [CustomEditor(typeof(Thing)), Serializable, InitializeOnLoad]
 public class ThingEditor : Editor {
 
     void OnSceneGUI(){
 
         Event current = Event.current;
         
         Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); 
 
         if(current.type == EventType.MouseDown) {
             Debug.Log ("HI");
         }
 
         if (current.type == EventType.MouseUp) {
             Debug.Log ("BYE");
         }
     }
 }

Its not even saying BYE when I want it to.

Comment
Add comment · Show 2
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
avatar image ffxz7ff · Nov 24, 2013 at 11:03 PM 0
Share

I don't know why your code isn't working because I never tried your approach, but I use

 if(Input.Get$$anonymous$$ouseButtonDown(0))

and

 Input.Get$$anonymous$$ouseButtonUp(0))

in one of my scripts and it works just fine. $$anonymous$$aybe try that? Then again I just saw you're doing something with OnSceneGUI, that was for OnGUI..

avatar image zardini123 · Nov 24, 2013 at 11:30 PM 1
Share

The "Input" class is only used In-Game. When wanting to get input In-Editor, you have to use Event. Its weird that way.

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Bunny83 · Nov 24, 2013 at 10:33 PM

Are you sure your ThingEditor is active? So you have a Thing selected? Otherwise it ofcourse won't work.

If you want to handle all events you should subscribe an event to SceneView.onSceneGUIDelegate. Unfortunately the SceneView class is still not documented but is available since i can think back.

Next thing is InitializeOnLoad only loads the class at start. That will initialize the static members of the class. It won't create an instance of this editor. An editor is ment to "edit" an object (the target). Unless you have selected such an object (in your case a "Thing") there won't be an instance of this editor.

To handle generic SceneView events you should only use static things. This should work:

 [InitializeOnLoad]
 public class SceneViewEventHandler : Editor
 {
     // static constructor
     static SceneViewEventHandler()
     {
         SceneView.onSceneGUIDelegate += OnSceneGUI;
     }
 
     static void OnSceneGUI(SceneView aView)
     {
         Event current = Event.current;
         Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
         
         if(current.type == EventType.MouseDown)
         {
             Debug.Log ("HI");
         }
  
         if (current.type == EventType.MouseUp)
         {
             Debug.Log ("BYE");
         }
     }
 }

Keep in mind that the onSceneGUIDelegate is called for each SceneView independently. If you want to refer to the scene view, use the parameter that is passed to the delegate.

If you plan more complicated editor extension you might want to download ILSpy and take a look into the UnityEditor.dll. It really helps to understand the system better.

http://docs.unity3d.com/Documentation/Manual/RunningEditorCodeOnLaunch.html

Comment
Add comment · Show 4 · 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
avatar image zardini123 · Nov 24, 2013 at 10:41 PM 0
Share

Nope this did not work. Everything i coded works fine except Event for the mouse. It says hi when i press my mouse down, but doesn't say bye when i let go.

avatar image Bunny83 · Nov 25, 2013 at 01:42 AM 0
Share

If i use your code it works as i told you. The problem is when you click down and release you will deselect your object and your editor will be disabled. Also the sceneview eats the mouseup event in this case. Just test your middle or right mouse button. They will work just fine.

If you want to prevent deselecting of your object you have to register yourself as hotcontrol while your mouse is down. Again, if you take a look at UnityEditor library you can learn a lot. For example the TerrainInspector which implements the terrain-drawing-brush.

You have to do something like that:

 int hash = "SomeUniqueStringForYourEditorType".GetHashCode();
 void OnSceneGUI()
 {
     Event current = Event.current;
     int ID = GUIUtility.GetControlID(hash, FocusType.Passive);
     EventType type = current.GetTypeForControl(ID);
     Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
     
     if (type == EventType.Layout)
     {
         HandleUtility.AddDefaultControl(ID);
     }
     if(type == EventType.$$anonymous$$ouseDown && HandleUtility.nearestControl == ID)
     {
         Debug.Log ("HI");
         GUIUtility.hotControl = ID;
         current.Use();
     }
     
     if (type == EventType.$$anonymous$$ouseUp)
     {
         Debug.Log ("BYE");
         if (GUIUtility.hotControl == ID)
             GUIUtility.hotControl = 0;
         current.Use();
     }
 }

What's still missing in this example is that you should somehow disable "your" handling or a lot of the default features will be blocked.

So important is to only register your control id as default control when you actually want to get the mouse input. Also only "Use" the events when you are actually handling them. Also make sure you don't set the hotcontrol without resetting it, otherwise it would block the whole sceneView.

avatar image Socapex Bunny83 · Dec 27, 2016 at 08:04 PM 0
Share

Thank you! This is really helpful :)

avatar image zardini123 · Nov 25, 2013 at 11:55 AM 0
Share

The newest code you posted worked beautifully! Thanks a bunch Bunny83!

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

19 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

Related Questions

Left Click Up Event in Editor 4 Answers

OnGUI Event 0 Answers

Detecting moment when GameObiect is created in hierarchy. 1 Answer

Adding Tab support to TextArea's TextEditor (almost done) 2 Answers

Persistent listeners for Button.onClick are removed at runtime, if buttons are instances of prefab 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