Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 emreCanbazoglu · May 27, 2013 at 11:35 PM · c#guieditorinput

Editor Scripting Input Problem OnGUI()

I'm trying to use mouse imputs in editor scripting by using "Event" class in OnGUI funcion. But as OnGUI is called multiple times in one frame I can't do what I want to. Here is the code I use. Is there a way to limit the input count in a frame or use Update function?

     private void CheckInput()
     { 
          _e = Event.current;
 
          if (_e.type == EventType.MouseDown && _e.button == 0)
               _isMouseDown = true;
          else if (_e.type == EventType.MouseUp && _e.button == 0)
              _isMouseDown = false;
 
          _prevMousePos = _curMousePos;
 
          _curMousePos.x = _e.mousePosition.x;
          _curMousePos.y = _e.mousePosition.y;
          
     }
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 Benproductions1 · May 27, 2013 at 11:41 PM 0
Share

Just use the Update function :)

avatar image emreCanbazoglu · May 28, 2013 at 08:46 AM 0
Share

I can't. It gives NullReferenceException if I use event in Update function. I think events can be used only in OnGUI function.

1 Reply

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

Answer by numberkruncher · May 28, 2013 at 08:52 AM

The Mouse down event should only occur once each time the mouse button is pressed down. You are, however, assigning your mouse data too frequently:

Note: If you do not use GUIUtility.hotControl then there is no guarantee that EventType.MouseUp will be triggered.

Example along lines of your question:

 private void CheckInput() {
     // Get a unique ID for your control.
     int controlID = GUIUtility.GetControlID(FocusType.Passive);

     switch (Event.current.GetTypeForControl(controlID)) {
         case EventType.MouseDown:
             // If you have a rectangle for your control then you should
             // perform bounds check here...
             if (Event.current.button == 0) {
                 // Lock on your custom control to prevent interaction with
                 // other controls in your GUI.
                 GUIUtility.hotControl = controlID;
                 Event.current.Use();

                 _isMouseDown = true;
             }
             break;

         case EventType.MouseMove:
         case EventType.MouseDrag:
             // Only update mouse position on move or drag.
             _prevMousePos = _curMousePos;
 
             _curMousePos.x = Event.current.mousePosition.x;
             _curMousePos.y = Event.current.mousePosition.y;
             break;

         case EventType.MouseUp:
             // If this control is currently active.
             if (GUIUtility.hotControl == controlID) {
                 // Release lock on it :)
                 GUIUtility.hotControl = 0;
                 Event.current.Use();

                 _isMouseDown = false;
             }
             break;
     }
 }

Example with GUILayout:

 private static readonly GUIStyle _customStyle;

 private void CheckInput() {
     // Initialise custom style which defines control area of 300x300.
     if (_customStyle == null) {
         _customStyle = new GUIStyle();
         _customStyle.fixedWidth = 300;
         _customStyle.fixedHeight = 300;
     }

     // Get position of control area using layout.
     Rect position = GUILayoutUtility.GetRect(GUIContent.none, _customStyle);
     // Get a unique ID for your control.
     int controlID = GUIUtility.GetControlID(FocusType.Passive, position);

     switch (Event.current.GetTypeForControl(controlID)) {
         case EventType.MouseDown:
             // If you have a rectangle for your control then you should
             // perform bounds check here...
             if (Event.current.button == 0
                     && position.Contains(Event.current.mousePosition)) {
                 // Lock on your custom control to prevent interaction with
                 // other controls in your GUI.
                 GUIUtility.hotControl = controlID;
                 Event.current.Use();

                 _isMouseDown = true;
             }
             break;

         case EventType.MouseMove:
         case EventType.MouseDrag:
             // Only update mouse position on move or drag.
             _prevMousePos = _curMousePos;
 
             _curMousePos.x = Event.current.mousePosition.x;
             _curMousePos.y = Event.current.mousePosition.y;
             break;

         case EventType.MouseUp:
             // If this control is currently active.
             if (GUIUtility.hotControl == controlID) {
                 // Release lock on it :)
                 GUIUtility.hotControl = 0;
                 Event.current.Use();

                 _isMouseDown = false;
             }
             break;
     }
 }
Comment
Add comment · Show 1 · 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 emreCanbazoglu · May 28, 2013 at 07:01 PM 0
Share

Thanks man. That helped a lot!

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

15 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

Related Questions

Game Menus WIth Keyboard Input Control 1 Answer

Trying to Highlight text in order to copy and paste, but do not want the text to be editable. 1 Answer

Custom Timeline Playable - GUI Override 0 Answers

How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers

Importing .png via script 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